检查模型输入时出错:预期 lstm_1_input 有 3 个维度,但得到了具有形状的数组 (339732, 29) [英] Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)
问题描述
我的输入只是一个包含 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)
这会引发错误:
回溯(最近一次调用最后一次):文件first_approach.py",第 80 行,在模型.fit(X_train,y_train,batch_size = 400,epochs = 20,verbose = 1)
Traceback (most recent call last): File "first_approach.py", line 80, in 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:
ValueError: 检查模型输入时出错:预期 lstm_1_input 到有形状 (None, 8, 29) 但得到形状为 (1, 339732, 29) 的数组
ValueError: Error when checking model input: expected lstm_1_input to have shape (None, 8, 29) but got array with shape (1, 339732, 29)
如何将我的输入提供给 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!