autonomio / talos

Hyperparameter Experiments with TensorFlow and Keras
https://autonom.io
MIT License
1.62k stars 268 forks source link

KeyError: 'val_acc' #314

Closed stevexxs closed 5 years ago

stevexxs commented 5 years ago

I use below code to get the model

# design network
def myModel(train_X, train_y, test_X, test_y, params):
    model = Sequential()
    model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=False))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    # fit network
    out = model.fit(train_X, train_y, epochs=params['epochs'], batch_size=params['batch_size'], validation_data=(test_X, test_y), verbose=2, shuffle=False)
    return out, model

talos.Scan(train_X, train_y, params=p, model=myModel)

KeyErr happened when running predict function.

model= talos.Predict(t)
yhat = model.predict(test_X)

the error is

KeyError                                  Traceback (most recent call last)
~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2524             try:
-> 2525                 return self._engine.get_loc(key)
   2526             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'val_acc'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-29-7f786a3b5650> in <module>
      1 # make a prediction
----> 2 yhat = model.predict(test_X)
      3 print('yhat.shape', yhat.shape, yhat[0:5, :])
      4 test_X_reshape = test_X.reshape((test_X.shape[0], test_X.shape[2]))
      5 print(test_X_reshape.shape, test_X_reshape[0:5, -7:])

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/commands/predict.py in predict(self, x, model_id, metric, asc)
     25 
     26         if model_id is None:
---> 27             model_id = best_model(self.scan_object, metric, asc)
     28 
     29         model = activate_model(self.scan_object, model_id)

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/utils/best_model.py in best_model(self, metric, asc)
      9     NOTE: for loss 'asc' should be True'''
     10 
---> 11     best = self.data.sort_values(metric, ascending=asc).iloc[0].name
     12 
     13     return best

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/frame.py in sort_values(self, by, axis, ascending, inplace, kind, na_position)
   3617 
   3618             by = by[0]
-> 3619             k = self.xs(by, axis=other_axis).values
   3620             if k.ndim == 2:
   3621 

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level)
   2333 
   2334         if axis == 1:
-> 2335             return self[key]
   2336 
   2337         self._consolidate_inplace()

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2137             return self._getitem_multilevel(key)
   2138         else:
-> 2139             return self._getitem_column(key)
   2140 
   2141     def _getitem_column(self, key):

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2144         # get column
   2145         if self.columns.is_unique:
-> 2146             return self._get_item_cache(key)
   2147 
   2148         # duplicate columns & possible reduce dimensionality

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1840         res = cache.get(item)
   1841         if res is None:
-> 1842             values = self._data.get(item)
   1843             res = self._box_item_values(item, values)
   1844             cache[item] = res

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3841 
   3842             if not isna(item):
-> 3843                 loc = self.items.get_loc(item)
   3844             else:
   3845                 indexer = np.arange(len(self.items))[isna(self.items)]

~/anaconda3/envs/python36/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2525                 return self._engine.get_loc(key)
   2526             except KeyError:
-> 2527                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2528 
   2529         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'val_acc'

Could you please help me find out the reason ? Thanks !

mikkokotila commented 5 years ago

In t.data do you have column 'val_acc'. If not, add metrics=['acc'] to model.compile() in your model.