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: 'TQDMCallback' object has no attribute 'on_train_batch_begin' #31

Open
Braintelligence opened this issue Feb 26, 2019 · 11 comments

Comments

@Braintelligence
Copy link

I get

AttributeError: 'TQDMCallback' object has no attribute 'on_train_batch_begin'

when trying to use keras-tqdm with LSTM.

@omer54321
Copy link

Getting that too. Any luck?

@Braintelligence
Copy link
Author

Nope, no luck.
Sorry, misclicked.

@omer54321
Copy link

It seems that the library is just outdated. Even when I tried to fix it, it doesn't seem to work. I will look for an updated solution for the problem that got me here.

@omer54321
Copy link

Solved-ish by creating a progress bar callback, tho I'd love to get an actual fix..
Here's my code if you're still looking:

import time
import sys
import tensorflow as tf
import math

class progBarCallback(tf.keras.callbacks.Callback):
    def setEpochSize(self, es):
        self.epoch_size = es
    def on_epoch_begin(self, epoch, logs={}):
        self.done = 0
        self.cur_epoch = epoch
        printProgBar(epoch, 0, self.epoch_size)
    def on_batch_end(self, batch, logs={}):
        self.done += self.params["batch_size"]
        printProgBar(self.cur_epoch, self.done, self.epoch_size)

def printProgBar(batch, amount, biggest, args=[]):
    fraction = amount / biggest
    num = math.floor(fraction * 50)
    print("\rEpoch " + str(batch + 1) + " [", end="", flush=True)
    for i in range(num - 1):
        print("=", end="")
    if(num > 0):
        print(">", end="")
    for i in range(50 - num):
        print("_", end="")
    print("]", end="")
    for arg in args:
        print(" " + str(arg), end="")
    print(" " + str(amount) + "/" + str(biggest), end="")

Example Usage:

progBar = progbar.progBarCallback()
progBar.setEpochSize(len(x_train))
model.fit(x_train, y_train,
          epochs=1,
          batch_size=1000,
          verbose=0,
          callbacks=[progBar])

Unfortunately, it seems that Keras removed the callback argument from the evaluate function, so this works only for training.

@Braintelligence
Copy link
Author

Thanks, very kind of you to share!

@omer54321
Copy link

Dude, I dunno if you're still into that, but the solution is very stupid. Keras decides if it allows updating the progress bar by checking a function named sys.stdout.isatty() which is false in PyCharm (I hope you are using PyCharm, that's how I got the error), so all I had to do is to go into \Lib\site-packages\keras\utils and add or True to the if statement. (Line 355 for me, if self._dynamic_display or True.

@Braintelligence
Copy link
Author

No, I'm actually using Jupyter Notebook. Maybe this could help. Thanks a lot!

@gsportelli
Copy link

This is a simple workaround to use until fixed:

from keras_tqdm import TQDMNotebookCallback

# keras, model definition...
cb = TQDMNotebookCallback()
setattr(cb,'on_train_batch_begin',lambda x,y:None)
setattr(cb,'on_train_batch_end',lambda x,y:None)

model.fit(X_train, Y_train, verbose=0, callbacks=[cb])

@xingyi-li
Copy link

I also went through the same problem, @gsportelli 's method can temporarily help, thanks!

@ipazc
Copy link

ipazc commented Jul 30, 2019

This is a simple workaround to use until fixed:

from keras_tqdm import TQDMNotebookCallback

# keras, model definition...
cb = TQDMNotebookCallback()
setattr(cb,'on_train_batch_begin',lambda x,y:None)
setattr(cb,'on_train_batch_end',lambda x,y:None)

model.fit(X_train, Y_train, verbose=0, callbacks=[cb])

This temporal fix does not update the widget for me. A workaround is:

from keras_tqdm import TQDMNotebookCallback
 
cb = TQDMNotebookCallback()
cb.on_train_batch_begin = cb.on_batch_begin
cb.on_train_batch_end = cb.on_batch_end

model.fit(X_train, Y_train, verbose=0, callbacks=[cb])

@espears1
Copy link

espears1 commented Apr 2, 2021

Note that for more modern versions of Tensorflow, you may be forced to patch more attributes. I needed to do this, when using Tensorflow 1.14 with tf.keras having version 2.2.4.

    tqdm_cb = TQDMCallback()
    tqdm_cb.on_train_batch_begin = tqdm_cb.on_batch_begin
    tqdm_cb.on_train_batch_end = tqdm_cb.on_batch_end
    tqdm_cb.on_test_begin = lambda y: None
    tqdm_cb.on_test_end = lambda y: None
    tqdm_cb.on_test_batch_begin = lambda x, y: None
    tqdm_cb.on_test_batch_end = lambda x, y: None

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

6 participants