Simplest Lstm training with Keras io

做~自己de王妃 提交于 2019-12-03 16:03:10

First problem I see is using Pandas Dataframe. I think you should use numpy array here. The second problem is the X matrix. It should be a 3D array. For example if I try with

X_train = np.random.randn(6,2,2)

then it will work.

The error it shows me when I run your code is

ValueError: Error when checking input: expected lstm_2_input to have 3 dimensions, but got array with shape (6, 2)

You can fix this by inputting a 3D numpy array, as user108372 mentioned

A good way to debug is this to print out model.summary(), which will give you the output shapes expected for each of the layers. Additionally, you don't always need to specify the output and input shapes. Keras will take care of that for you :) Now, a working example would be something like this:

X_train = np.random.randn(6,2,2)
y_train = pd.DataFrame( np.array([1, 2, 3, 4, 3, 4]) ).values
X_test = np.random.randn(2,2,2)
y_test = pd.DataFrame( np.array([1, 2]) ).values

model = Sequential()
model.add(LSTM(32, 
               return_sequences=False, 
               input_dim=X_train.shape[1]))
# The shape of the last Dense layer should always correspond to y_train.shape[1]
model.add(Dense(y_train.shape[1])) 
model.add(Activation("linear"))
model.compile(loss="mean_squared_error",
              optimizer="rmsprop")

model.fit(X_train, y_train)

I recommend you print out shapes of the example above and align your shapes with the shapes described here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!