如何处理keras LSTM的输入和输出形状 [英] How to process input and output shape for keras LSTM

查看:312
本文介绍了如何处理keras LSTM的输入和输出形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习RNN,我使用sklearn生成的样本数据集在keras(theano)中编写了这个简单的LSTM模型.

I am learning about RNN and I wrote this simple LSTM model in keras (theano) using a sample dataset generated using sklearn.

from sklearn.datasets import make_regression
from keras.models import Sequential
from keras.layers import Dense,Activation,LSTM

#creating sample dataset
X,Y=make_regression(100,9,9,2)
X.shape
Y.shape

#creating LSTM model
model = Sequential()
model.add(LSTM(32, input_dim=9))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

#model fitting
model.fit(X, Y, nb_epoch=1, batch_size=32)

样本数据集包含9个要素和2个目标.当我尝试使用这些功能拟合模型并将目标定位给我这个错误

The sample data set contains 9 features and 2 targets. when I tried to fit my model using those features and targets its giving me this error

Exception: Error when checking model input: expected lstm_input_9 to have 3 dimensions, but got array with shape (100, 9)

推荐答案

如果我是正确的,那么LSTM需要3D输入.

If I'm correct, then LSTM expects a 3D input.

X = np.random.random((100, 10, 64))
y = np.random.random((100, 2))

model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
model.add(Dense(2)) 
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, nb_epoch=1, batch_size=32)

更新:如果要将X, Y = make_regression(100, 9, 9, 2)转换为3D,则可以使用它.

UPDATE: If you want to convert X, Y = make_regression(100, 9, 9, 2) into 3D, then you can use this.

from sklearn.datasets import make_regression
from keras.models import Sequential
from keras.layers import Dense,Activation,LSTM

#creating sample dataset
X, Y = make_regression(100, 9, 9, 2)
X = X.reshape(X.shape + (1,))

#creating LSTM model
model = Sequential()
model.add(LSTM(32, input_shape=(9, 1)))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, nb_epoch=1, batch_size=32)

这篇关于如何处理keras LSTM的输入和输出形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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