Sou
Sou

Reputation: 41

Keras 2D output

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

Upvotes: 4

Views: 2573

Answers (2)

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11198

X will be 10 even though using FC layers for 2-d data may not be very suitable in the first place, also you're sure the metrics will be accuracy.

Here's your model with the correct output shape.

Code:

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import numpy as np

model=Sequential()
model.add(Dense(32,activation='linear',input_shape=(37,10)))
model.add(Dense(32,activation='linear'))
model.add(Dense(10,activation='linear'))
model.compile(loss='mse',optimizer=Adam(lr=.001),metrics=['accuracy'])
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_8 (Dense)              (None, 37, 32)            352       
_________________________________________________________________
dense_9 (Dense)              (None, 37, 32)            1056      
_________________________________________________________________
dense_10 (Dense)             (None, 37, 10)            330       
=================================================================
Total params: 1,738
Trainable params: 1,738
Non-trainable params: 0
__________________________

Upvotes: 0

Vishnuvardhan Janapati
Vishnuvardhan Janapati

Reputation: 3278

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
_________________________________________________________________

Upvotes: 4

Related Questions