使用Keras io最简单的Lstm培训 [英] Simplest Lstm training with Keras io

查看:89
本文介绍了使用Keras io最简单的Lstm培训的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to create the simplest LSTM there is using keras python library.

我有以下代码:

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)

但似乎不起作用...

but does not seem to work...

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'

谁能向我解释到底出了什么问题?

Can anyone explain me what exactly goes wrong ?

我也尝试对矩阵进行转置,不是那样.

I also tried to Transpose the matrices but that's not it.

推荐答案

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

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)

然后它将起作用.

这篇关于使用Keras io最简单的Lstm培训的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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