Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras 2D output

Tags:

keras

I need to make a model that takes as input a 2D binary matrix: (37,10) for instance and return a real 2D matrix of the same shape as the input one. I erote this code but I am not sure what X (in the output layer) should be equal to.

model=Sequential()
model.add(Dense(32,activation='linear',input_shape=(37,10)))
model.add(Dense(32,activation='linear'))
model.add(Dense(X,activation='linear'))
model.compile(loss='mse',optimizer=Adam(lr=self.learning_rate),metrics=['accuracy'])

Please let me know if you think my model is correct as defined and what to write instead of X

Thank you

like image 758
Sou Avatar asked Oct 17 '25 16:10

Sou


1 Answers

I updated your code to get shape of output same as input. We need to add Flatten and Reshape layer at the start and end of the model. In simple, X should be equal to the number of elements in the input_shape.

from tensorflow.keras.layers import Dense, Flatten,Reshape
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam

input_shape=(37,10)
num_elm =input_shape[0]*input_shape[1]
model=Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(32, activation='linear'))
model.add(Dense(32, activation='linear'))
model.add(Dense(num_elm, activation='linear'))
model.add(Reshape(input_shape))
model.compile(loss='mse',optimizer=Adam(),metrics=['accuracy'])

model.summary()

Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_4 (Flatten)          (None, 370)               0         
_________________________________________________________________
dense_14 (Dense)             (None, 32)                11872     
_________________________________________________________________
dense_15 (Dense)             (None, 32)                1056      
_________________________________________________________________
dense_16 (Dense)             (None, 370)               12210     
_________________________________________________________________
reshape (Reshape)            (None, 37, 10)            0         
=================================================================
Total params: 25,138
Trainable params: 25,138
Non-trainable params: 0
_________________________________________________________________
like image 162
Vishnuvardhan Janapati Avatar answered Oct 20 '25 16:10

Vishnuvardhan Janapati