Keras: TypeError: predict() got an unexpected keyword argument 'callbacks'

Created on 9 May 2019  路  5Comments  路  Source: keras-team/keras

TypeError: predict() got an unexpected keyword argument 'callbacks'

I am using Keras 2.2.4 version with tensorboard 1.10.0 and tensorflow 1.10.0 in Python 3,6 version. But I can't use callbacks on predict method.

I have one saved model, so first I loaded the model and then trying to predict the test data and display it in tensor board. It's throwing an error, but as per keras documents predict function (reference: https://keras.io/models/model/) looks like:

predict(x, batch_size=None, verbose=0, steps=None, callbacks=None)

Code:
import keras.callbacks
from keras.models import load_model

model = load_model(strPath_model)
tb_test = keras.callbacks.TensorBoard(log_dir=strPath_model_test_logs,histogram_freq=0, write_graph=True, write_images=True)

y_test = model.predict(test_val_X, verbose=1, callbacks=[tb_test])

Error:
TypeError: predict() got an unexpected keyword argument 'callbacks'

tensorflow awaiting response support

Most helpful comment

@roys393 model.predict doesn't have a argument callbacks. Only you can provide callbacks handle to model.fit. Please check official keras doc on callbacks and another example here. If you have further questions, please make a standalone code to reproduce your issue so that we can resolve the issue faster. You could use public data such as mnist data if your data is private. Thanks!

If you are correct that Callbacks is only available on Fit, then the Issue is in the documentation:
Sequential and Model(Functional API) say:

predict(x, batch_size=None, verbose=0, steps=None, callbacks=None)
...
callbacks: List of keras.callbacks.Callback instances. List of callbacks to apply during prediction. See callbacks.

Also, looking at the code it appears to have that param:

def predict(self, x,
                batch_size=None,
                verbose=0,
                steps=None,
                callbacks=None,
                max_queue_size=10,
                workers=1,
                use_multiprocessing=False):

I was able to easily reproduce the issue, getting the exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-49e5fe4e0652> in <module>()
      5 
      6 # predict, even without training this should not error out
----> 7 result = model.predict(test_val_X, verbose=1, callbacks=[call_back_single])

TypeError: predict() got an unexpected keyword argument 'callbacks'

Here is a colab that reproduces

In case I didn't share that corretly, here is the code:

import tensorflow as tf
import keras
import numpy as np

model = keras.models.Sequential()
model.add(keras.layers.Dense(10, input_shape=(10,10,1)))
model.add(keras.layers.Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

call_back_single = keras.callbacks.TerminateOnNaN()
test_val_X = np.zeros((1,10,10,1))
# predict should not error out even without training and with zeros for input
result = model.predict(test_val_X, verbose=1, callbacks=[call_back_single])

All 5 comments

@roys393 Could you provide a code to reproduce the issue? Thanks!

import keras.callbacks
from keras.models import load_model

model = load_model(strPath_model)
tb_test = keras.callbacks.TensorBoard(log_dir=strPath_model_test_logs,histogram_freq=0, write_graph=True, write_images=True)

y_test = model.predict(test_val_X, verbose=1, callbacks=[tb_test])

This is the basic code I did to load a model and predict the test data.

Please let me know if you want anything more

@roys393 model.predict doesn't have a argument callbacks. Only you can provide callbacks handle to model.fit. Please check official keras doc on callbacks and another example here. If you have further questions, please make a standalone code to reproduce your issue so that we can resolve the issue faster. You could use public data such as mnist data if your data is private. Thanks!

@roys393 model.predict doesn't have a argument callbacks. Only you can provide callbacks handle to model.fit. Please check official keras doc on callbacks and another example here. If you have further questions, please make a standalone code to reproduce your issue so that we can resolve the issue faster. You could use public data such as mnist data if your data is private. Thanks!

If you are correct that Callbacks is only available on Fit, then the Issue is in the documentation:
Sequential and Model(Functional API) say:

predict(x, batch_size=None, verbose=0, steps=None, callbacks=None)
...
callbacks: List of keras.callbacks.Callback instances. List of callbacks to apply during prediction. See callbacks.

Also, looking at the code it appears to have that param:

def predict(self, x,
                batch_size=None,
                verbose=0,
                steps=None,
                callbacks=None,
                max_queue_size=10,
                workers=1,
                use_multiprocessing=False):

I was able to easily reproduce the issue, getting the exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-49e5fe4e0652> in <module>()
      5 
      6 # predict, even without training this should not error out
----> 7 result = model.predict(test_val_X, verbose=1, callbacks=[call_back_single])

TypeError: predict() got an unexpected keyword argument 'callbacks'

Here is a colab that reproduces

In case I didn't share that corretly, here is the code:

import tensorflow as tf
import keras
import numpy as np

model = keras.models.Sequential()
model.add(keras.layers.Dense(10, input_shape=(10,10,1)))
model.add(keras.layers.Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

call_back_single = keras.callbacks.TerminateOnNaN()
test_val_X = np.zeros((1,10,10,1))
# predict should not error out even without training and with zeros for input
result = model.predict(test_val_X, verbose=1, callbacks=[call_back_single])

The callback parameter of predict() was added by #11808, and it's included in the latest version 2.2.5 released just 6 days ago, but not in 2.2.4.

Was this page helpful?
0 / 5 - 0 ratings