Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnboundLocalError: local variable 'batch_index' referenced before assignment

This is NOT MY code by here is the line, where it shows a problem:

model.fit(trainX, trainY, batch_size=2, epochs=200, verbose=2)

(As I am thinking now, it is very possible this code uses an older version of TF, because 'epochs' was written as 'nb_epoch').

The last update of the code is from: Jan 11, 2017!

I have tried everything from the internet (which is not so much), including looking inside the source code of tensorflow/keras for some hints. Just to make it clear that I don't have a variable, called 'batch_index' inside the code.

So far I have looked inside different versions of TF (tensorflow/tensorflow/python/keras/engine/training_arrays.py). It appears that all are from 2018 copyright, but some start with the function fit_loop, and other with model_iteration (which is probably an update of fit_loop).

So, this 'batch_index' variable can be seen only in the first function.

I wonder if I am going in the right direction at all??!

There is no point in showing the code, because, as I explained, there is no such variable in the first place inside the code.

but, here is some code of the function 'stock_prediction', that gives the error:


def stock_prediction():

    # Collect data points from csv
    dataset = []

    with open(FILE_NAME) as f:
        for n, line in enumerate(f):
            if n != 0:
                dataset.append(float(line.split(',')[1]))

    dataset = np.array(dataset)

    # Create dataset matrix (X=t and Y=t+1)
    def create_dataset(dataset):
        dataX = [dataset[n+1] for n in range(len(dataset)-2)]
        return np.array(dataX), dataset[2:]
        
    trainX, trainY = create_dataset(dataset)

    # Create and fit Multilinear Perceptron model
    model = Sequential()
    model.add(Dense(8, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(trainX, trainY, nb_epoch=200, batch_size=2, verbose=2)

    # Our prediction for tomorrow
    prediction = model.predict(np.array([dataset[0]]))
    result = 'The price will move from %s to %s' % (dataset[0], prediction[0][0])

    return result


---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-19-3dde95909d6e> in <module>
     14 
     15 # We have our file so we create the neural net and get the prediction
---> 16 print(stock_prediction())
     17 
     18 # We are done so we delete the csv file

<ipython-input-18-8bbf4f61c738> in stock_prediction()
     23     model.add(Dense(1))
     24     model.compile(loss='mean_squared_error', optimizer='adam')
---> 25     model.fit(trainX, trainY, batch_size=1, epochs=200, verbose=2)
     26 
     27     # Our prediction for tomorrow

~\Anaconda3\lib\site-packages\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_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1176                                         steps_per_epoch=steps_per_epoch,
   1177                                         validation_steps=validation_steps,
-> 1178                                         validation_freq=validation_freq)
   1179 
   1180     def evaluate(self,

~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    211                     break
    212 
--> 213             if batch_index == len(batches) - 1:  # Last batch.
    214                 if do_validation and should_run_validation(validation_freq, epoch):
    215                     val_outs = test_loop(model, val_function, val_inputs,

UnboundLocalError: local variable 'batch_index' referenced before assignment

A little clarification:

I tried to see my version of tf/keras and here is it:

from tensorflow.python import keras
print(keras.__version__)
import keras
print(keras.__version__)
import tensorflow
print(tensorflow.__version__)

2.2.4-tf

2.2.5

1.14.0

Why keras shows different versions??

like image 285
Marko Kolaksazov Avatar asked Sep 14 '19 13:09

Marko Kolaksazov


People also ask

How do you fix UnboundLocalError local variable referenced before assignment?

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 .

How do you fix UnboundLocalError?

UnboundLocalError can be solved by changing the scope of the variable which is complaining. You need to explicitly declare the variable global. Variable x's scope in function printx is global. You can verify the same by printing the value of x in terminal and it will be 6.

What does UnboundLocalError mean?

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.


3 Answers

I checked in the training_arrays.py (here) the function in which you got the error and, as I can see, I think the problem could be in these statements (from lines 177 - 205):

batches = make_batches(num_train_samples, batch_size)
for batch_index, (batch_start, batch_end) in enumerate(batches): # the problem is here
    # do stuff
    ...
if batch_index == len(batches) - 1:
    # do stuff
    ...

If batches is an empty list, you could get this error. Could be that your training set has some problem?

like image 125
Nikaido Avatar answered Oct 15 '22 10:10

Nikaido


UnboundLocalError: local variable 'batch_index' referenced before assignment

The reason for the problem is that the list of batches is empty! batches ==[]

The reason it is blank is because the number of samples for training data is too small to be divided by batch_size

You should check your data, number of samples or You should reduce batch_size to a point that allows you to divide the number of samples by batch size with a real result..

like image 39
Mohamed Emad Avatar answered Oct 15 '22 11:10

Mohamed Emad


The problem was resolved!

I had to import the correct libraries (Tensorflow and not Keras directly):

instead of importing directly Keras:

from tensorflow.python import keras.models.Sequential
from tensorflow.python import keras.layers.Dense

importing of only tensorflow works:

from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Sequential

Apparently this is related to the different version issue of Keras.

like image 34
Marko Kolaksazov Avatar answered Oct 15 '22 11:10

Marko Kolaksazov