UnboundLocalError: local variable 'batch_index' referenced before assignment

后端 未结 4 1072
清歌不尽
清歌不尽 2020-12-11 10:06

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 i

相关标签:
4条回答
  • 2020-12-11 10:38

    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?

    0 讨论(0)
  • 2020-12-11 10:41
    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..

    0 讨论(0)
  • 2020-12-11 10:42

    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.

    0 讨论(0)
  • 2020-12-11 10:47

    This error is because there is empty training data. whether you import from keras directly or from tensorflow there will be error if you not pass proper data, error message might be different as per import or version. Also make sure your are passing couple of records in data. If you import Keras from tensorflow and use the the error will be

    "raise ValueError('Empty training data.') ValueError: Empty training data."

    If directly then the message will be the error message given in question.

    0 讨论(0)
提交回复
热议问题