• Python Basics
  • Interview Questions
  • Python Quiz
  • Popular Packages
  • Python Projects
  • Practice Python
  • AI With Python
  • Learn Python3
  • Python Automation
  • Python Web Dev
  • DSA with Python
  • Python OOPs
  • Dictionaries
  • How to Fix - UnboundLocalError: Local variable Referenced Before Assignment in Python
  • UnboundLocalError Local variable Referenced Before Assignment in Python
  • How to use Pickle to save and load Variables in Python?
  • How to Use a Variable from Another Function in Python
  • How to fix Unresolved reference issue in PyCharm
  • Unused variable in for loop in Python
  • How to Access Dictionary Values in Python Using For Loop
  • Python | Accessing variable value from code scope
  • How to fix "SyntaxError: invalid character" in Python
  • Assign Function to a Variable in Python
  • How to Reference Elements in an Array in Python
  • How to Fix: SyntaxError: positional argument follows keyword argument in Python
  • How To Fix Valueerror Exceptions In Python
  • How To Fix - Python RuntimeWarning: overflow encountered in scalar
  • Different Forms of Assignment Statements in Python
  • Unused local variable in Python
  • Access environment variable values in Python
  • How to fix "ValueError: invalid literal for int() with base 10" in Python
  • How to import variables from another file in Python?
  • JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization

How to Fix – UnboundLocalError: Local variable Referenced Before Assignment in Python

Developers often encounter the  UnboundLocalError Local Variable Referenced Before Assignment error in Python. In this article, we will see what is local variable referenced before assignment error in Python and how to fix it by using different approaches.

What is UnboundLocalError: Local variable Referenced Before Assignment?

This error occurs when a local variable is referenced before it has been assigned a value within a function or method. This error typically surfaces when utilizing try-except blocks to handle exceptions, creating a puzzle for developers trying to comprehend its origins and find a solution.

Below, are the reasons by which UnboundLocalError: Local variable Referenced Before Assignment error occurs in  Python :

Nested Function Variable Access

Global variable modification.

In this code, the outer_function defines a variable ‘x’ and a nested inner_function attempts to access it, but encounters an UnboundLocalError due to a local ‘x’ being defined later in the inner_function.

In this code, the function example_function tries to increment the global variable ‘x’, but encounters an UnboundLocalError since it’s treated as a local variable due to the assignment operation within the function.

Solution for Local variable Referenced Before Assignment in Python

Below, are the approaches to solve “Local variable Referenced Before Assignment”.

In this code, example_function successfully modifies the global variable ‘x’ by declaring it as global within the function, incrementing its value by 1, and then printing the updated value.

In this code, the outer_function defines a local variable ‘x’, and the inner_function accesses and modifies it as a nonlocal variable, allowing changes to the outer function’s scope from within the inner function.

Please Login to comment...

Similar reads.

  • Python Errors
  • Python How-to-fix
  • Python Programs

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

How to fix UnboundLocalError: local variable 'x' referenced before assignment in Python

You could also see this error when you forget to pass the variable as an argument to your function.

How to reproduce this error

How to fix this error.

I hope this tutorial is useful. See you in other tutorials.

Take your skills to the next level ⚡️

Local variable referenced before assignment in Python

avatar

Last updated: Apr 8, 2024 Reading time · 4 min

banner

# Local variable referenced before assignment in Python

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function.

To solve the error, mark the variable as global in the function definition, e.g. global my_var .

unboundlocalerror local variable name referenced before assignment

Here is an example of how the error occurs.

We assign a value to the name variable in the function.

# Mark the variable as global to solve the error

To solve the error, mark the variable as global in your function definition.

mark variable as global

If a variable is assigned a value in a function's body, it is a local variable unless explicitly declared as global .

# Local variables shadow global ones with the same name

You could reference the global name variable from inside the function but if you assign a value to the variable in the function's body, the local variable shadows the global one.

accessing global variables in functions

Accessing the name variable in the function is perfectly fine.

On the other hand, variables declared in a function cannot be accessed from the global scope.

variables declared in function cannot be accessed in global scope

The name variable is declared in the function, so trying to access it from outside causes an error.

Make sure you don't try to access the variable before using the global keyword, otherwise, you'd get the SyntaxError: name 'X' is used prior to global declaration error.

# Returning a value from the function instead

An alternative solution to using the global keyword is to return a value from the function and use the value to reassign the global variable.

return value from the function

We simply return the value that we eventually use to assign to the name global variable.

# Passing the global variable as an argument to the function

You should also consider passing the global variable as an argument to the function.

pass global variable as argument to function

We passed the name global variable as an argument to the function.

If we assign a value to a variable in a function, the variable is assumed to be local unless explicitly declared as global .

# Assigning a value to a local variable from an outer scope

If you have a nested function and are trying to assign a value to the local variables from the outer function, use the nonlocal keyword.

assign value to local variable from outer scope

The nonlocal keyword allows us to work with the local variables of enclosing functions.

Had we not used the nonlocal statement, the call to the print() function would have returned an empty string.

not using nonlocal prints empty string

Printing the message variable on the last line of the function shows an empty string because the inner() function has its own scope.

