Problem Summarization
I created keras model using functional API without any error.
But when I call the model.summary(), there is not any layers but only parameters exists.
When I call the model.layers, empty list is returned.
For example, I created simple mlp model.
input = Input(shape = (20,))
x = Dense(30, name = 'dense1')(input)
x = Dense(20, name = 'dense2')(x)
output = Dense(1)(x)
model = keras.models.Model(inputs = input ,outputs = output)
model.compile(loss = 'mse', optimizer = 'adam')
code above is executed without any error. but when I call summary method or layers method, not any layers come up.
model.layers
-> []
model.summary()
-> Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Total params: 1,271
Trainable params: 1,271
Non-trainable params: 0
_________________________________________________________________
but this model is able to be trained and inference despite not having any layers. And much more weird thing is that This happens only on Google colab pro. In the kaggle kernel, the same code creates all layers without any problem.
To make more complicated model with functional API, layers have to be exist. Is there anybody knows the reason why this happens on google colab?
You are probably mixing keras and tensorflow libraries. Since Tensorflow implemented keras libraries, this a common mistake between developers that import keras and tensorflow and use both of them randomly and this leads to some weird behavior.
Just use either
import tensorflow.keras
orimport keras
in your entire code.For example if I code like this (use both library randomly):
The output will be:
But if I modify imports and use just
tensorflow.keras
and not usekeras
like this:I will get the output like this: