3

我是使用 Keras 进行预测的初学者。我理解这个概念和它背后的所有理论。但我很难让它运行。在这个阶段我并不担心它的效率,我只是想运行它并看到一个输出,以便我以后可以进化。

我有这个用作 predictor_train (X) 的虚拟 Pandas DataFrame:

                 Value        1lag        2lag        3lag        4lag...
Date                                                                     
2005-04-01  231.721933  165.195418  170.418903  225.892387  206.282539   
2005-05-01  163.259812  231.721933  165.195418  170.418903  225.892387   
2005-06-01  211.649963  163.259812  231.721933  165.195418  170.418903   
2005-07-01  249.054951  211.649963  163.259812  231.721933  165.195418   
2005-08-01  168.657539  249.054951  211.649963  163.259812  231.721933   
2005-09-01  179.623448  168.657539  249.054951  211.649963  163.259812   
2005-10-01  228.437842  179.623448  168.657539  249.054951  211.649963   
2005-11-01  165.805266  228.437842  179.623448  168.657539  249.054951
...
[129 rows x 96 columns]

我有另一个 DataFrame 用作 target_train (Y):

Date
2005-04-01   -0.01136
2005-05-01    0.04713
2005-06-01    0.00329
2005-07-01   -0.00985
2005-08-01    0.05635
2005-09-01   -0.03766
2005-10-01    0.01848
2005-11-01   -0.01387
[129 rows x 1 column]

我正在使用以下代码:

from keras.models import Sequential
from keras.layers.core import Dense, Activation


model=Sequential() 
model.add(Dense(output_dim=64, input_dim=100, init="glorot_uniform"))
model.add(Activation("tanh"))
model.add(Dense(output_dim=10, init="glorot_uniform"))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.fit(predictor_train, target_train, nb_epoch=5, batch_size=32,show_accuracy=True)
prediction=model.predict(predictor_train)

print prediction

我收到以下错误:

File "/Users/file.py", line 1271, in var_neural_reg1
model.fit(predictor_train, target_train, nb_epoch=5, batch_size=32,show_accuracy=True)
File "/Library/Python/2.7/site-packages/keras/models.py", line 581, in fit
shuffle=shuffle, metrics=metrics)
File "/Library/Python/2.7/site-packages/keras/models.py", line 230, in _fit
ins_batch = slice_X(ins, batch_ids)
File "/Library/Python/2.7/site-packages/keras/models.py", line 65, in slice_X
return [x[start] for x in X]
File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1908, in __getitem__
return self._getitem_array(key)
File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1953, in _getitem_array
return self.take(indexer, axis=1, convert=True)
File "/Library/Python/2.7/site-packages/pandas/core/generic.py", line 1370, in take
convert=True, verify=True)
File "/Library/Python/2.7/site-packages/pandas/core/internals.py", line 3508, in take
indexer = maybe_convert_indices(indexer, n)
File "/Library/Python/2.7/site-packages/pandas/core/indexing.py", line 1721, in maybe_convert_indices
raise IndexError("indices are out-of-bounds")

IndexError: indices are out-of-bounds

关于如何使这个猛犸象移动的任何见解?

4

1 回答 1

3

因此,您不想将您的设置input_dim为 129。这只是样本数。Keras 并不真正关心您的数据是 1 行还是 100 万行;这不是您的数据的维度。如果您的数据是一个 numpy 数组,则输入形状将为 (1, 2, 3) = (2, 3)。行数 (1) 没有区别。

但是,这不是这里的问题。您收到的错误与您指定的维度无关。如果是这种情况,那么如果您的数据与您在模型中指定的内容之间实际上存在形状不匹配,那么您将收到一个不同的错误(ValueError而不是),该错误会追溯到您的后端(Theano 或 Tensorflow)。IndexError例如:

ValueError: ('shapes (x,x) and (x,x) not aligned: x (dim 1) != x (dim 0)', (x, x), (x, x))

查看错误消息,这是您如何预处理数据的问题;即,问题在于熊猫而不是Keras。您正在使用 Keras 的slice_X函数来分割您的数据,但是当 Keras 与 pandas 交谈并告诉它您想要从中获取数据时dataframe[x:y],pandas 会响应您指定的索引在给定数据帧的情况下无效。

但是,在排序之后,一旦您的程序到达您正在调用的 Keras 函数,您可能会遇到不同的错误model.fit。具体来说,您可能会看到数据与模型预期的形状不匹配。在输入 Keras 模型之前,您的列是如何编码的? Keras 期待一个 numpy 数组forXyin model.fit。另外,您期望什么样的输出output_dim = 10

我推荐这个 Keras 示例,它使用 LSTM 模型显示序列到序列的预测,在这种情况下学习加法,因为它完全符合您的要求;即,这是一个具体的例子,它显示了模型在每个时期都在运行和发展。

--------------------------------------------------
Iteration 1
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 30s - loss: 1.6745 - acc:0.3927- val_loss: 1.5373 - val_acc: 0.4320
Q 58333+69862
T 128195
☒ 13335 
---
.
.
.
---
Q 83911+65   
T 83976 
☒ 11115 
---

该模型开始了解 epoch 7 左右的情况:

Iteration 7
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 31s - loss: 0.9129 - acc: 0.6549 - val_loss: 0.9117 - val_acc: 0.6510
Q 80+3375    
T 3455  
☒ 3420  
---
.
.
.
---
Q 6+50853    
T 50859 
☑ 50859    <--------------------------
---

您可以将以下打印行添加到类中的decode函数中,CharacterTable以查看发生了什么。在这种情况下,源添加问题是反向输入的,这通过在输入和目标之间引入短期依赖关系来提高此类模型的性能(Sutskever、Vinyals 和 Le 2014

    def decode(self, X, calc_argmax=True):
        if calc_argmax:
            X = X.argmax(axis=-1)
        print('{0} --> {1}'.format(X, [self.indices_char[x] for x in X]))
        return ''.join(self.indices_char[x] for x in X)

例如:

Iteration 3
Train on 45000 samples, validate on 5000 samples
Epoch 1/1
45000/45000 [==============================] - 21s - loss: 1.4865 - acc: 0.4491 - val_loss: 1.4132 - val_acc: 0.4676
[ 0 11 12  2  4 11 12] --> [' ', '7', '8', '+', '0', '7', '8']
[13  9 11  0] --> ['9', '5', '7', ' ']
[12  4 11  0] --> ['8', '0', '7', ' ']
Q 870+87 
T 957 
☒ 807 
---
[ 0  8 10  3  8  4 10] --> [' ', '4', '6', '-', '4', '0', '6']
[9 8 4 0] --> ['5', '4', '0', ' ']
[10  4 13  0] --> ['6', '0', '9', ' ']
Q 604-64 
T 540 
☒ 609 
---

这种编码器/解码器模型可以很容易地改变以适应其他类型的序列到序列预测问题。

您可能需要修改代码以适合您的数据。但是,在您的情况下,如果您要将上述数据按原样输入模型,我相信您input_dim会这样做len('0123456789.- '),因为这是您的数据的维度;即,输入和目标中所有字符的集合。在这个链接的例子中,序列被转换为单个字符的 one-hot 编码。

因此,您将在模型的第一层中指定一个输入形状:

model.add(LSTM(HIDDEN_DIMENSIONS, input_shape=(MAX_LENGTH, INPUT_DIMENSIONALITY)))
于 2016-02-07T17:07:41.853 回答