Changing the value of the variable in the inner scope is not possible unless we use the nonlocal keyword.

Instead, the message variable in the inner function simply shadows the variable with the same name from the outer scope.

# Discussion

As shown in this section of the documentation, when you assign a value to a variable inside a function, the variable:

  • Becomes local to the scope.
  • Shadows any variables from the outer scope that have the same name.

The last line in the example function assigns a value to the name variable, marking it as a local variable and shadowing the name variable from the outer scope.

At the time the print(name) line runs, the name variable is not yet initialized, which causes the error.

The most intuitive way to solve the error is to use the global keyword.

The global keyword is used to indicate to Python that we are actually modifying the value of the name variable from the outer scope.

  • If a variable is only referenced inside a function, it is implicitly global.
  • If a variable is assigned a value inside a function's body, it is assumed to be local, unless explicitly marked as global .

If you want to read more about why this error occurs, check out [this section] ( this section ) of the docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

  • SyntaxError: name 'X' is used prior to global declaration

book cover

Borislav Hadzhiev

Web Developer

buy me a coffee

Copyright © 2024 Borislav Hadzhiev

The Research Scientist Pod

Python UnboundLocalError: local variable referenced before assignment

by Suf | Programming , Python , Tips

If you try to reference a local variable before assigning a value to it within the body of a function, you will encounter the UnboundLocalError: local variable referenced before assignment.

The preferable way to solve this error is to pass parameters to your function, for example:

Alternatively, you can declare the variable as global to access it while inside a function. For example,

This tutorial will go through the error in detail and how to solve it with code examples .

Table of contents

What is scope in python, unboundlocalerror: local variable referenced before assignment, solution #1: passing parameters to the function, solution #2: use global keyword, solution #1: include else statement, solution #2: use global keyword.

Scope refers to a variable being only available inside the region where it was created. A variable created inside a function belongs to the local scope of that function, and we can only use that variable inside that function.

A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available within any scope, global and local.

UnboundLocalError occurs when we try to modify a variable defined as local before creating it. If we only need to read a variable within a function, we can do so without using the global keyword. Consider the following example that demonstrates a variable var created with global scope and accessed from test_func :

If we try to assign a value to var within test_func , the Python interpreter will raise the UnboundLocalError:

This error occurs because when we make an assignment to a variable in a scope, that variable becomes local to that scope and overrides any variable with the same name in the global or outer scope.

var +=1 is similar to var = var + 1 , therefore the Python interpreter should first read var , perform the addition and assign the value back to var .

var is a variable local to test_func , so the variable is read or referenced before we have assigned it. As a result, the Python interpreter raises the UnboundLocalError.

Example #1: Accessing a Local Variable

Let’s look at an example where we define a global variable number. We will use the increment_func to increase the numerical value of number by 1.

Let’s run the code to see what happens:

The error occurs because we tried to read a local variable before assigning a value to it.

We can solve this error by passing a parameter to increment_func . This solution is the preferred approach. Typically Python developers avoid declaring global variables unless they are necessary. Let’s look at the revised code:

We have assigned a value to number and passed it to the increment_func , which will resolve the UnboundLocalError. Let’s run the code to see the result:

We successfully printed the value to the console.

We also can solve this error by using the global keyword. The global statement tells the Python interpreter that inside increment_func , the variable number is a global variable even if we assign to it in increment_func . Let’s look at the revised code:

Let’s run the code to see the result:

Example #2: Function with if-elif statements

Let’s look at an example where we collect a score from a player of a game to rank their level of expertise. The variable we will use is called score and the calculate_level function takes in score as a parameter and returns a string containing the player’s level .

In the above code, we have a series of if-elif statements for assigning a string to the level variable. Let’s run the code to see what happens:

The error occurs because we input a score equal to 40 . The conditional statements in the function do not account for a value below 55 , therefore when we call the calculate_level function, Python will attempt to return level without any value assigned to it.

We can solve this error by completing the set of conditions with an else statement. The else statement will provide an assignment to level for all scores lower than 55 . Let’s look at the revised code:

In the above code, all scores below 55 are given the beginner level. Let’s run the code to see what happens:

We can also create a global variable level and then use the global keyword inside calculate_level . Using the global keyword will ensure that the variable is available in the local scope of the calculate_level function. Let’s look at the revised code.

In the above code, we put the global statement inside the function and at the beginning. Note that the “default” value of level is beginner and we do not include the else statement in the function. Let’s run the code to see the result:

Congratulations on reading to the end of this tutorial! The UnboundLocalError: local variable referenced before assignment occurs when you try to reference a local variable before assigning a value to it. Preferably, you can solve this error by passing parameters to your function. Alternatively, you can use the global keyword.

If you have if-elif statements in your code where you assign a value to a local variable and do not account for all outcomes, you may encounter this error. In which case, you must include an else statement to account for the missing outcome.

For further reading on Python code blocks and structure, go to the article: How to Solve Python IndentationError: unindent does not match any outer indentation level .

Go to the  online courses page on Python  to learn more about Python for data science and machine learning.

Have fun and happy researching!

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Tumblr (Opens in new window)

[SOLVED] Local Variable Referenced Before Assignment

local variable referenced before assignment

