ValueError:输入数组应具有与目标数组相同数量的样本。 LSTM Keras

时间:2018-10-02 18:55:30

标签: python-3.x keras lstm

我知道论坛上已经发布了多个类似的问题,但是我无法从中得出解决方案……无论如何,这是我的问题:

我有一个形状为(10240,3)的Pandas DataFrame。前两列包含数值,而第三列和最后一列包含每一行的标签。

# Selecting and reshaping the X_train set
X_train=df.loc[0:8191,df.columns != 'Label']
X_train=X_train.values.reshape([1, 8192, 2])

# Selecting and converting to categorical y_train
temp = df['Label']
y_train = temp[0:8192]
y_train = np_utils.to_categorical(y_train, num_classes = 5)

# Selecting and reshaping X_test
X_test = df.loc[8192:10240,df.columns != 'Label']
X_test = X_test.values.reshape([1, 2048, 2])

# Selecting and coverting to categorical y_test
y_test = temp[8192:10240]
y_test = np_utils.to_categorical(y_test, num_classes = 5)

接下来,我尝试在LSTM模型中插入具有5个不同类的数据,如下所示:

model = Sequential()

model.add(LSTM(100, input_shape = (8192, 2), return_sequences = True))
model.add(TimeDistributed(Dense(50)))
model.add(GlobalAvgPool1D())
model.add(Dense(5, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
print(model.summary())
history = model.fit(X_train, y_train, validation_data = (X_test, y_test), batch_size = 64, epochs = 40)

在编译时,我得到以下错误:ValueError:输入数组应具有与目标数组相同数量的样本。找到1个输入样本和8192个目标样本。

我检查了不同集合的形状是否匹配,并且据我所知它们确实匹配。此外,我认为输入遵循[示例,时间步长,功能]“经验法则”。而且批量大小似乎还可以。任何人都知道如何使输入正常工作吗?

我正在运行Python 3.x和Keras 2.2.2版

0 个答案:

没有答案