Keras functional API doesn't create any layers on google colab (model.summary() prints no layer)

923 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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 or import keras in your entire code.

For example if I code like this (use both library randomly):

import keras                                #import keras
import tensorflow as tf
from tensorflow.keras.layers import Dense   #import layers from tensorflow.keras
from tensorflow.keras import Input

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')
model.summary()

The output will be:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 1,271
Trainable params: 1,271
Non-trainable params: 0
_________________________________________________________________

But if I modify imports and use just tensorflow.keras and not use keras like this:

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Input
from tensorflow.keras.models import Model

input = Input(shape = (20,))
x = Dense(30, name = 'dense1')(input)
x = Dense(20, name = 'dense2')(x)
output = Dense(1)(x)
model = Model(inputs = input ,outputs = output)
model.compile(loss = 'mse', optimizer = 'adam')
model.summary()

I will get the output like this:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 20)]              0         
_________________________________________________________________
dense1 (Dense)               (None, 30)                630       
_________________________________________________________________
dense2 (Dense)               (None, 20)                620       
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 21        
=================================================================
Total params: 1,271
Trainable params: 1,271
Non-trainable params: 0
_________________________________________________________________