Python treats variables referenced only inside a function as global variables. Any variable assigned to a function’s body is assumed to be a local variable unless explicitly declared as global.

Why Does This Error Occur?

Unboundlocalerror: local variable referenced before assignment occurs when a variable is used before its created. Python does not have the concept of variable declarations. Hence it searches for the variable whenever used. When not found, it throws the error.

Before we hop into the solutions, let’s have a look at what is the global and local variables.

Local Variable Declarations vs. Global Variable Declarations

Local VariablesGlobal Variables
A variable is declared primarily within a Python function.Global variables are in the global scope, outside a function.
A local variable is created when the function is called and destroyed when the execution is finished.A Variable is created upon execution and exists in memory till the program stops.
Local Variables can only be accessed within their own function.All functions of the program can access global variables.
Local variables are immune to changes in the global scope. Thereby being more secure.Global Variables are less safer from manipulation as they are accessible in the global scope.

[Fixed] typeerror can’t compare datetime.datetime to datetime.date

Local Variable Referenced Before Assignment Error with Explanation

Try these examples yourself using our Online Compiler.

Let’s look at the following function:

Local Variable Referenced Before Assignment Error

Explanation

The variable myVar has been assigned a value twice. Once before the declaration of myFunction and within myFunction itself.

Using Global Variables

Passing the variable as global allows the function to recognize the variable outside the function.

Create Functions that Take in Parameters

Instead of initializing myVar as a global or local variable, it can be passed to the function as a parameter. This removes the need to create a variable in memory.

UnboundLocalError: local variable ‘DISTRO_NAME’

This error may occur when trying to launch the Anaconda Navigator in Linux Systems.

Upon launching Anaconda Navigator, the opening screen freezes and doesn’t proceed to load.

Try and update your Anaconda Navigator with the following command.

If solution one doesn’t work, you have to edit a file located at

After finding and opening the Python file, make the following changes:

In the function on line 159, simply add the line:

DISTRO_NAME = None

Save the file and re-launch Anaconda Navigator.

DJANGO – Local Variable Referenced Before Assignment [Form]

The program takes information from a form filled out by a user. Accordingly, an email is sent using the information.

Upon running you get the following error:

We have created a class myForm that creates instances of Django forms. It extracts the user’s name, email, and message to be sent.

A function GetContact is created to use the information from the Django form and produce an email. It takes one request parameter. Prior to sending the email, the function verifies the validity of the form. Upon True , .get() function is passed to fetch the name, email, and message. Finally, the email sent via the send_mail function

Why does the error occur?

We are initializing form under the if request.method == “POST” condition statement. Using the GET request, our variable form doesn’t get defined.

Local variable Referenced before assignment but it is global

This is a common error that happens when we don’t provide a value to a variable and reference it. This can happen with local variables. Global variables can’t be assigned.

This error message is raised when a variable is referenced before it has been assigned a value within the local scope of a function, even though it is a global variable.

Here’s an example to help illustrate the problem:

In this example, x is a global variable that is defined outside of the function my_func(). However, when we try to print the value of x inside the function, we get a UnboundLocalError with the message “local variable ‘x’ referenced before assignment”.

This is because the += operator implicitly creates a local variable within the function’s scope, which shadows the global variable of the same name. Since we’re trying to access the value of x before it’s been assigned a value within the local scope, the interpreter raises an error.

To fix this, you can use the global keyword to explicitly refer to the global variable within the function’s scope:

However, in the above example, the global keyword tells Python that we want to modify the value of the global variable x, rather than creating a new local variable. This allows us to access and modify the global variable within the function’s scope, without causing any errors.

Local variable ‘version’ referenced before assignment ubuntu-drivers

This error occurs with Ubuntu version drivers. To solve this error, you can re-specify the version information and give a split as 2 –

Here, p_name means package name.

With the help of the threading module, you can avoid using global variables in multi-threading. Make sure you lock and release your threads correctly to avoid the race condition.

When a variable that is created locally is called before assigning, it results in Unbound Local Error in Python. The interpreter can’t track the variable.

Therefore, we have examined the local variable referenced before the assignment Exception in Python. The differences between a local and global variable declaration have been explained, and multiple solutions regarding the issue have been provided.

Trending Python Articles

[Fixed] nameerror: name Unicode is not defined

local variable 'batch_outputs' referenced before assignment

Explore your training options in 10 minutes Get Started

  • Graduate Stories
  • Partner Spotlights
  • Bootcamp Prep
  • Bootcamp Admissions
  • University Bootcamps
  • Coding Tools
  • Software Engineering
  • Web Development
  • Data Science
  • Tech Guides
  • Tech Resources
  • Career Advice
  • Online Learning
  • Internships
  • Apprenticeships
  • Tech Salaries
  • Associate Degree
  • Bachelor's Degree
  • Master's Degree
  • University Admissions
  • Best Schools
  • Certifications
  • Bootcamp Financing
  • Higher Ed Financing
  • Scholarships
  • Financial Aid
  • Best Coding Bootcamps
  • Best Online Bootcamps
  • Best Web Design Bootcamps
  • Best Data Science Bootcamps
  • Best Technology Sales Bootcamps
  • Best Data Analytics Bootcamps
  • Best Cybersecurity Bootcamps
  • Best Digital Marketing Bootcamps
  • Los Angeles
  • San Francisco
  • Browse All Locations
  • Digital Marketing
  • Machine Learning
  • See All Subjects
  • Bootcamps 101
  • Full-Stack Development
  • Career Changes
  • View all Career Discussions
  • Mobile App Development
  • Cybersecurity
  • Product Management
  • UX/UI Design
  • What is a Coding Bootcamp?
  • Are Coding Bootcamps Worth It?
  • How to Choose a Coding Bootcamp
  • Best Online Coding Bootcamps and Courses
  • Best Free Bootcamps and Coding Training
  • Coding Bootcamp vs. Community College
  • Coding Bootcamp vs. Self-Learning
  • Bootcamps vs. Certifications: Compared
  • What Is a Coding Bootcamp Job Guarantee?
  • How to Pay for Coding Bootcamp
  • Ultimate Guide to Coding Bootcamp Loans
  • Best Coding Bootcamp Scholarships and Grants
  • Education Stipends for Coding Bootcamps
  • Get Your Coding Bootcamp Sponsored by Your Employer
  • GI Bill and Coding Bootcamps
  • Tech Intevriews
  • Our Enterprise Solution
  • Connect With Us
  • Publication
  • Reskill America
  • Partner With Us

Career Karma

  • Resource Center
  • Bachelor’s Degree
  • Master’s Degree

Python local variable referenced before assignment Solution

When you start introducing functions into your code, you’re bound to encounter an UnboundLocalError at some point. This error is raised when you try to use a variable before it has been assigned in the local context .

In this guide, we talk about what this error means and why it is raised. We walk through an example of this error in action to help you understand how you can solve it.

Find your bootcamp match

What is unboundlocalerror: local variable referenced before assignment.

Trying to assign a value to a variable that does not have local scope can result in this error:

Python has a simple rule to determine the scope of a variable. If a variable is assigned in a function , that variable is local. This is because it is assumed that when you define a variable inside a function you only need to access it inside that function.

There are two variable scopes in Python: local and global. Global variables are accessible throughout an entire program; local variables are only accessible within the function in which they are originally defined.

Let’s take a look at how to solve this error.

An Example Scenario

We’re going to write a program that calculates the grade a student has earned in class.

We start by declaring two variables:

These variables store the numerical and letter grades a student has earned, respectively. By default, the value of “letter” is “F”. Next, we write a function that calculates a student’s letter grade based on their numerical grade using an “if” statement :

Finally, we call our function:

This line of code prints out the value returned by the calculate_grade() function to the console. We pass through one parameter into our function: numerical. This is the numerical value of the grade a student has earned.

Let’s run our code and see what happens:

An error has been raised.

The Solution

Our code returns an error because we reference “letter” before we assign it.

We have set the value of “numerical” to 42. Our if statement does not set a value for any grade over 50. This means that when we call our calculate_grade() function, our return statement does not know the value to which we are referring.

We do define “letter” at the start of our program. However, we define it in the global context. Python treats “return letter” as trying to return a local variable called “letter”, not a global variable.

We solve this problem in two ways. First, we can add an else statement to our code. This ensures we declare “letter” before we try to return it:

Let’s try to run our code again:

Our code successfully prints out the student’s grade.

If you are using an “if” statement where you declare a variable, you should make sure there is an “else” statement in place. This will make sure that even if none of your if statements evaluate to True, you can still set a value for the variable with which you are going to work.

Alternatively, we could use the “global” keyword to make our global keyword available in the local context in our calculate_grade() function. However, this approach is likely to lead to more confusing code and other issues. In general, variables should not be declared using “global” unless absolutely necessary . Your first, and main, port of call should always be to make sure that a variable is correctly defined.

In the example above, for instance, we did not check that the variable “letter” was defined in all use cases.

That’s it! We have fixed the local variable error in our code.

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.

Now you’re ready to solve UnboundLocalError Python errors like a professional developer !

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication .

What's Next?

icon_10

Get matched with top bootcamps

Ask a question to our community, take our careers quiz.

James Gallagher

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Apply to top tech training programs in one click

UnboundLocalError: local variable 'beta1' referenced before assignment

I got an error about an local variable referenced before assignment:

Can you provide us a MRE? It’s hard to understand what’s going on without the code. Nevertheless, usually you get this error when the variable, beta1 in this case, can’t be computed or initialized and it’s used immediately after. For instance, let’s suppose you have the following code:

If the condition is not met, you won’t be able to compute beta1 , which is needed to compute the loss variable aswell. So give a look at your code and check if the variable beta1 can be always computed.

Actually, I am not using beta1 anywhere myself. It is a variable that is set by torch in the AdamW optimizer. This is why I am confused.

Can you post here the code in which you define the optimizer?

Because you just need to pass a tuple of values to AdamW called betas , not beta1 , so I’m a bit confused.

Sure, here you go:

I guess for some reason none of the passed parameters have a valid gradient, which will thus break the code. This was a known issue in PyTorch 1.8.0 after a refactoring of the optimizers, which was then fixed in this PR and should be available in 1.8.1 as well as the nightly binaries. To fix it you could thus update PyTorch or make sure that some of the parameters have a gradient.

Hi, @ptrblck . My torch verision is 1.8.1+cu111. Why I still met this error? Thanks.

The PR might not have been picked into 1.8.1 (you could check it by searching for the commit in the v1.8.1 branch). As aquick check, update to the latest release and rerun your code.

Python doesn’t have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . The unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). All variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

Why do I get UnboundLocalError: local variable 'batch_idx' referenced before assignment when using interleaved data sets with Hugging Face (HF)?

I get the following error:

it happens when I interleave my data set:

why is this happening?

ref: machine learning - Why do I get UnboundLocalError: local variable 'batch_idx' referenced before assignment when using interleaved data sets with Hugging Face (HF)? - Stack Overflow ref: Why do I get UnboundLocalError: local variable 'batch_idx' referenced before assignment when using interleaved data sets with Hugging Face (HF)? ref: Discord

The issue happened because llama2 does not have a max sequence length set. So it defaults to max int 1000000000000000019884624838656 . You can set it manually if you google the max seq len for your model e.g., for llama2-7b:

That should remove the issue in the code because

will not be zero when trying to concatenate all 1000 texts in the batch when forming a batch all of which have a length of block size (assumign block size is max seq len or some value you chose).

ref: [Tokenizers]What this max_length number?

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.

Related Topics

Topic Replies Views Activity
🤗Accelerate 5 1108 June 30, 2021
🤗Datasets 2 18397 February 6, 2023
🤗AutoTrain 0 211 March 27, 2024
🤗Optimum 4 1151 June 13, 2024
Beginners 0 204 October 23, 2023

UndboundLocalError: local variable referenced before assignment

Hello all, I’m using PsychoPy 2023.2.3 Win 10 x64bits

image

What I’m trying to do? The experiment will show in the middle of the screen an abstracted stimuli (B1 or B2), and after valid click on it, the stimulus will remain on the middle of the screen and three more stimuli will appear in the cornor of the screen.

I’m having this erro (attached above), a simple error, but I can not see where the error is. Also the experiment isn’t working proberly and is the old version (I don’t know but someone are having troubles with this version of PscyhoPy)? ba_training_block.xlsx (13.8 KB) SMTS.psyexp (91.6 KB) stimuli, instructions and parameters.xlsx (12.8 KB)

You have a routine called sample but you also use that name for your image file in sample_box .

I changed the name of the routine for ‘stimulus_sample’ and manteined the image file in sample_box as ‘sample’. But, the error still remain. But it do not happen all the time, this is very interesting…

Can u give it a look again? (I made some minor changes here)

image

Here the exp file ba_training_block.xlsx (13.7 KB) SMTS.psyexp (89.7 KB) stimuli, instructions and parameters.xlsx (12.8 KB)

Thanks again

Please could you confirm/show the new error message? Is it definitely still related to sample?

image

I think you have blank rows in your spreadsheet. The loop claims that there are 19 conditions but I think you only want 12. Without a value for sample_category sample doesn’t get set. With random presentation this will happen at a random point.

Related Topics

Topic Replies Views Activity
Builder 10 4131 February 9, 2024
Builder 2 175 April 22, 2024
Builder 1 182 October 17, 2023
Builder 2 518 February 1, 2023
Builder 1 51 June 6, 2024

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError: local variable 'logs' referenced before assignment on training with little data #38064

@omalleyt12

naripok commented Mar 31, 2020

I found an error caused by an attempt of coping training logs from a not yet assigned variable.
The error occurred on my machine (Arch linux, tensorflow v2.2rc2 compiled from source) and I managed to reproduce the error on colab, on a stock environment.
It only happens when the model.fit method is called with very little training / eval data.
The variable is assigned inside a loop that never happens when there is no sufficient data.

The code lives here:

Line 793 in

epoch_logs = copy.copy(logs)

The notebook gist link for reproducing the bug: https://colab.research.google.com/gist/naripok/8ce09ec9c3e795b3635a6b1ac11ebd4b/tpu_transformer_model.ipynb

The text was updated successfully, but these errors were encountered:

  • 👍 18 reactions

@naripok

amahendrakar commented Mar 31, 2020

Was able to reproduce the issue with TF v2.2.0-rc2. Please find the gist . Thanks!

Sorry, something went wrong.

@gowthamkpr

Tuxius commented Apr 14, 2020

I have the same issue with TF v2.2.0-rc2

  • 👍 8 reactions

@PeterBrummer

PeterBrummer commented Apr 16, 2020

and others which stuck until this bug is fixed:
I had the same issue and I found that my validation data set had less samples as the batch_size. Because I'm working with TFRecords-data-sets which have no meta data about how many records, i.e. samples, are in the data set I check now upfront whether the data set contains at least batch_size records (samples).

For that I use the following helper functions:

Please keep in mind that by using the first helper function directly ,
i.e. doesDataSetContainsEnoughDataForBatch, you already read batch_size samples from the data set. So you should recreate the data set after the check.

By using the second helper function you just lose some execution time upfront.

If you don't use TFRecord data sets you might have a similar issue. But then it is also a good idea to check upfront whether you have enough data for at least one batch.

  • 👍 17 reactions

