检查模型输入时出错:预期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)

查看:157
本文介绍了检查模型输入时出错:预期lstm_1_input具有3个维,但数组的形状为(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)

这会引发错误:

回溯(最近通话最近): 在第80行的文件"first_approach.py​​"中 model.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为 形状为(无,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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