bstriner / keras-tqdm

Keras integration with TQDM progress bars
MIT License
348 stars 41 forks source link

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

Open Braintelligence opened 5 years ago

Braintelligence commented 5 years ago

I get

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

when trying to use keras-tqdm with LSTM.

omer54321 commented 5 years ago

Getting that too. Any luck?

Braintelligence commented 5 years ago

Nope, no luck. Sorry, misclicked.

omer54321 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Thanks, very kind of you to share!

omer54321 commented 5 years ago

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 commented 5 years ago

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

gsportelli commented 5 years ago

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 commented 5 years ago

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

ipazc commented 5 years ago

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 commented 3 years ago

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