Python/Keras/Neural 网络 - 基础知识 运行

Python/Keras/Neural Networks - Basics to make it run

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

我有这个虚拟 Pandas DataFrame 我正在使用 predictor_train (X):

                 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

对如何进行这一重大举措有何见解?

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

但是,这不是这里的问题。您收到的错误与您指定的维度无关。如果是这种情况,那么你会收到一个不同的错误(ValueError 而不是 IndexError),它会追溯到你的后端(Theano 或 Tensorflow),如果你的数据以及您在模型中指定的内容。例如:

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

查看错误消息,这是您预处理数据的方式存在问题;即,问题出在 pandas 而不是 Keras。您正在使用 Keras 的 slice_X 函数来分割您的数据,但是当 Keras 与 pandas 对话并告诉它您想从 dataframe[x:y] 获取数据时,pandas 是回应您指定的索引对于您的数据框无效。

不过,排序后,一旦您的程序进入您在 model.fit 中调用的 Keras 函数,您可能 运行 会遇到不同的错误。具体来说,您可能会发现数据与模型预期的形状不匹配。在输入 Keras 模型之前,您的列是如何编码的? XKeras is expecting a numpy arraymodel.fity。另外,您期望什么样的输出 output_dim = 10?

我推荐 this Keras example,它显示了使用 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 
---

模型开始学习第 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    <--------------------------
---

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

这个 encoder/decoder 模型可以很容易地改变以适应其他类型的序列到序列预测问题。

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

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

model.add(LSTM(HIDDEN_DIMENSIONS, input_shape=(MAX_LENGTH, INPUT_DIMENSIONALITY)))