Keras: UnboundLocalError: local variable 'logs' referenced before assignment

Viewed 16216

I am relatively new to python, and while attempting to train a chatbot I received the error: ‘UnboundLocalError: local variable 'logs' referenced before assignment‘. I used model.fit to train:

model.fit(x_train, y_train, epochs=7)

And I received the error:

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-10-847c83704a3f> in <module>()
      2           x_train,
      3           y_train,
----> 4           epochs=7
      5           )

1 frames
/usr/local/lib/python3.6/dist-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 `run_distribute_coordinator` already.

/usr/local/lib/python3.6/dist-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)
    854               logs = tmp_logs  # No error, now safe to assign to logs.
    855               callbacks.on_train_batch_end(step, logs)
--> 856         epoch_logs = copy.copy(logs)
    857 
    858         # Run validation.

UnboundLocalError: local variable 'logs' referenced before assignment 

I ran this in google colab, with the link here: https://colab.research.google.com/drive/18uTvvKYDrd8CQi31kg6vX2Dbxg1gD20X?usp=sharing

I used the chatterbot/english dataset on kaggle: https://www.kaggle.com/kausr25/chatterbotenglish

6 Answers

This issue looks similar to the problem I had while working with small datasets and it is covered in this thread: #38064. I solved my particular issue setting a smaller batch_size, in my case:

batch_size = 2

This issue was seen when my dataset was not properly loaded or when the dataset file was missing. It implies there was not even one record available for the code to process.

I guess it may also occur when the available dataset is small too.

May be a small thing to see, check if your validation data is not empty. It can be, somehow.

If you are using tf.data.Dataset as input, you should notice that tf.data.Dataset.take(num) takes num BATCHES, instead of single items, from the original dataset.

For example, if you have 30000 items in the dataset, and apply tf.data.Dataset.batch(256) on the dataset, you will have 30000 / 256 = 118 batches in the dataset. Here if you use tf.data.Dataset.take(2000) to build your validating set, you will have all the 118 batches inside the validating set. After that, if you try tf.data.Dataset.skip(2000) to get the training set, there will be nothing inside of it.

Hence, since there is nothing inside the training set, you will obviously fail when you try fitting on it.

To avoid this problem, you should always pass a num which is less than the total number of batches in your dataset. In the case above, you could try:

validating_set = complete_dataset.take(18)
training_set   = complete_dataset.skip(18)
# Notice that 30000 / 256 = 118 = 18 + 100

Try setting steps_per_epoch while fitting your model.
i.e., model.fit(x_train, y_train, steps_per_epoch= 5, epochs=7)

if you using K-Fold validation try divide k your samples. k = fold num

Related