You tried to call count_params on ..., but the layer isn't built. TensorFlow 2.0

Viewed 3861

I receive the following error in in Pyhotn 3 and TF 2.0.

"ValueError: You tried to call count_params on digits, but the layer isn't built. You can build it manually via: digits.build(batch_input_shape)." at line new_model.summary().

what is the problem and how to solve it?

inputs = keras.Input(shape=(784,), name='digits')
x = layers.Dense(64, activation='relu', name='dense_1')(inputs)
x = layers.Dense(64, activation='relu', name='dense_2')(x)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)

model = keras.Model(inputs=inputs, outputs=outputs, name='3_layer_mlp')
model.summary()

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(60000, 784).astype('float32') / 255
x_test = x_test.reshape(10000, 784).astype('float32') / 255

model.compile(loss='sparse_categorical_crossentropy',
              optimizer=keras.optimizers.RMSprop(),
              metrics=['accuracy'])
history = model.fit(x_train, y_train,
                    batch_size=64,
                    epochs=2)

model.save('saved_model', save_format='tf')
new_model = keras.models.load_model('saved_model')
new_model.summary()
4 Answers

For 2.0 version Model can be saved in .h5 format, please use model.save('my_model.h5') while saving.

Please find the link of working gist.

Also issue seems to be resolved in the Latest TF-nightly version,as going forward 2.1 will be official version try using pip install tf-nightly

Find the link of working gist here.

I met the same error, and it solved after upgraded tf2.0 to tf2.1.

I had the same problem, I was using tensorflow==2.0.0. I tried running the same code using tensorflows nightly build (in my case pip install tf-nightly==2.1.0.dev20191003).

It worked on the nightly build but you may have to save the model again using the nightly build.

you can use the model properly if there is not the last line of you codes, that is to say you just can not use summary here.

Related