【问题标题】:Python/Keras/Neural Networks - Basics to make it runPython/Keras/Neural Networks - 使其运行的基础知识
【发布时间】:2016-05-15 15:54:52
【问题描述】:

我是一名使用 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

关于如何让这只猛犸象动起来有什么见解吗?

【问题讨论】:

  • 输入 dim 需要与您的训练数据的维度相匹配。我想 Keras 正在尝试仅使用 96 列数据来训练您的 100 列规范。
  • 那么,在上面写着“input_dim=100”的地方我应该用“input_dim=129”代替吗? (129 是我的预测系列中的列数)@CharlieHaley
  • 你上面的数据说它是 96 列和 129 行,所以我认为是 96。
  • 该死,你是对的。我试试看。

标签: python keras


【解决方案1】:

因此,您不想将 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))

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

不过,在排序之后,一旦您的程序到达您在model.fit 中调用的 Keras 函数,您可能会遇到不同的错误。具体来说,您可能会看到数据与模型预期的形状不匹配。在输入 Keras 模型之前,您的列是如何编码的? Keras is expecting a numpy array 用于 Xymodel.fit。另外,您期望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 
---

模型开始了解 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    <--------------------------
---

您可以将以下打印行添加到CharacterTable 类中的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 
---

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

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

所以,你可以在模型的第一层指定一个输入形状:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2018-05-25
    相关资源
    最近更新 更多