tensorboardcolab icon indicating copy to clipboard operation
tensorboardcolab copied to clipboard

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

Open francisco-fernandes opened this issue 6 years ago • 4 comments

Hello! It seems that the latest 1.x version of Keras+Tensorflow requires an on_train_batch_begin function definition that is missing...

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-f2f738e2066b> in <module>()
      2 with session.as_default(), graph.as_default() :
      3   model.set_weights(weights)
----> 4   result = model.fit(X_train, y_train, batch_size=32, epochs=100, verbose=0, shuffle=False, validation_data=(X_test, y_test), callbacks=[PrintDots(),TensorBoardColabCallback(tbc)])
      5 end_time = time.perf_counter()
      6 print( "time = " + str(end_time - start_time) + "s" )

2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _call_batch_hook(self, mode, hook, batch, logs)
    194     t_before_callbacks = time.time()
    195     for callback in self.callbacks:
--> 196       batch_hook = getattr(callback, hook_name)
    197       batch_hook(batch, logs)
    198     self._delta_ts[hook_name].append(time.time() - t_before_callbacks)

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

These are the versions of Tensorflow, Keras and tensorboardcolab that I'm using, respectively, which already come pre-installed in Google Colab:

1.13.1
2.2.4-tf
Requirement already up-to-date: tensorboardcolab in /usr/local/lib/python3.6/dist-packages (0.0.22)

Any workaround for this issue? Thanks!

francisco-fernandes avatar May 01 '19 14:05 francisco-fernandes

same issue here while running the mnist keras tutorial

smatterchoo avatar May 04 '19 07:05 smatterchoo

If you can switch to tensorflow 2.0, there's now an official implementation: click

It works well, especially if you want to embed tensorboard into colab directly. Although I have to say it is a bit bothersome, I like the possibility of using a separate tab.

If not, maybe you can try to create a wrapper object of the likes:

tbc = ...
class custom_callback(TensorBoardColabCallback):
  def __init__(self, *args, **kwargs):
    super().__init__(self, *args, **kwargs)

  def on_train_batch_begin(self, *args, **kwargs):
    pass

...
model.fit(..., callbacks=[custom_callback(tbc)])

r-or avatar May 08 '19 11:05 r-or

@r-or This request comes from my lack of Python object oriented knowledge but would you be able to explain what should be going into the ellipsis for tbc? Alternatively, I more complete example would be appreciated :)

ptrk8 avatar Jul 20 '19 08:07 ptrk8

This fixed the issue for me:

class PlotLossesCallback(livelossplot.keras.PlotLossesCallback):
    def on_train_batch_begin(self, a, b): pass
    def on_train_batch_end(self, a, b): pass

AndreaRigoni avatar Aug 30 '19 09:08 AndreaRigoni