I recently wrote an LSTM model to predict sequence:
############### BUILD MODEL ###############
''' HERE WE ARE CREATING THE LSTM MODEL '''
model = Sequential()
model.add(LSTM(128, input_shape=(X.shape[1:]), activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128,input_shape=(X.shape[1:]), activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
# In[8]:
'''HERE WE ARE CREATING AN OPTIMIZER AND THEN TRAINING OUR MODEL'''
opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'],
)
model.fit(X, Y, batch_size=10, epochs=1)
where np.shape(X) = (237, 30, 3)
and np.shape(Y) = (237, 3)
. But while fitting this data to the model it is returning an error:
ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (3,)
What is wrong in this code?
答案 0 :(得分:0)
It seems you have one-hot encoded the labels. Either don't one-hot encode them (i.e. let them be sparse labels) and use sparse_categorical_crossentropy
as the loss function or alternatively, one-hot encode them and use categorical_crossentropy
as the loss function.
As a side note: are you sure it is a classification task? Since you are using a softmax layer as the last layer with 10 units but you mentioned that the labels have 3 classes?!
答案 1 :(得分:0)
如果数据形状是这样的:np.shape(X)=(237,30,3)和np.shape(Y)=(237,3) 试试这个网络。
model = Sequential()
model.add(LSTM(32, input_shape=(X.shape[1:]), activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(32, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)
model.compile(
loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'],
)
model.fit(X, Y, batch_size=10, epochs=1)
由于使用的是Keras Sequential Api,因此您不必为第二个LSTM层指定输入,并且在最终的Dense层,您的“ Y”形状为(237,3),因此最终的Dense层也应为也给出3。
如果每个样本中输入中的“ 3”(要素数量)恒定,则还可以为第一个LSTM层输入input_shape =(None,3)。