0

我正在使用 Keras Tuner 来寻找神经网络的最佳超参数。

params = ['batch_size', 'epochs', 'drop_rate', 'learning_rate', 'num_layers','units_1','units_2','units_3','units_4', 'units_5']
params_nn = pd.DataFrame(np.zeros((len(params), len(commodities))), index = params, columns = commodities)


def build_model(hp):
    
    drop_rate = hp.Choice('drop_rate', values = [0.0, 0.2, 0.4])
    
    model_full = Sequential()
    for i in range(hp.Int('num_layers', 2, 5)):
        model_full.add(keras.layers.Dense(units = hp.Int('units_'+str(i+1),
                                            min_value=3,
                                            max_value=24,
                                            step=3), activation="relu",
                            input_shape=(1, x_nn_train[0].shape[2])))
        model_full.add(keras.layers.Dropout(drop_rate))
    
    model_full.add(keras.layers.Dense(1))

    model_full.compile(loss='huber_loss', 
                    optimizer = keras.optimizers.Adam(hp.Choice('learning_rate',
                                                            values = [1e-2, 1e-3, 1e-4])))
    return model_full

bayesian_opt_tuner = MyTuner(
    build_model,
    objective = 'val_loss',
    max_trials=70,
    executions_per_trial=3)

bayesian_opt_tuner.search(x_nn_train[0], y_nn_train[0],  
                            validation_split=0.2,verbose=1)
    
best_params = bayesian_opt_tuner.get_best_hyperparameters(1)[0]

该代码在我的 PC 上运行顺畅,但最近我已切换到 AWS 以加快该过程。当我使用 AWS 运行此代码时,经过 30-40 次试验后,我不断收到以下错误:

Trial 39 Complete [00h 00m 03s]
val_loss: 0.005517204757779837

Best val_loss So Far: 0.004489939038952191
Total elapsed time: 00h 07m 48s
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-9e7dd2527b22> in <module>
     35     overwrite = True)
     36 
---> 37 bayesian_opt_tuner.search(x_nn_train[0], y_nn_train[0],  
     38                             validation_split=0.2,verbose=1)
     39 

~/project/graduation/lib/python3.8/site-packages/kerastuner/engine/base_tuner.py in search(self, *fit_args, **fit_kwargs)
    119         self.on_search_begin()
    120         while True:
--> 121             trial = self.oracle.create_trial(self.tuner_id)
    122             if trial.status == trial_module.TrialStatus.STOPPED:
    123                 # Oracle triggered exit.

~/project/graduation/lib/python3.8/site-packages/kerastuner/engine/oracle.py in create_trial(self, tuner_id)
    163             values = None
    164         else:
--> 165             response = self._populate_space(trial_id)
    166             status = response['status']
    167             values = response['values'] if 'values' in response else None

~/project/graduation/lib/python3.8/site-packages/kerastuner/tuners/bayesian.py in _populate_space(self, trial_id)
    103         x, y = self._vectorize_trials()
    104         try:
--> 105             self.gpr.fit(x, y)
    106         except exceptions.ConvergenceWarning:
    107             # If convergence of the GPR fails, create a random trial.

~/project/graduation/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py in fit(self, X, y)
    243                         self._rng.uniform(bounds[:, 0], bounds[:, 1])
    244                     optima.append(
--> 245                         self._constrained_optimization(obj_func, theta_initial,
    246                                                        bounds))
    247             # Select result from run with minimal (negative) log-marginal

~/project/graduation/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py in _constrained_optimization(self, obj_func, initial_theta, bounds)
    492                 obj_func, initial_theta, method="L-BFGS-B", jac=True,
    493                 bounds=bounds)
--> 494             _check_optimize_result("lbfgs", opt_res)
    495             theta_opt, func_min = opt_res.x, opt_res.fun
    496         elif callable(self.optimizer):

~/project/graduation/lib/python3.8/site-packages/sklearn/utils/optimize.py in _check_optimize_result(solver, result, max_iter, extra_warning_msg)
    241                 "    https://scikit-learn.org/stable/modules/"
    242                 "preprocessing.html"
--> 243             ).format(solver, result.status, result.message.decode("latin1"))
    244             if extra_warning_msg is not None:
    245                 warning_msg += "\n" + extra_warning_msg

AttributeError: 'str' object has no attribute 'decode'

您能否告诉我可能是什么问题以及如何解决?我搜索了这个问题,许多人建议将 h5py 设置为 2.10.0 版;然而,这并没有帮助。

4

0 回答 0