@alimhanif

alimhanif commented Apr 25, 2020

and others which stuck until this bug is fixed:
I had the same issue and I found that my validation data set had less samples as the batch_size. Because I'm working with TFRecords-data-sets which have no meta data about how many records, i.e. samples, are in the data set I check now upfront whether the data set contains at least batch_size records (samples).

For that I use the following helper functions:

Please keep in mind that by using the first helper function directly ,
i.e. doesDataSetContainsEnoughDataForBatch, you already read batch_size samples from the data set. So you should recreate the data set after the check.

By using the second helper function you just lose some execution time upfront.

If you don't use TFRecord data sets you might have a similar issue. But then it is also a good idea to check upfront whether you have enough data for at least one batch.

I added more data. It works well!
Thank you!

@tombelieber

tombelieber commented Apr 28, 2020

Hi there, I change the to smaller so that it could be more evenly divided during training.
For example, when you have 300 training samples, do not use , try or something, it works for me.

@ghost

ghost commented May 7, 2020

The issue is still reproducible with 2.2.0rc4, will test with 2.2.0 release.

@aliencaocao

aliencaocao commented May 8, 2020

2.2 is still reproducible for me

  • 👍 11 reactions

ghost commented May 11, 2020

2.2 is still reproducible indeed

@F-Node-Karlsruhe

F-Node-Karlsruhe commented May 26, 2020

Are there any other workarounds known yet? The batch size one did not work out for me.

  • 👍 6 reactions

@dominique-nshimyimana

dominique-nshimyimana commented Jun 2, 2020 • edited Loading

Same problem: Is there way to fix or alternatives ?

@avitomar12

avitomar12 commented Jun 10, 2020

same problem: Is there way ro fix the issue?

@AlmogDavid

AlmogDavid commented Jun 10, 2020

Same problem, the root cause for this issue is that the training cannot perform a single step because your dataset is not large enough to fill one training iteration.

Anyway, there is still a bug here, this is not the expected behavior.

  • 👍 4 reactions

avitomar12 commented Jun 11, 2020

Anyway, there is still a bug here, this is not the expected behavior.

Yes. You are right. When i increased training data the error doesnot occure.

@20tamil20

20tamil20 commented Jun 12, 2020

I have also same error arise , even increase the training dataset its not solved. Again i got the same error. kindly share me the solution if anyone knows about this.
Thank you

avitomar12 commented Jun 12, 2020

What is the size of the dataset you are using?

512, 512 size and 247 images and masks

@maraujo

maraujo commented Jun 12, 2020

I solved my particular issue setting the batch_size very small, in my case batch_size = 1. I confirm that this happens with small datasets.

  • 👍 7 reactions

20tamil20 commented Jun 13, 2020

yes sir, i have also reduced batchsize as 1, but i dont know the reason why the same error arise.

@mathpluscode

mathpluscode commented Jun 20, 2020 • edited Loading

I had the same error with using it turns out the or was zero. By making them >=1, the bug disappeared.

  • 👍 3 reactions

@dbspace

dbspace commented Jun 20, 2020

same problem also. is there a workaround?

@ducviet00

ducviet00 commented Jun 22, 2020 • edited Loading

same problem during training BiT. Batch size 128, Steps_per_epoch = 100
Edit: I throw out validation_data and it works. but I want valid model

  • 👍 1 reaction

@mridoc

mridoc commented Jun 30, 2020

Working with a small dataset
changed batch_size = 64 - worked

@ChungNPH

ChungNPH commented Jul 1, 2020

This error also raise when len(traindata) or len(validdata) is 0.

  • 👍 2 reactions

@DatGuy1

BibhuDas123 commented Jul 7, 2020

I found this error while running the training part of vgg16 model

def vgg16_model( num_classes=None):

vgg_conv=vgg16_model(6)

def kappa_score(y_true, y_pred):

opt = SGD(lr=0.001)
vgg_conv.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy',kappa_score])

nb_epochs = 3
batch_size = 16
nb_train_steps = train.shape[0]//batch_size
nb_val_steps=validation.shape[0]//batch_size

print("Number of training and validation steps: {} and {}".format(nb_train_steps,nb_val_steps))

check_point = ModelCheckpoint('./model.h5',monitor='val_loss',verbose=True, save_best_only=True, save_weights_only=True)

early_stop = EarlyStopping(monitor='val_loss',patience=25,verbose=True)

callbacks = [check_point,early_stop]

vgg_conv.fit_generator(
train_generator,
verbose=2,
steps_per_epoch=nb_train_steps,
epochs=nb_epochs,
validation_data=validation_generator,
validation_steps=nb_val_steps,
callbacks=callbacks,
use_multiprocessing=True)

