FrederickDeny
FrederickDeny

Reputation: 21

Keras Model output shape is "(None,)"

My model includes a previously loaded model, and gives an output shape of "(None,)":

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Activation, Dense, Input, Subtract, Multiply, Lambda

x = Input((158,))
y = model(x)
c = Subtract()([x,y])
c = Multiply()([c,c])
d = Lambda(lambda arg: tf.keras.backend.mean(arg,axis=1), output_shape = (None,1))
e = d(c)

new_model = Model(inputs = x, outputs = e)
new_model.summary()

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 158)]        0                                            
__________________________________________________________________________________________________
model_1 (Model)                 (None, 158)          57310       input_1[0][0]                    
__________________________________________________________________________________________________
subtract (Subtract)             (None, 158)          0           input_1[0][0]                    
                                                                 model_1[1][0]                    
__________________________________________________________________________________________________
multiply (Multiply)             (None, 158)          0           subtract[0][0]                   
                                                                 subtract[0][0]                   
__________________________________________________________________________________________________
lambda (Lambda)                 (None,)              0           multiply[0][0]                   
==================================================================================================
Total params: 57,310
Trainable params: 57,310
Non-trainable params: 0
__________________________________________________________________________________________________

This model outputs correct values, but it might be creating issues down the line with the next step of my work, so I would like to know what this output shape means, and if I have to correct it (as I saw no exemples of this case online).

Edit

To specify, I'm not investigating about the None value, but the fact that it doesn't say (None,1), is it the same thing ?

As an exemple, this summary:

Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 2)                 4
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 3
=================================================================
Total params: 7
Trainable params: 7
Non-trainable params: 0
_________________________________________________________________

source: https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/

Upvotes: 1

Views: 5051

Answers (2)

FrederickDeny
FrederickDeny

Reputation: 21

I managed to reshape the last layer into a (None,1), and it did solve an issue in my code, I did it by adding a Reshape layer to my model:

x = Input(158,)
y = model(x)
c = Subtract()([x,y])
c = Multiply()([c,c])
d = Lambda(lambda arg: tf.keras.backend.mean(arg,axis=1), output_shape = (None,1))
e = d(c)
f = Reshape([1])(e)

new_model = Model(inputs = x, outputs = f)

Which gives:

new_model.summary()

Model: "model_4"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            [(None, 158)]        0                                            
__________________________________________________________________________________________________
model_1 (Model)                 (None, 158)          57310       input_5[0][0]                    
__________________________________________________________________________________________________
subtract_4 (Subtract)           (None, 158)          0           input_5[0][0]                    
                                                                 model_1[5][0]                    
__________________________________________________________________________________________________
multiply_4 (Multiply)           (None, 158)          0           subtract_4[0][0]                 
                                                                 subtract_4[0][0]                 
__________________________________________________________________________________________________
lambda_4 (Lambda)               (None,)              0           multiply_4[0][0]                 
__________________________________________________________________________________________________
reshape_3 (Reshape)             (None, 1)            0           lambda_4[0][0]                   
==================================================================================================
Total params: 57,310
Trainable params: 57,310
Non-trainable params: 0
__________________________________________________________________________________________________

Upvotes: 1

Rishabh Sahrawat
Rishabh Sahrawat

Reputation: 2507

None here represents your batch size. Batch size value is dynamic, you define it later during .fit() so before the definition it doesn't know the size and it stays None meaning any positive integer value.

You can read here to understand parameters and values a bit better.

Upvotes: 3

Related Questions