Keras LSTM层的输入形状

3

我正在尝试将包含20个特征的序列馈送到LSTM网络中,如代码所示。但是我遇到了一个错误,提示我的Input0与LSTM输入不兼容。不确定如何更改我的层结构以适应数据。

def build_model(features, aux1=None, aux2=None):
# create model
features[0] = np.asarray(features[0])
main_input = Input(shape=features[0].shape, dtype='float32', name='main_input')
main_out   = LSTM(40, activation='relu')
aux1_input = Input(shape=(len(aux1[0]),),   dtype='float32', name='aux1_input')
aux1_out   = Dense(len(aux1[0]))(aux1_input)
aux2_input = Input(shape=(len(aux2[0]),),   dtype='float32', name='aux2_input')
aux2_out   = Dense(len(aux2[0]))(aux2_input)
x = concatenate([aux1_out, main_out, aux2_out])
x = Dense(64, activation='relu')(x)
x = Dropout(0.5)(x)
output = Dense(1, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[aux1_input, aux2_input, main_input], outputs= [output])
return model

特征变量是一个形状为(1456,20)的数组,我有1456天,每天有20个变量。


1
请显示错误信息。您的序列有20个特征?但是您的序列的长度是多少?(有多少个时间步骤?) - Daniel Möller
1
ValueError: 输入0与层lstm_1不兼容:期望的ndim=3,但发现的ndim=2 是确切的错误。 - Oguzhan
2个回答

4
您的 main_input 应该具有形状 (samples, timesteps, features),然后您应该像这样定义 main_input:
main_input = Input(shape=(timesteps,))  # for stateless RNN (your one)

对于有状态的RNN(不是你在示例中使用的那个),或者 main_input = Input(batch_shape=(batch_size, timesteps,))

如果你的features [0]是一个各种特征的一维数组(1个时间步长),那么你还需要像这样重新整形features [0]:

features[0] = np.reshape(features[0], (1, features[0].shape))

然后对features[1]features[2]等执行相同的操作。

或者更好的方法是一次性地重塑所有样本:

features = np.reshape(features, (features.shape[0], 1, features.shape[1]))

我将主要输入更改为main_input = Input(batch_shape=(batch_size, 20,), dtype='float32', name='main_input'),并使用LSTM(40, activation='relu')(main_input)进行了操作,但仍然出现错误(Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2)。 - Oguzhan
@Oğu,你确定你的x.shape和y.shape有3个维度(样本数、时间步长、特征数)吗?否则你需要重新调整数据。 - Bob

3

LSTM层被设计用于处理"序列"。

你说你的序列有20个特征,但它有多少时间步长?你是不是指的是20个时间步长呢?

LSTM层需要输入形状,如(BatchSize, TimeSteps, Features)

如果情况是每个时间步长中都有1个特征,那么你必须将你的数据进行如下的形状:

inputData = someData.reshape(NumberOfSequences, 20, 1)

Input 张量应该采用这种形式:

main_input = Input((20,1), ...) #yes, it ignores the batch size

如果我有5个时间步长,我该如何修改我的输入(在这种情况下是“特征”)? - Oguzhan
2
重新塑形为(NumberOfSequences, 5, 20) --- 假设有20个特征在5个时间步骤中变化。--- --- 我注意到你使用features[0].shape作为输入形状。如果features[0]已经正确塑形,你应该使用features[0].shape[1:],因为第一个数字是NumberOfSequences,在定义层时不会被传递。 - Daniel Möller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接