Found 16 non-validated image filenames belonging to 6 classes.
Found 4 non-validated image filenames belonging to 2 classes.
Number of training and validation steps: 1 and 0
Epoch 1/3

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
322 'in a future version' if date is None else ('after %s' % date),
323 instructions)
--> 324 return func(*args, **kwargs)
325 return tf_decorator.make_decorator(
326 func, new_func, 'deprecated',

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1477 use_multiprocessing=use_multiprocessing,
1478 shuffle=shuffle,
-> 1479 initial_epoch=initial_epoch)
1480
1481 @deprecation.deprecated(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside already.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
870 workers=workers,
871 use_multiprocessing=use_multiprocessing,
--> 872 return_dict=True)
873 val_logs = {'val_' + name: val for name, val in val_logs.items()}
874 epoch_logs.update(val_logs)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside already.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in evaluate(self, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, return_dict)
1089 callbacks.on_test_end()
1090
-> 1091 logs = tf_utils.to_numpy_or_python_type(logs)
1092 if return_dict:
1093 return logs

UnboundLocalError: local variable 'logs' referenced before assignment

@cassianokc

cassianokc commented Jul 7, 2020

Can we add tag 2.3 for this issue?

@gbrlfaria

bersbersbers commented Jul 24, 2020

I am surprised there is no self-contained code example reproducing this, so here is one:

@mathpluscode

uzi0espil commented Jul 30, 2020 • edited Loading

This is reproducible in TF 2.3. I am using batch_size of 1 and no validation data.
By the way, this is working when I ran my custom model eager mode

  • 👍 5 reactions

@Smileek

jdraines commented Sep 3, 2020 • edited Loading

Encountered this error, myself, and thought I'd share my debugging in case others would benefit in some way.

Tracing the calls and returns, it seems that inside , a local variable is first defined inside a for loop iterating over , which, in many cases, is a number dependent on the cardinality of the Dataset. If the Dataset cardinality is zero, then performs 0 iterations, and will never be created, resulting in the UnboundLocalError we're all getting.

The cardinality of my Dataset was being returned as 0 because I had created it from a called on a that had a batch_size > 1 -- this was an artifact of a previous iteration of my code. (Worth noting that when was called on the with batch_size of 1, then my was nonzero.)

After cleaning the code (in my case, calling on a Dataset that had not been batched), my Dataset.cardinality() returned a nonzero value, and the error was resolved.

  • 🎉 1 reaction

@rollingdeep

rollingdeep commented Sep 9, 2020

when turn off validation_data, it works! tf. == '2.2.0'

@vokhidovhusan

vokhidovhusan commented Sep 9, 2020

check number of your training data. It might equal to zero

  • ❤️ 1 reaction

@Nithanaroy

Nithanaroy commented Oct 2, 2020

Looks like this happens when size of the training data is <= batch size

@ianovski

ianovski commented Oct 9, 2020

In my case, I read my tfrecords incorrectly but didn't get any warnings or errors. After reading the record, you can try printing its size as a sanity check.

@goldiegadde

goldiegadde commented Oct 12, 2020

the original issue of "UnboundLocalError: local variable 'logs' referenced before assignment" is no longer with the latest tf-nightly. Can you please confirm and if so close the issue ?

@tensorflowbutler

naripok commented Oct 23, 2020

Sorry for the delay,

Confirmed.

Thanks!

@google-ml-butler

google-ml-butler bot commented Oct 23, 2020

Are you satisfied with the resolution of your issue?

@hafiz031

hafiz031 commented Nov 13, 2020

The same situation can occur if the train-set has less number of examples than the batch size. In my case my train-set's directory was wrongly given.

@khlevnov

khlevnov commented Nov 22, 2020

Thank you!

@TommiRTVA

No branches or pull requests

@Nithanaroy

IMAGES

  1. [SOLVED] Local Variable Referenced Before Assignment

    local variable 'batch_outputs' referenced before assignment

  2. Local variable referenced before assignment Python

    local variable 'batch_outputs' referenced before assignment

  3. [Solved] Local Variable referenced before assignment

    local variable 'batch_outputs' referenced before assignment

  4. "Fixing UnboundLocalError: Local Variable Referenced Before Assignment"

    local variable 'batch_outputs' referenced before assignment

  5. [SOLVED] Local Variable Referenced Before Assignment

    local variable 'batch_outputs' referenced before assignment

  6. Local variable referenced before assignment in Python

    local variable 'batch_outputs' referenced before assignment

