拟合时的Keras GRU NN KeyError:“不在索引中”

时间:2015-11-06 10:05:14

标签: python neural-network theano keyerror keras

我正在尝试将我的GRU模型与我的训练数据相匹配时遇到问题。 在快速浏览StackOverflow之后,我发现这篇文章与我的问题非常相似:

Simplest Lstm training with Keras io

我自己的模型如下:

nn = Sequential()
nn.add(Embedding(input_size, hidden_size))
nn.add(GRU(hidden_size_2, return_sequences=False))
nn.add(Dropout(0.2))
nn.add(Dense(output_size))
nn.add(Activation('linear'))

nn.compile(loss='mse', optimizer="rmsprop")

history = History()
nn.fit(X_train, y_train, batch_size=30, nb_epoch=200, validation_split=0.1, callbacks=[history])

错误是:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-14-e2f199af6e0c> in <module>()
      1 history = History()
----> 2 nn.fit(X_train, y_train, batch_size=30, nb_epoch=200, validation_split=0.1, callbacks=[history])

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\keras\models.pyc in fit(self, X, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, show_accuracy, class_weight, sample_weight)
    487                          verbose=verbose, callbacks=callbacks,
    488                          val_f=val_f, val_ins=val_ins,
--> 489                          shuffle=shuffle, metrics=metrics)
    490 
    491     def predict(self, X, batch_size=128, verbose=0):

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\keras\models.pyc in _fit(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, metrics)
    199                 batch_ids = index_array[batch_start:batch_end]
    200                 try:
--> 201                     ins_batch = slice_X(ins, batch_ids)
    202                 except TypeError as err:
    203                     raise Exception('TypeError while preparing batch. \

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\keras\models.pyc in slice_X(X, start, stop)
     53     if type(X) == list:
     54         if hasattr(start, '__len__'):
---> 55             return [x[start] for x in X]
     56         else:
     57             return [x[start:stop] for x in X]

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in __getitem__(self, key)
   1789         if isinstance(key, (Series, np.ndarray, Index, list)):
   1790             # either boolean or fancy integer index
-> 1791             return self._getitem_array(key)
   1792         elif isinstance(key, DataFrame):
   1793             return self._getitem_frame(key)

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in _getitem_array(self, key)
   1833             return self.take(indexer, axis=0, convert=False)
   1834         else:
-> 1835             indexer = self.ix._convert_to_indexer(key, axis=1)
   1836             return self.take(indexer, axis=1, convert=True)
   1837 

C:\Users\XXXX\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\indexing.pyc in _convert_to_indexer(self, obj, axis, is_setter)
   1110                 mask = check == -1
   1111                 if mask.any():
-> 1112                     raise KeyError('%s not in index' % objarr[mask])
   1113 
   1114                 return _values_from_object(indexer)

KeyError: '[   61 13980 11357  5577 11500 12125 19673 10985  2480  5237  2519 14874\n 16003  2611  3851 10837 11865 14607 10682  5495 10220  5043 23145 11280\n  9547  4766 18323   730  6263] not in index'

有什么想法解决这个问题吗? 感谢

编辑:有关数据的一些事实:

data_X = pd.read_csv("X.csv")
data_Y = pd.read_csv("Y.csv")

def train_test_split(X,Y, test_size=0.15):  
    #    This just splits data to training and testing parts
    ntrn = int(round(X.shape[0] * (1 - test_size)))
    perms = np.random.permutation(X.shape[0])
    X_train = X.ix[perms[0:ntrn]]
    Y_train = Y.ix[perms[0:ntrn]]
    X_test = X.ix[perms[ntrn:]]
    Y_test = Y.ix[perms[ntrn:]]

    return (X_train, Y_train), (X_test, Y_test) 

X和Y是包含时间序列值的CSV文件(例如,对于每一行,X文件中有37个连续的时间序列值+ 2个时间值(视为过去),Y文件中有30个(视为预测预测))

print X_train[:1]
print y_train[:1]

          0   1   2   3   4   5   6   7   8    9      ...       29   30   31   32  \
1629  84  76  76  72  72  72  72  87  87  100     ...      165  165  169  169   

       33   34   35   36          37          38  
1629  166  166  185  185  1236778440  1236789240  

[1 rows x 39 columns]
       0    1    2    3    4    5    6    7    8    9  ...    20   21   22  \
1629  195  195  195  195  196  196  194  194  192  192 ...   182  182  164   

       23   24   25   26   27   28   29  
1629  164  146  146  128  128  103  103  

[1 rows x 30 columns]

2 个答案:

答案 0 :(得分:22)

我无法使用Pandas DataFrames作为输入&amp;输出到Keras model.fit,至少不是Pandas 0.13.1,这是Ubuntu的标准软件包。

相反,使用np.array(X_train)和np.array(Y_train)。这对我有用。

答案 1 :(得分:1)

我遇到了类似的问题。在我的情况下,问题在于您在输入中使用具有预定义维度的嵌入图层,因此传递给此图层的序列应使用keras.preprocessing.sequence填充或截断为input_size。