Home > Software engineering >  Is it possible to make the output shape the same as the input shape?
Is it possible to make the output shape the same as the input shape?

Time:07-09

I want my model below to take in an input shape of [8, 1] with a batch size of 16. I have been trying to get the output shape of the model to be the same as the input but I have only been able to get it to output a shape of (16, 1, 1) when I want (16, 8, 1). Is it possible to do this? Thank you for any help.

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv1D(filters = 512,
                           batch_size = 16,
                           input_shape = [8, 1],
                           kernel_size = 3,
                           strides = 1,
                           activation = 'relu'),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128)),
    tf.keras.layers.Dense(30),
    tf.keras.layers.Dense(15),
    tf.keras.layers.Dense(1),
    tf.keras.layers.Reshape([1, -1])
])


Model: "sequential_26"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_26 (Conv1D)          (16, 6, 512)              2048      
                                                                 
 bidirectional_26 (Bidirecti  (16, 256)                656384    
 onal)                                                           
                                                                 
 dense_69 (Dense)            (16, 30)                  7710      
                                                                 
 dense_70 (Dense)            (16, 15)                  465       
                                                                 
 dense_71 (Dense)            (16, 1)                   16        
                                                                 
 reshape_23 (Reshape)        (16, 1, 1)                0         
                                                                 
=================================================================
Total params: 666,623
Trainable params: 666,623
Non-trainable params: 0
_________________________________________________________________

EDIT: Also the last dense layer can only have 1 unit since I am only predicting one feature

CodePudding user response:

You can use a Flatten() layer and then a Dense layer with (16 * 8 * 1) units, then you can use a Reshape((16, 8, 1)), that should work.

CodePudding user response:

In order to obtain an output shape of ( 16 , 8 , 1 ), you need to set the target shape as [ -1 , 1 ] in the Reshape layer of the model,

import tensorflow as tf

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv1D(filters = 512,
                           batch_size = 16,
                           input_shape = [8, 1],
                           kernel_size = 3,
                           strides = 1,
                           activation = 'relu'),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128)),
    tf.keras.layers.Dense(30),
    tf.keras.layers.Dense(15),
    tf.keras.layers.Dense(8),
    tf.keras.layers.Reshape( [ -1 , 1 ] )
])
model.summary()

-1 indicates that the same number of dimensions should be taken as in the input shape for a particular axis. In your case, the input shape is ( 16 , 8 ) where 16 is the batch dimension. So -1 corresponds to 2nd dimension in the input shape i.e. 8. Hence, the target shape for the Reshape layer is ( -1 , 1 ) or ( 8 , 1 ).

  • Related