Simon GIS
Simon GIS

Reputation: 1055

`steps_per_epoch=None` is only valid for a generator based on the `keras.utils.Sequence`

I am running this code in tensorflow: https://github.com/empathy87/nn-grocery-shelves/blob/master/Step%202%20-%20Brands%20Recognition%20with%20CNN.ipynb

batch_size = 50
epochs = 15
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
                    validation_data=(x_validation, y_validation),
                    epochs=epochs, verbose=1, workers=4, 
                    callbacks=[LearningRateScheduler(lr_schedule)])

ValueError: `steps_per_epoch=None` is only valid for a generator based on the `keras.utils.Sequence` class. Please specify `steps_per_epoch` or use the `keras.utils.Sequence` class.

How I can fix this issue? I've tried to reinstall Tensosflow in Pip install tensorflow and conda install tensorflow.

Upvotes: 2

Views: 4061

Answers (1)

gmds
gmds

Reputation: 19885

The reason is that the object created by datagen.flow doesn't know its size, and accordingly you should specify how many times it is expected to yield values, which you can calculate in conjunction with a batch size.

Say you have 100 training points and want to work with a batch size of 30. Then, you would have 4 steps per epoch, calculated in the following way:

from math import ceil

n_points = len(X_train)
batch_size = 30

steps_per_epoch = ceil(n_points / batch_size)

Upvotes: 5

Related Questions