ValueError:检查输入时出错:期望input_1具有形状(无,1)但是具有形状的数组(5,54)

时间:2018-05-16 06:53:08

标签: python-3.x keras lstm rnn

我正在用keras实现一个lstm模型。

我的数据集中有11200行和5列。每个数据都是一个向量。数据集的形状为(11200,5,54),看起来像这样。

col1 col2 col3 col4 col5

[1,3,...,-999] [2,4,...,-999] [3,4,...,-999] [5,6,...,-999] [4,5,...,-999]

[0,2,...,-999] [1,5,...,-999] [1,24,...,-999] [11,7,...,-999] [-1,4,...,-999]

...

[0,2,...,5] [1,5,...,8] [1,24,...,6] [11,7,...,5] [-1,4,...,2]

每个载体的长度如此[1,3,..., - 999]为54。

目标是一个大小为(11200,1)的布尔矢量,如此

1      T

2      F

...    ...

11200  F   

我创建了这样的模型:

X_train, X_test, y_train, y_test = train_test_split(data,target, test_size=0.2, random_state=1)  
batch_size = 32 
timesteps = None 
output_size = 1
epochs=120

inputs = Input(batch_shape=(batch_size, timesteps, output_size))
lay1 = LSTM(20, stateful=True, return_sequences=True)(inputs)
output = Dense(units = output_size)(lay1)
regressor = Model(inputs=inputs, outputs = output)
regressor.compile(optimizer='adam', loss = 'mae')
regressor.summary()

for i in range(epochs):
    print("Epoch: " + str(i))
    regressor.fit(X_train, y_train, shuffle=False, epochs = 1, batch_size = batch_size)
    regressor.reset_states()

问题是我有这个错误:

ValueError: Error when checking input: expected input_1 to have shape (None, 1) but got array with shape (5, 54).

有什么问题?输入还是输出?我怎么能喂错了?

感谢。

1 个答案:

答案 0 :(得分:0)

问题在于Input图层的形状:

inputs = Input(batch_shape=(batch_size, timesteps, output_size)) # output_size = 1

您需要它的最终尺寸为54而不是output_size所以batch_shape=(batch_size, timesteps, 54)。让timesteps=None成为问题,如果你修复为5,它将允许你选择性地展开LSTM,这可能会加快计算速度。否则,它表示某些未知的时间步数,在您的情况下为5。