autonomio / talos

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

ValueError when adjust LSTM model for talos #311

Closed stevexxs closed 5 years ago

stevexxs commented 5 years ago

code as below works without talos

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
history = model.fit(train_X, train_y, epochs=50, batch_size=50, validation_data=(test_X, test_y), verbose=2, shuffle=False)

but when using talos, ValueError happened, the modification is as below.

# design network
def myModel(train_X, test_X, train_y, 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)

the error message is,

  0%|          | 0/12 [00:00<?, ?it/s]
[[0.06852049]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-7e98033f1bb7> in <module>
     18     return out, model
     19 
---> 20 talos.Scan(train_X, train_y, params=p, model=myModel)
     21 
     22 '''

~/anaconda3/envs/python36/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)
    168         # input parameters section ends
    169 
--> 170         self._null = self.runtime()
    171 
    172     def runtime(self):

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/scan/Scan.py in runtime(self)
    173 
    174         self = scan_prepare(self)
--> 175         self = scan_run(self)

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/scan/scan_run.py in scan_run(self)
     16                      disable=self.disable_progress_bar)
     17     while len(self.param_log) != 0:
---> 18         self = scan_round(self)
     19         self.pbar.update(1)
     20     self.pbar.close()

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/scan/scan_round.py in scan_round(self)
     30     # fit the model
     31     try:
---> 32         _hr_out, self.keras_model = ingest_model(self)
     33     except TypeError as err:
     34         if err.args[0] == "unsupported operand type(s) for +: 'int' and 'numpy.str_'":

~/anaconda3/envs/python36/lib/python3.6/site-packages/talos/model/ingest_model.py in ingest_model(self)
      8                       self.x_val,
      9                       self.y_val,
---> 10                       self.round_params)

<ipython-input-17-7e98033f1bb7> in myModel(train_X, test_X, train_y, test_y, params)
     15     model.compile(loss='mae', optimizer='adam')
     16     # fit network
---> 17     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)
     18     return out, model
     19 

~/anaconda3/envs/python36/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~/anaconda3/envs/python36/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    787                 feed_output_shapes,
    788                 check_batch_axis=False,  # Don't enforce the batch size.
--> 789                 exception_prefix='target')
    790 
    791             # Generate sample-wise weight values given the `sample_weight` and

~/anaconda3/envs/python36/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (2832, 1, 1)

is there anyone know the reason ?

Thanks !

mikkokotila commented 5 years ago

def myModel(train_X, test_X, train_y, test_y, params):

has to be

def myModel(train_X, train_y, test_X, test_y, params):

Feel free to open a new issue if anything.

stevexxs commented 5 years ago

Thanks, it works, but another error 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'
jtlz2 commented 4 years ago

@stevexxs Try 'val_accuracy' in place of 'val_acc'