Reputation: 2605
I'm using Tensorflow 1.14 and Python 3.5. I got the following error:
UnboundLocalError: local variable 'batch_index' referenced before assignment
The full trace back is:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<timed exec> in <module>
/usr/local/lib/python3.5/dist-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)
1237 steps_per_epoch=steps_per_epoch,
1238 validation_steps=validation_steps,
-> 1239 validation_freq=validation_freq)
1240
1241 def evaluate(self,
/usr/local/lib/python3.5/dist-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, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
203 break
204
--> 205 if batch_index == len(batches) - 1: # Last batch.
206 if do_validation and should_run_validation(validation_freq, epoch):
207 val_outs = test_loop(model, val_function, val_inputs,
UnboundLocalError: local variable 'batch_index' referenced before assignment
And after trying multiple suggestions from different SO answers, I managed to fix it by switching from these import statements:
from keras.layers import LSTM, Dense
from keras.models import Sequential
To these import statements:
from tensorflow.python.keras.layers import LSTM, Dense
from tensorflow.python.keras.models import Sequential
This did resolve my issue, but I am puzzled: How are the two any different?
Are tf.keras
and keras
using different methods and classes?
Upvotes: 1
Views: 9748
Reputation: 897
Difference between tf.keras and keras.
Keras: Is a high level neural network API for training neural networks. It's independent of tensorflow
and can run on top of multiple backends such as tensorflow, Theano and CNTK
. Documentation here
tf.keras: tf.keras
is a specific high level implementation of the keras
API in tensorflow with added support for certain tensorflow
features.
Upvotes: 3