问题描述
我的输入只是一个具有339732行和两列的csv文件:
My input is simply a csv file with 339732 rows and two columns :
- 第一个是29个特征值,即X
- 第二个是二进制标签值,即Y
我正在尝试在堆叠的LSTM模型上训练数据:
I am trying to train my data on a stacked LSTM model:
data_dim = 29
timesteps = 8
num_classes = 2
model = Sequential()
model.add(LSTM(30, return_sequences=True,
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 30
model.add(LSTM(30, return_sequences=True)) # returns a sequence of vectors of dimension 30
model.add(LSTM(30)) # return a single vector of dimension 30
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.summary()
model.fit(X_train, y_train, batch_size = 400, epochs = 20, verbose = 1)
这会引发错误:
ValueError:检查模型输入时出错:预期的lstm_1_input为 有3个维度,但数组的形状为(339732,29)
ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)
我尝试使用X_train.reshape((1,339732, 29))
重塑我的输入,但显示错误不起作用:
I tried reshaping my input using X_train.reshape((1,339732, 29))
but it did not work showing error:
如何将我的输入输入LSTM?
How can I feed in my input to the LSTM ?
推荐答案
设置timesteps = 1
(因为每个实例我要一个时间步长),并将X_train和X_test重塑为:
Setting timesteps = 1
(since, I want one timestep for each instance) and reshaping the X_train and X_test as:
import numpy as np
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
这行得通!
这篇关于检查模型输入时出错:预期lstm_1_input具有3个维,但数组的形状为(339732,29)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!