autonomio / talos

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

Key error, 'not in index' with model #233

Closed shbfy closed 5 years ago

shbfy commented 5 years ago
p = {'lr': (0.5, 5, 10),
     'first_neuron':[4, 8, 16, 32, 64],
     'batch_size': (2, 30, 10),
     'epochs': [3, 5, 10],
     'dropout': (0, 0.5, 5),
     'optimizer': [Adam, Nadam, RMSprop],
     'activation':[relu, elu, sigmoid],
     'last_activation': [sigmoid]}
def cnn_gru_model(X_train, y_train, max_words, params):
    model = Sequential()
    model.add(Embedding(max_words, 4000))
    model.add(Dropout(params['dropout']))
    model.add(Conv1D(params['first_neuron'], 5, activation=params['activation']))
    model.add(MaxPooling1D(pool_size=5))

    model.add(Bidirectional(GRU(16)))
    model.add(BatchNormalization())

    model.add(Dense(3, activation=params['last_activation']))

    model.compile(loss='binary_crossentropy', 
                  optimizer=params['optimizer'](lr=lr_normalizer(params['lr'], params['optimizer'])),
                                                metrics=['accuracy'])
    out = model.fit(X_train, y_train.values, batch_size=params['batch_size'],
                    epochs=params['epochs'],
                    verbose=0,
                    callbacks=[early_stop, reduce_lr])
    return out, model

Talos scan

h = ta.Scan(X_train, y_train, seed=7, params=p, model=cnn_gru_model, grid_downsample=.01)

Traceback

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-ef172bafecb3> in <module>
----> 1 h = ta.Scan(X_train, y_train, seed=7, params=p, model=cnn_gru_model, grid_downsample=.01)

/anaconda3/lib/python3.6/site-packages/talos/scan/Scan.py in __init__(self, x, y, params, model, dataset_name, experiment_no, x_val, y_val, val_split, shuffle, round_limit, grid_downsample, random_method, seed, search_method, reduction_method, reduction_interval, reduction_window, reduction_threshold, reduction_metric, reduce_loss, last_epoch_value, clear_tf_session, disable_progress_bar, print_params, debug)
    163         # input parameters section ends
    164 
--> 165         self._null = self.runtime()
    166 
    167     def runtime(self):

/anaconda3/lib/python3.6/site-packages/talos/scan/Scan.py in runtime(self)
    167     def runtime(self):
    168 
--> 169         self = scan_prepare(self)
    170         self = scan_run(self)

/anaconda3/lib/python3.6/site-packages/talos/scan/scan_prepare.py in scan_prepare(self)
     57     # create the data asset
     58     self.y_max = self.y.max()
---> 59     self = validation_split(self)
     60     self.shape = classify(self.y)
     61     self.last_neuron = last_neuron(self)

/anaconda3/lib/python3.6/site-packages/talos/utils/validation_split.py in validation_split(self)
     16     else:
     17         if self.shuffle:
---> 18             random_shuffle(self)
     19 
     20         limit = int(len(self.x) * (1 - self.val_split))

/anaconda3/lib/python3.6/site-packages/talos/utils/validation_split.py in random_shuffle(self)
     60         self.x = self.x[ix]
     61 
---> 62     self.y = self.y[ix]
     63 
     64 

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2680         if isinstance(key, (Series, np.ndarray, Index, list)):
   2681             # either boolean or fancy integer index
-> 2682             return self._getitem_array(key)
   2683         elif isinstance(key, DataFrame):
   2684             return self._getitem_frame(key)

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_array(self, key)
   2724             return self._take(indexer, axis=0)
   2725         else:
-> 2726             indexer = self.loc._convert_to_indexer(key, axis=1)
   2727             return self._take(indexer, axis=1)
   2728 

/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1325                 if mask.any():
   1326                     raise KeyError('{mask} not in index'
-> 1327                                    .format(mask=objarr[mask]))
   1328 
   1329                 return com._values_from_object(indexer)

KeyError: '[2060  933  378 ...  537 1220  175] not in index'
mikkokotila commented 5 years ago

what you do you get with:

pip freeze | grep talos

shbfy commented 5 years ago

what you do you get with:

pip freeze | grep talos

Hey Mikkokotlia,

I get this:

talos==0.4.7
mikkokotila commented 5 years ago

I noted that your model does not have validation data in model.fit(). You have to explicitly input x_val and y_val there as you would in a Keras model where you directly pass validation data.

You can alsopip install -U git+https://github.com/autonomio/talos@daily-dev to get yourself on the latest version v.0.5.0 which is a major upgrade in fact.

shbfy commented 5 years ago

Thanks! I’ll give that a go and let you know if it corrects the issue.

Yes I did have the validation included before, ran into the same issue though unfortunately :(

shbfy commented 5 years ago

I noted that your model does not have validation data in model.fit(). You have to explicitly input x_val and y_val there as you would in a Keras model where you directly pass validation data.

You can alsopip install -U git+https://github.com/autonomio/talos@daily-dev to get yourself on the latest version v.0.5.0 which is a major upgrade in fact.

Still have the same issue mikkokotlia :(

mikkokotila commented 5 years ago

Your input model has a mistake:

Now it's:

def cnn_gru_model(X_train, y_train, max_words, params):

But has to be:

def cnn_gru_model(x_train, y_train, x_val, y_val, params):

If you want to pass max_words, you do that through params dictionary. All parameters go in that way.

mikkokotila commented 5 years ago

Closing here, feel free to open new issue if you find anything.