problem description
use Tensorflow"s keras to deal with a text emotion multi-classification problem, but there are some inexplicable problems when building the model?
Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (4500, 4, 4)
the environmental background of the problems and what methods you have tried
Layer (type) Output Shape Param -sharp
=================================================================
lstm_9 (LSTM) (150, 1, 32) 772224
_________________________________________________________________
lstm_10 (LSTM) (150, 1, 32) 8320
_________________________________________________________________
lstm_11 (LSTM) (150, 32) 8320
_________________________________________________________________
dense_3 (Dense) (150, 4) 132
=================================================================
Total params: 788,996
Trainable params: 788,996
Non-trainable params: 0
it"s strange that the output from the upper layer of dense_3 is 2 dimensions, so why do you still report an error that does not conform to dim?
related codes
/ / Please paste the code text below (do not replace the code with pictures)
timesteps = 1
data_dim = 6000
num_classes = 4
batch_size = 150
x_train = train_x_array
x_val = val_x_array
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=6000, value=0)
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=6000, value=0)
print("x_train shape: {}".format(x_train.shape))
print("x_val shape: {}".format(x_val.shape))
x_train = x_train.reshape([batch_size * 30, timesteps, data_dim])
x_val = x_val.reshape([batch_size * 10, timesteps, data_dim])
print("x_train shape: {}".format(x_train.shape))
print("x_val shape: {}".format(x_val.shape))
// x_train shape: (4500, 6000)
// x_val shape: (1500, 6000)
// x_train shape: (4500, 1, 6000)
// x_val shape: (1500, 1, 6000)
model = Sequential()
model.add(LSTM(32, return_sequences=True, stateful=True,
batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(4, activation="softmax"))
model.compile(loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.summary()
model.fit(x_train, y_train,
batch_size=batch_size, epochs=3, shuffle=False,
validation_data=(x_val, y_val))