COMMENTS

  1. UnboundLocalError: local variable 'batch_outputs' referenced before

    UnboundLocalError: local variable 'batch_outputs' referenced before assignment. Ask Question Asked 3 years, 10 months ago. Modified 6 months ago. Viewed 7k times 12 I am writing machine learning code using Keras to grade the severity of prostate cancer. ... 1287 UnboundLocalError: local variable 'batch_outputs' referenced before assignment ...

  2. TensorFlow UnboundLocalError: local variable 'batch_outputs' referenced

    I am trying to run some python3 code on databricks GPU cluster for image understanding by CNN. The env: TensorFlow: 2.2 python 3.7.6 keras: 2.3.0-tf Unbuntu: 4.4 The code: import os import numpy as...

  3. UnboundLocalError: local variable 'batch_outputs' referenced before

    A user reports an error when training COM-finder with num_validation_per_exp set to 0. The owner suggests setting debug to False to fix the issue.

  4. How to Fix

    Output. Hangup (SIGHUP) Traceback (most recent call last): File "Solution.py", line 7, in <module> example_function() File "Solution.py", line 4, in example_function x += 1 # Trying to modify global variable 'x' without declaring it as global UnboundLocalError: local variable 'x' referenced before assignment Solution for Local variable Referenced Before Assignment in Python

  5. How to fix UnboundLocalError: local variable 'x' referenced before

    The UnboundLocalError: local variable 'x' referenced before assignment occurs when you reference a variable inside a function before declaring that variable. To resolve this error, you need to use a different variable name when referencing the existing variable, or you can also specify a parameter for the function. I hope this tutorial is useful.

  6. Local variable referenced before assignment in Python

    If a variable is assigned a value in a function's body, it is a local variable unless explicitly declared as global. # Local variables shadow global ones with the same name You could reference the global name variable from inside the function but if you assign a value to the variable in the function's body, the local variable shadows the global one.

  7. Python UnboundLocalError: local variable referenced before assignment

    UnboundLocalError: local variable referenced before assignment. Example #1: Accessing a Local Variable. Solution #1: Passing Parameters to the Function. Solution #2: Use Global Keyword. Example #2: Function with if-elif statements. Solution #1: Include else statement. Solution #2: Use global keyword. Summary.

  8. [SOLVED] Local Variable Referenced Before Assignment

    Output. 10 Create Functions that Take in Parameters. Instead of initializing myVar as a global or local variable, it can be passed to the function as a parameter. This removes the need to create a variable in memory. ... DJANGO - Local Variable Referenced Before Assignment [Form]

  9. how to solve the exception with "UnboundLocalError: local variable

    model.fit(x, y, epochs=epochs, batch_size=batch_size, verbose=2, shuffle=False) and then catch the exception as below: UnboundLocalError: local variable 'batch_index' referenced before assignment. I guess it maybe concern with epochs or batch_size, or shape of my dataset?

  10. Python local variable referenced before assignment Solution

    Trying to assign a value to a variable that does not have local scope can result in this error: UnboundLocalError: local variable referenced before assignment. Python has a simple rule to determine the scope of a variable. If a variable is assigned in a function, that variable is local. This is because it is assumed that when you define a ...

  11. UnboundLocalError: local variable 'batch_outputs' referenced before

    UnboundLocalError: local variable 'batch_outputs' referenced before assignment #9. Closed RDePlaen opened this issue Sep 8, 2020 · 2 comments ... 1286 return tf_utils.to_numpy_or_python_type(all_outputs) 1287 UnboundLocalError: local variable 'batch_outputs' referenced before assignment ...

  12. UnboundLocalError: local variable 'beta1' referenced before assignment

    Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . The unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has ...

  13. Why do I get UnboundLocalError: local variable 'batch_idx' referenced

    ref: machine learning - Why do I get UnboundLocalError: local variable 'batch_idx' referenced before assignment when using interleaved data sets with Hugging Face (HF)? - Stack Overflow ref: Why do I get UnboundLocalError: local variable 'batch_idx' referenced before assignment when using interleaved data sets with Hugging Face (HF)? ref: Discord

  14. UndboundLocalError: local variable referenced before assignment

    And still are the same error: 1366×768 58.8 KB. wakecarter February 29, 2024, 8:54pm 6. I think you have blank rows in your spreadsheet. The loop claims that there are 19 conditions but I think you only want 12. Without a value for sample_category sample doesn't get set. With random presentation this will happen at a random point.

  15. UnboundLocalError: local variable 'batch_idx' referenced before assignment

    Sorry for the delay in getting back to you, but I tried reproducing this on my end and I think the issue with the minimal example at least is that the LazyBatcher will immediately raise StopIteration if the total length of the triples is less than the specified batch size (which I assume you kept at 32 as per the example code snippet): https ...

  16. UnboundLocalError: local variable 'output' referenced before assignment

    UnboundLocalError: local variable 'output' referenced before assignment. Ask Question Asked 8 years, 5 months ago. Modified 8 years, 5 months ago. Viewed 2k times ... Local variable referenced before assignment but I already assigned it a value. 0. Variable apparently not defined when it is. 2.

  17. UnboundLocalError: local variable 'a' referenced before assignment

    UnboundLocalError: local variable 'a' referenced before assignment The text was updated successfully, but these errors were encountered: 👍 17 callensm, bendesign55, nulladdict, rohanbanerjee, balovbohdan, drewszurko, hellotuitu, rribani, jeromesteve202, egmaziero, and 7 more reacted with thumbs up emoji

  18. python

    1. The variables wins, money and losses were declared outside the scope of the fiftyfifty() function, so you cannot update them from inside the function unless you explicitly declare them as global variables like this: def fiftyfifty(bet): global wins, money, losses. chance = random.randint(0,100)

  19. UnboundLocalError: local variable 'logs' referenced before assignment

    @Tuxius and others which stuck until this bug is fixed: I had the same issue and I found that my validation data set had less samples as the batch_size. Because I'm working with TFRecords-data-sets which have no meta data about how many records, i.e. samples, are in the data set I check now upfront whether the data set contains at least batch_size records (samples).

  20. UnboundLocalError: local variable 'boxprops' referenced before assignment

    I am trying to plot using seaborn boxplot, however I get the UnboundLocalError: local variable 'boxprops' referenced before assignment. These codes were executing fine last week. I have tried updat...