最简单的Lstm培训与Keras io

时间:2015-10-22 23:04:01

标签: python lstm keras

我想创建使用keras python库的最简单的LSTM。

我有以下代码:

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM

X_train = pd.DataFrame( np.array([ [1, 2], [3, 4], [5, 6], [7, 8], [5.1, 6.1], [7.1, 8.1] ]))
y_train = pd.DataFrame( np.array([1, 2, 3, 4, 3, 4]) )
X_test = pd.DataFrame( np.array([ [1.1, 2.1], [3.1, 4.1] ]) )
y_test = pd.DataFrame( np.array([1, 2]) )

model = Sequential()
model.add(LSTM( output_dim = 10, return_sequences=False, input_dim=X_train.shape[1]))
model.add(Dense(input_dim = 10, output_dim=2))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.fit(X_train, y_train)

但似乎不起作用......

Epoch 1/100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/keras/models.py", line 489, in fit
    shuffle=shuffle, metrics=metrics)
  File "/usr/lib/python2.7/site-packages/keras/models.py", line 201, in _fit
    ins_batch = slice_X(ins, batch_ids)
  File "/usr/lib/python2.7/site-packages/keras/models.py", line 55, in slice_X
    return [x[start] for x in X]
  File "/usr/lib/python2.7/site-packages/pandas-0.17.0-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 1908, in __getitem__
    return self._getitem_array(key)
  File "/usr/lib/python2.7/site-packages/pandas-0.17.0-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 1952, in _getitem_array
    indexer = self.ix._convert_to_indexer(key, axis=1)
  File "/usr/lib/python2.7/site-packages/pandas-0.17.0-py2.7-linux-x86_64.egg/pandas/core/indexing.py", line 1121, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: '[3 4 2 5] not in index'

有谁能解释一下到底出了什么问题?

我也尝试转置矩阵,但事实并非如此。

2 个答案:

答案 0 :(得分:1)

我看到的第一个问题是使用Pandas Dataframe。我想你应该在这里使用numpy数组。第二个问题是X矩阵。它应该是一个3D数组。例如,如果我尝试

X_train = np.random.randn(6,2,2)
然后它会起作用。

答案 1 :(得分:0)

运行代码时显示的错误是

  

ValueError:检查输入时出错:预期lstm_2_input有3个维度,但得到的数组有形状(6,2)

您可以通过输入3D numpy数组来解决此问题,如提及的用户108372

调试的一个好方法是打印出model.summary(),它将为您提供每个图层所需的输出形状。此外,您并不总是需要指定输出和输入形状。 Keras将为您照顾:)现在,一个工作示例将是这样的:

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)

我建议您打印上面示例的形状,并将形状与此处描述的形状对齐。