2015-11-06 3 views
5

Je suis actuellement confronté à un problème en essayant d'adapter mon modèle GRU à mes données d'entraînement. Après un rapide coup d'oeil sur StackOverflow, je trouve ce poste tout à fait semblable à ma question:Keras GRU NN KeyError lors de l'ajustement: "not in index"

Simplest Lstm training with Keras io

Mon modèle est le suivant:

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]) 

Et l'erreur est:

--------------------------------------------------------------------------- 
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' 

Une idée pour résoudre ce problème? Merci

EDIT: quelques faits sur les données:

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 et Y sont fichier CSV contenant des valeurs de série temporelle (par exemple, pour chaque rangée, il y a 37 valeurs consécutives de la série temporelle dans le fichier X + 2 valeurs de temps (considéré comme passé) et 30 dans le fichier Y (considérés comme les prévisions pour prédire))

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] 
+0

Quels sont les types de 'X_train' et' y_train'? –

+0

tous les deux sont '' – Julian

+0

J'ai édité le post pour ajouter quelques informations sur les données, au cas où cela pourrait aider ... – Julian

Répondre

19

Je ne pouvais pas utiliser Pandas DataFrames comme entrées & sorties à Keras model.fit, au moins pas Pandas 0.13.1, qui est t Le paquet standard d'Ubuntu. À la place, utilisez np.array (X_train) et np.array (Y_train). Cela a fonctionné pour moi.

+0

Superbe et simple solution :) –

+0

Solution frugale mais incroyable! –

1

J'ai rencontré un problème similaire. Dans mon cas, le problème était que vous utilisiez la couche Embeddings avec des dimensions prédéfinies en entrée, donc les séquences que vous passez à cette couche devraient être complétées ou tronquées à l'input_size en utilisant keras.preprocessing.sequence.