获取单层keras LSTM的正确尺寸

2024-04-23 19:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难弄清楚LSTM网络的规模

因此,我有以下数据:

train_data.shape
 (25391, 3) # to be read as 25391 timesteps and 3 features

train_labels.shape
 (25391, 1) # to be read as 25391 timesteps and 1 feature

所以我认为我的输入维度是(1, len(train_data), train_data.shape[1]),因为我计划提交一批。但我得到了以下错误:

Error when checking target: expected lstm_10 to have 2 dimensions, but got array with shape (1, 25391, 1)

以下是型号代码:

model = Sequential()
model.add(LSTM(1, # predict one feature and one timestep
               batch_input_shape=(1, len(train_data), train_data.shape[1]),
               activation='tanh',
               return_sequences=False))

model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])
print(model.summary())

# as 1 sample with len(train_data) time steps and train_data.shape[1] features.
model.fit(x=train_data.values.reshape(1, len(train_data), train_data.shape[1]), 
          y=train_labels.values.reshape(1, len(train_labels), train_labels.shape[1]), 
          epochs=1, 
          verbose=1, 
          validation_split=0.8, 
          validation_data=None, 
          shuffle=False)

输入维度应该是什么样子


Tags: andtoreaddatalabelsmodellenas
1条回答
网友
1楼 · 发布于 2024-04-23 19:19:58

问题在于您提供的目标(即标签)形状(即Error when checking target)。模型中LSTM层的输出也是模型的输出,其形状为(None, 1),因为您只指定要返回的最终输出(即return_sequences=False)。为了获得每个时间步的输出,您需要设置return_sequences=True。这样,LSTM层的输出形状将是(None, num_timesteps, num_units),这与您提供的标签数组的形状一致

相关问题 更多 >