【发布时间】:2023-03-22 15:47:01
【问题描述】:
我正在尝试创建一个输入整数序列(索引)并输出另一个整数序列(索引)的模型。这些索引是编码的 item_ids。由于我有不同长度的时间步长,我正确填充了所有序列,现在我在LSTM 层之前添加了一个Masking。所以在填充之后,所有序列都有18长度,我也想输出18长度的序列,但是我的尺寸没有问题。我也只有 1 个功能。
我对神经网络很陌生,所以请原谅一些错误。
model = Sequential()
# masking empty timesteps
model.add(Masking(mask_value=MASKING, input_shape=(timesteps, 1)))
# LSTM layer with "max_views" time steps and one feature
model.add(LSTM(50)) # I tried to do model.add(LSTM(50, input_shape=(timesteps, 1))) but it gives the same result
# we need to output max views labels
model.add(Dense(timesteps))
# we can use here sparse categorical crossentropy, because our data are integers
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
我也使用sparse_categorical_crossentropy,因为我的数据是整数,不是热编码的。
这是摘要:
Model: "sequential_43"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_35 (Masking) (None, 18, 1) 0
_________________________________________________________________
lstm_42 (LSTM) (None, 50) 10400
_________________________________________________________________
dense_30 (Dense) (None, 18) 918
=================================================================
Total params: 11,318
Trainable params: 11,318
Non-trainable params: 0
当我尝试拟合模型时:
history = model.fit(X_train, y_train, epochs=10, batch_size=1, validation_data=(X_val, y_val), verbose=1)
它给了我错误:
ValueError: Shape mismatch: The shape of labels (received (18,)) should equal the shape of logits except for the last dimension (received (1, 18)).
这些是我的 3D 阵列形状:
print(X_train.shape)
print(y_train.shape)
print(X_val.shape)
print(y_val.shape)
(117182, 18, 1)
(117182, 18, 1)
(4132, 18, 1)
(4132, 18, 1)
【问题讨论】: