Please make sure that the boxes below are checked before you submit your issue. Thank you!
I am performing batch learning and after a few batches I get this error from this line of code:
model.fit(Xtrain, Ytrain, batch_size=128, nb_epoch=1,
verbose=1,validation_split=0.01,
callbacks=[ModelCheckpoint(weightStr, monitor='val_loss', verbose=0, save_best_only=True, mode='auto')])
Traceback (most recent call last):
File "<ipython-input-1-0ab90ed05873>", line 321, in <module>
callbacks=[ModelCheckpoint(weightStr, monitor='val_loss', verbose=0, save_best_only=True, mode='auto')])
File "/home/kevin/.local/lib/python2.7/site-packages/keras/models.py", line 620, in fit
sample_weight=sample_weight)
File "/home/kevin/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1104, in fit
callback_metrics=callback_metrics)
File "/home/kevin/.local/lib/python2.7/site-packages/keras/engine/training.py", line 842, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/kevin/.local/lib/python2.7/site-packages/keras/callbacks.py", line 40, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/home/kevin/.local/lib/python2.7/site-packages/keras/callbacks.py", line 196, in on_epoch_end
self.progbar.update(self.seen, self.log_values, force=True)
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
I have no idea why I get this error, It seems to happen randomly, can anyone point me in the right direction?
Here is the module of code that I am running:
for e in range(numEpoch):
numOfImgToLoad = 50000#we can tune this
totalNumberOfImages = len(imagesAndClass)
runningTotal = 0
startingPoint = 0
endingPoint = numOfImgToLoad
while totalNumberOfImages > 0:
print "StartingPoint: {}, endingPoint {}".format(startingPoint, endingPoint)
totalNumberOfImages = totalNumberOfImages - numOfImgToLoad#subtract the number of images loaded into mem
if totalNumberOfImages < 0:
remainder = totalNumberOfImages + numOfImgToLoad
(Xtrain, Ytrain) = loadImages(imagesAndClass[startingPoint:remainder])
Xtrain = np.array(Xtrain).reshape(len(Xtrain), 1, 106, 106)#np.array(Xtrain).reshape(4415, 1, 106, 106)
runningTotal += remainder
else:
(Xtrain, Ytrain) = loadImages(imagesAndClass[startingPoint:endingPoint])
Xtrain = np.array(Xtrain).reshape(len(Xtrain), 1, 106, 106)
runningTotal += numOfImgToLoad
startingPoint = endingPoint+1
endingPoint = startingPoint + numOfImgToLoad - 1
Xtrain /= 255#change pixel value to between 0 and 1
Xtrain = Xtrain.astype('float32')
Ytrain = np_utils.to_categorical(Ytrain, len(classes)+1)
Ytrain = np.array(Ytrain)
print "Starting epoch {}".format(e)
model.fit(Xtrain, Ytrain, batch_size=128, nb_epoch=1,
verbose=1,validation_split=0.01,
callbacks=[ModelCheckpoint(weightStr, monitor='val_loss', verbose=0, save_best_only=True, mode='auto')])
#callbacks=[EarlyStopping(monitor='val_loss', patience=2)])
#print "Starting epoch {} on image {}".format(e, runningTotal)
print "Killing Xtrain and resetting"
del Xtrain
del Ytrain
Can you post a minimal, short, standalone script to reproduce your issue?
Thanks fchollet, I found the bug in my code.
Apparently this line was causing it:
(Xtrain, Ytrain) = loadImages(imagesAndClass[startingPoint:remainder])
It should have been
(Xtrain, Ytrain) = loadImages(imagesAndClass[startingPoint:startingPoint+remainder])
It was trying to return an array with an invalid range something like: 500000-4900
Hi,
I've the same error. I'm following this https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html tutorial for Using pre-trained word embeddings in a Keras model.
File "/home/mrx/src/experpk/exper.pk/API/Product/management/commands/categorize.py", line 130, in handle
model.fit(x_train, y_train, validation_data=(x_val, y_val), nb_epoch=2, batch_size=50, verbose=1)
File "/home/mrx/src/experpk/venv-experpk/local/lib/python2.7/site-packages/keras/engine/training.py", line 1192, in fit
initial_epoch=initial_epoch)
File "/home/mrx/src/experpk/venv-experpk/local/lib/python2.7/site-packages/keras/engine/training.py", line 912, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/mrx/src/experpk/venv-experpk/local/lib/python2.7/site-packages/keras/callbacks.py", line 76, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/home/mrx/src/experpk/venv-experpk/local/lib/python2.7/site-packages/keras/callbacks.py", line 265, in on_epoch_end
self.progbar.update(self.seen, self.log_values, force=True)
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
Respective code is ;
if self.verbose:
# here
self.progbar.update(self.seen, self.log_values, force=True)
Can you please help me.
For method fit() Keras should throw an exception when using validation_split and training set is ended up to be 0... Example: your last batch in epoch contains 1 sample and validation_split = 0.3 => training size = 0...
When training set size is 0 you will see this exception...
If you look at callbacks.py in keras, class ProgbarLogger, log_values member is created ONLY when batch starts... So if you don't have data in the batch it gets to on_epoch_end with log_values undefined...
It seems this is not solved yet. I uploaded Keras yesterday (2/6/2017) and the code is still raising this message: AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
from keras.models import Sequential
from keras.layers import Embedding, GRU, Dense
import numpy as np
def get_model():
model = Sequential()
model.add(Embedding(input_dim=27, output_dim=300))
model.add(GRU(256))
model.add(Dense(27, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
return model
model = get_model()
X = np.zeros((0, 10), dtype=np.int)
y = np.zeros((0, 27), dtype=np.bool)
model.fit(X, y)
Here's a self-contained example that reproduces the error. @fchollet , can we reopen this?
Here's the output I get
~ g$ python3 keras_error.py
Using TensorFlow backend.
Epoch 1/10
Traceback (most recent call last):
File "keras_error.py", line 17, in <module>
model.fit(X, y)
File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 870, in fit
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 1507, in fit
initial_epoch=initial_epoch)
File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 1176, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/usr/local/lib/python3.6/site-packages/keras/callbacks.py", line 77, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/usr/local/lib/python3.6/site-packages/keras/callbacks.py", line 309, in on_epoch_end
self.progbar.update(self.seen, self.log_values, force=True)
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
I'm also getting the same error while training a four layer LSTM on small conversation corpus.epochs = 500
epochs = 500
for i in range(10):
model.fit(vecX, vecY, epochs = epochs, validation_split = 0.2, verbose = 1)
model.save('LSTM' + str((epochs * i + 500)) + '.h5')
this gives out
Train on 0 samples, validate on 1 samples
Epoch 1/500
AttributeError Traceback (most recent call last)
1 epochs = 500
2 for i in range(10):
----> 3 model.fit(vecX, vecY, epochs = epochs, validation_split = 0.2, verbose = 1)
4 model.save('LSTM' + str((epochs * i + 500)) + '.h5')
~/.local/lib/python3.5/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
868 class_weight=class_weight,
869 sample_weight=sample_weight,
--> 870 initial_epoch=initial_epoch)
871
872 def evaluate(self, x, y, batch_size=32, verbose=1,
~/.local/lib/python3.5/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, **kwargs)
1505 val_f=val_f, val_ins=val_ins, shuffle=shuffle,
1506 callback_metrics=callback_metrics,
-> 1507 initial_epoch=initial_epoch)
1508
1509 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):
~/.local/lib/python3.5/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
1174 for l, o in zip(out_labels, val_outs):
1175 epoch_logs['val_' + l] = o
-> 1176 callbacks.on_epoch_end(epoch, epoch_logs)
1177 if callback_model.stop_training:
1178 break
~/.local/lib/python3.5/site-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
75 logs = logs or {}
76 for callback in self.callbacks:
---> 77 callback.on_epoch_end(epoch, logs)
78
79 def on_batch_begin(self, batch, logs=None):
~/.local/lib/python3.5/site-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
307 self.log_values.append((k, logs[k]))
308 if self.verbose:
--> 309 self.progbar.update(self.seen, self.log_values, force=True)
310
311
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
Same error, and I tried removing validation_split from my fit command, but no change.
Have you checked the dimensions for both ur x and y for train and test, I had a dimension issue with my y train. Generally the error arising if u tey to split data of incorrect size , say if it's 1, and u try to split it, it gives this error
Ran into this issue today. saransh-mehta suggestion was the solution. Also ran into an issue where the number of samples for test/train split was not sufficient. train was 0 samples.
If this is a common issue, we need a clear error message to handle it. Please open a PR to add appropriate error handling.
Hi Guys,
I also meet this issue, but it happened that the sample size is small, e.g., I used a sample size 2 and do model.fit it by split ratio 0.2 then get this error. but when I used sample size >1000, then it disappeared.
hope this information may help.
regards, Yifei
I also encountered this error today. Had to set verbose=0
to fix.
Please reopen the issue. It needs to be fixed!
Encountered this error when my training set was an empty array. Using Keras 2.0.9.
A more descriptive error message might be helpful
This happens if steps_per_epoch
is 0. Make sure that your batch size is not greater than the dataset size to avoid it.
I got a similar issue when my training data set was empty after data cleaning and normalization. Might be worth checking you data set size before training.
I've hit this error in Keras 2.1.5 as well.
I also encountered this problem and I do not know how to solve it
maybe this issue occurrence due to less train sample.increase more train sample can solve it
Encountered this in 2.1.6 when supplying empty training set by mistake. IMHO the code should handle this gracefully.
@kevkid @oswaldoludwig
Attached is my gist. https://gist.github.com/Anoopparjanya/d921e3edf1ef9eeb8621ed6138f9557f/revisions
Im getting the same error though i have installed updated versions f keras and Theano mentioned above.
Please help me out,how to overcome this issue?
Set verbose=0:
model.fit(X, Y, verbose=0)
Or make sure your dataset is bigger than the batch size.
将batch_size设置为较小的数字。当您的batch_size设置为比样本集大小更大的值时,会出现此错误。
(using TensorFlow 1.7.0
)
I would like to propose a code change to keras/callbacks.py
. The current error is confusing and provides no clue as to what's going on. I also believe that it wasn't designed to fail, but is just incorrect. IF keras wants to let the user know that they have their training setup wrong, that's ok, but I don't think this is the mechanism to do that.
The log_values
attribute is used on on_epoch_end()
, yet is created in on_batch_begin()
. This seems wrong.
Instead, place the initialization in ProgbarLogger.__init__()
(while still leaving on_batch_begin()
as is).
def __init__(self, count_mode='samples',
stateful_metrics=None):
super(ProgbarLogger, self).__init__()
if count_mode == 'samples':
self.use_steps = False
elif count_mode == 'steps':
self.use_steps = True
else:
raise ValueError('Unknown `count_mode`: ' + str(count_mode))
if stateful_metrics:
self.stateful_metrics = set(stateful_metrics)
else:
self.stateful_metrics = set()
self.log_values = [] # <----- INSERTED HERE
Please, see PR with fix above.
It fix this and some related bugs and contains tests for regressions and related bugs manifestation. Concerns behind change set are described as well.
I also got this error. Because the number of training samples is smaller than 'batch_size'.
Hope this message can help.
Same error with my execution:
from __future__ import print_function
import keras
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
batch_size = 32
num_classes = 10
epochs = 10
num_predictions = 20
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32), padding='same', data_format="channels_first"))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3), data_format="channels_first"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3), data_format="channels_first"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
opt = keras.optimizers.RMSprop(learning_rate=0.0001, decay=1e-6)
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'data',
target_size=(150,150),
batch_size=32,
class_mode='binary')
model.fit_generator(train_generator,
verbose=2,
epochs=epochs)
Traceback (most recent call last):
File "sturdy_couscous_cnn.py", line 62, in
epochs=epochs)
File "C:\Users\WINDOWS\Anaconda3envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\keras\legacy\interfaces.py", line 91, in wrapper
return func(args, *kwargs)
File "C:\Users\WINDOWS\Anaconda3envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\kerasengine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\WINDOWS\Anaconda3envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\kerasengine\training_generator.py", line 260, in fit_generator
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\WINDOWS\Anaconda3envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\keras\callbacks\callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "C:\Users\WINDOWS\Anaconda3envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\keras\callbacks\callbacks.py", line 611, in on_epoch_end
self.progbar.update(self.seen, self.log_values)
AttributeError: 'ProgbarLogger' object has no attribute 'log_values'
I had the same error for the same code that was running efficiently a day before, I only changed the data -I increased it though-
most of the solutions say that it's for the size of data or that the batch size might be greater than the train sample but I've got about 30k images with a split of 75-25 between train and validation, I tried to set (verbose=0) but it showed me another error: (list index out of range)
My guess is that the issue is not in how big is your training set, but rather the last batch size. Example: if the last batch has 3 samples, that gives you 2 samples for training set and 0 for validation (casted to integer). This is when you would see this exception. So, increase the last batch to the size where validation samples # will be > 0. Really surprised that the issue is not fixed for such a long time...
I thought of this too, but actually it seemed that the problem was more trivial than so.
Thanks for your help.
For my problem, I was calling the name of the images differently, so when it was supposed to take these images and split them it couldn't find them for the different naming I called it with, so it wasn't a problem in the model rather a step before it.
I used a fixed step_per_epoch and the issues was resolved. You can play around number like 100 etc. Thanks
Most helpful comment
This happens if
steps_per_epoch
is 0. Make sure that your batch size is not greater than the dataset size to avoid it.