Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'ProgbarLogger' object has no attribute 'log_values' #3657

Closed
3 tasks done
kevkid opened this issue Sep 1, 2016 · 31 comments
Closed
3 tasks done

AttributeError: 'ProgbarLogger' object has no attribute 'log_values' #3657

kevkid opened this issue Sep 1, 2016 · 31 comments

Comments

@kevkid
Copy link

kevkid commented Sep 1, 2016

Please make sure that the boxes below are checked before you submit your issue. Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

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
@fchollet
Copy link
Member

fchollet commented Sep 1, 2016

Can you post a minimal, short, standalone script to reproduce your issue?

@kevkid
Copy link
Author

kevkid commented Sep 2, 2016

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

@kevkid kevkid closed this as completed Sep 2, 2016
@SaaShah
Copy link

SaaShah commented Feb 8, 2017

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.

@vladbph
Copy link

vladbph commented May 6, 2017

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...

@oswaldoludwig
Copy link
Contributor

oswaldoludwig commented Jun 3, 2017

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'

@sixhobbits
Copy link

sixhobbits commented Jun 18, 2017

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'

@saransh-mehta
Copy link

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)
in ()
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'

@ss32
Copy link

ss32 commented Jul 9, 2017

Same error, and I tried removing validation_split from my fit command, but no change.

@saransh-mehta
Copy link

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

@JosephMRally
Copy link

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.

@fchollet
Copy link
Member

If this is a common issue, we need a clear error message to handle it. Please open a PR to add appropriate error handling.

@easyfly007
Copy link

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

@khalman-m
Copy link

I also encountered this error today. Had to set verbose=0 to fix.
Please reopen the issue. It needs to be fixed!

@akaszynski
Copy link

akaszynski commented Nov 5, 2017

Encountered this error when my training set was an empty array. Using Keras 2.0.9.

A more descriptive error message might be helpful

@agostbiro
Copy link

This happens if steps_per_epoch is 0. Make sure that your batch size is not greater than the dataset size to avoid it.

@Ather23
Copy link

Ather23 commented Apr 7, 2018

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.

@msollami
Copy link

I've hit this error in Keras 2.1.5 as well.

@1179548504
Copy link

I also encountered this problem and I do not know how to solve it

@zkeenly
Copy link

zkeenly commented May 26, 2018

maybe this issue occurrence due to less train sample.increase more train sample can solve it

@bzamecnik
Copy link
Contributor

Encountered this in 2.1.6 when supplying empty training set by mistake. IMHO the code should handle this gracefully.

@Anoopparjanya
Copy link

Anoopparjanya commented Jun 19, 2018

@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?

@ebuildy
Copy link

ebuildy commented Jun 19, 2018

Set verbose=0:

model.fit(X, Y, verbose=0)

Or make sure your dataset is bigger than the batch size.

@ZhangXiaoying0116
Copy link

将batch_size设置为较小的数字。当您的batch_size设置为比样本集大小更大的值时,会出现此错误。

@robertlugg
Copy link

(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

mrjj added a commit to mrjj/keras that referenced this issue May 31, 2019
…arget'" (or 'log_values') error (keras-team#12898) (keras-team#12893) (keras-team#8944)

This Change set is fixing two missing attribute bugs:
* AttributeError: 'ProgbarLogger' object has no attribute 'target' keras-team#12898
  Abstract reproduction scenario is provided in ticket
* AttributeError: 'ProgbarLogger' object has no attribute 'log_values' keras-team#3657
* AttributeError: 'ProgbarLogger' object has no attribute 'log_values' keras-team#8944 (dup)

Related changes:
* Cases of regression are covered by tests.
* Some potential bugs with same nature are prevented and covered by manifestation checks.
* run with empty data array (but having valid shape) is now handled properly
  and yielding related warnings on callback and training routine level without execution fail

Note:
Changes that affect `ProgbarLogger` should be aware of following things:
* proper target initialisation is requiring two attributes: `params` and `use_steps` to be defined
* `use_steps` is guaranteed attribute the is set in the constructor (but could be altered after object instantiation. It's currently safe condition.
* class `params` attribute could be altered between initialisation and training start. And current logic is made to be aware of this
* we don't have `params` initialisation in constructor, this attribute will be assigned on call of `set_params` of base class somewhere on caller level (no strict guarantees :( )
* `seen` attribute is working in pair with `target` during `log_values` initialisation and their initialisation should be under the equal condition, currently thats not true
* `if self.seen < self.target` condition is being checked whenever verbose mode value so both of them should be initialised without any conditions
* `if self.seen < self.target` is checking for training iteration being not finished but in case of degenerate case with zero length it will not be called and `log_values` will stay not initialised but i don't see any explicit logic preventing using it on exit from 0-length training cycle and potentially it is the bug of some kind that is prevented on caller logic level
* `progbar` attribute initialisation is definitely related to output verbosity (log values accumulation are not) and should be left under verbosity condition
@mrjj
Copy link

mrjj commented May 31, 2019

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.

@pingaowang
Copy link

I also got this error. Because the number of training samples is smaller than 'batch_size'.
Hope this message can help.

@ktfth
Copy link

ktfth commented Oct 21, 2019

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\Anaconda3\envs\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\Anaconda3\envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\keras\engine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\WINDOWS\Anaconda3\envs\tf\lib\site-packages\keras-2.3.0-py3.6.egg\keras\engine\training_generator.py", line 260, in fit_generator
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\WINDOWS\Anaconda3\envs\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\Anaconda3\envs\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'

@hosnaa
Copy link

hosnaa commented Feb 22, 2020

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)

@vladbph
Copy link

vladbph commented Feb 22, 2020

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...

@hosnaa
Copy link

hosnaa commented Feb 22, 2020

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.

@akashsngg99
Copy link

JUST INCREASE NO_OF_EPOCHS
IT WILL WORK FOR ME..

@demolakstate
Copy link

I used a fixed step_per_epoch and the issues was resolved. You can play around number like 100 etc. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests