I am getting this error when fitting a Keras model, defined using its functional API (inheriting from tf.keras.Model):
AttributeError: 'Model' object has no attribute '_output_tensor_cache'
How could I fix it? Here below a minimal piece of code that reproduces it, and then the full stack trace of the error. The error happens when calling Model.fit(), even before it gets into the implementation of Model.__call__().
I am using tensorflow-gpu (1.7.0).
import tensorflow as tf
import numpy as np
# Using Keras functional API
class Model(tf.keras.Model):
def __init__(self):
super(Model, self).__init__()
self.inp = tf.keras.layers.Input(shape=(8,))
self.fc1 = tf.keras.layers.Dense(32)
self.fc2 = tf.keras.layers.Dense(10)
def __call__(self, inputs, trainig=False):
y = self.inp(inputs)
y = self.fc1(y)
y = self.fc2(y)
return y
if __name__ == '__main__':
# Just a random dataset, to try out the code
X = np.random.rand(512, 8)
y = np.random.randint(0, 9, size=(512,))
model = Model()
model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer=tf.keras.optimizers.Adadelta(),
metric=['accuracy'])
model.fit(X, y, batch_size=64, epochs=1, verbose=2, validation_split=.2)
Error stack trace:
Traceback (most recent call last):
File "/home/fanta/workspace/wine-quality/minimal.py", line 29, in <module>
model.fit(X, y, batch_size=64, epochs=1, verbose=2, validation_split=.2)
File "/home/fanta/.local/python3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1150, in fit
batch_size=batch_size)
File "/home/fanta/.local/python3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 704, in _standardize_user_data
self._set_inputs(x)
File "/home/fanta/.local/python3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 880, in _set_inputs
self._symbolic_set_inputs(inputs, training=training)
File "/home/fanta/.local/python3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 999, in _symbolic_set_inputs
outputs = self.call(self.inputs[0], training=training)
File "/home/fanta/.local/python3.5/lib/python3.5/site-packages/tensorflow/python/keras/_impl/keras/engine/network.py", line 631, in call
if cache_key in self._output_tensor_cache:
AttributeError: 'Model' object has no attribute '_output_tensor_cache'
Process finished with exit code 1