Q:

Error when checking target: expected dense_21 to have 2 dimensions, but got array with shape (2300, 4, 54)

I have a problem which deals with predicting four outputs when given a vector of predictors. It is throwing an error at the LSTM layer input.
I have 
X.shape,Y.shape = ((2300, 36, 768), (2300, 4, 54))

# Core Part

checkpoint = ModelCheckpoint('model-{epoch:03d}-{acc:03f}-{val_acc:03f}.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')  

data_dim = 768
timesteps = X.shape[1]
num_classes = 10

# # expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()

model.add(LSTM(256, return_sequences=True, input_shape=(timesteps, data_dim))) 
model.add(LSTM(64, return_sequences=True))  
model.add(LSTM(32)) 

model.add(Dense(54, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop',  metrics=['accuracy'])

model.fit(X, Y, batch_size=64, epochs=10, validation_split=0.1)

The Above code is working fine when i have X.shape,Y.shape = ((2300, 36, 768), (2300, 15))
How can i overcome from this, and How can i set LSTM and DENSE layes if i have more then one, four or ten prediction output?
Thanks in Advance.

A:

If your output is not a sequence, you could make use of Reshape layer as follows:
from keras.models import Sequential
from keras.layers import LSTM, Reshape, Dense, Activation
model = Sequential()

model.add(LSTM(256, return_sequences=True, input_shape=(100, 32))) 
model.add(LSTM(64, return_sequences=True))  
model.add(LSTM(32))

# now we have a vector of length 32 and
# we want the dense layer to have 4*54=216 neurons so that
# we will be able to reshape it to a matrix of shape (4, 54) in
# the next (Reshape) layer.
model.add(Dense(4*54))
model.add(Reshape((4, 54)))

model.add(Activation('softmax'))

model.summary()

which will print:
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 100, 256)          295936    
_________________________________________________________________
lstm_2 (LSTM)                (None, 100, 64)           82176     
_________________________________________________________________
lstm_3 (LSTM)                (None, 32)                12416     
_________________________________________________________________
dense_1 (Dense)              (None, 216)               7128      
_________________________________________________________________
reshape_1 (Reshape)          (None, 4, 54)             0         
_________________________________________________________________
activation_1 (Activation)    (None, 4, 54)             0         
=================================================================
Total params: 397,656
Trainable params: 397,656
Non-trainable params: 0

