ray-project / tune-sklearn

A drop-in replacement for Scikit-Learn’s GridSearchCV / RandomizedSearchCV -- but with cutting edge hyperparameter tuning techniques.
https://docs.ray.io/en/master/tune/api_docs/sklearn.html
Apache License 2.0
465 stars 52 forks source link

[Question] skorch & TuneSearhCV (hyperopt) has some error #149

Closed sungreong closed 3 years ago

sungreong commented 3 years ago
class RegressorModule(nn.Module):
    def __init__(self,num_units_1=80,num_units_2 = 60, num_units_3 = 20, nonlin=nn.ReLU()):
        super(RegressorModule, self).__init__()
        print(num_units_1, num_units_2, num_units_3)
        self.nonlin = nonlin
        self.dropout = nn.Dropout(0.1)
        self.dense0 = nn.Linear(input_size, num_units_1)
        self.dense1 = nn.Linear(num_units_1, num_units_2)
        self.dense2 = nn.Linear(num_units_2, num_units_3)
        self.output = nn.Linear(num_units_3, 1)

    def forward(self, X):
        X = self.nonlin(self.dense0(X))
        X = self.nonlin(self.dense1(X))
        X = self.nonlin(self.dense2(X))
        return X

net_regr = NeuralNetRegressor(
    RegressorModule,
    criterion= nn.MSELoss,
    max_epochs=100000
)

param_space = {
    "module__num_units_1": hp.uniformint("module__num_units_1", 100, 150),
    "module__num_units_2": hp.uniformint("module__num_units_2", 50, 98),
    "module__num_units_3": hp.uniformint("module__num_units_3", 20, 40)
}

random = TuneSearchCV(
    net_regr,
    param_space,
    search_optimization="hyperopt",
    early_stopping=False,
    n_trials=3,
    max_iters=10)

Error


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-54-ac10e36520a8> in <module>
     11     n_trials=3,
     12     max_iters=10)
---> 13 random.fit(train_x_np, train_y_np)
     14 print(random.cv_results_)

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/tune_sklearn/tune_basesearch.py in fit(self, X, y, groups, **fit_params)
    470                                     "To show process output, set verbose=2.")
    471 
--> 472             result = self._fit(X, y, groups, **fit_params)
    473 
    474             if not ray_init and ray.is_initialized():

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/tune_sklearn/tune_basesearch.py in _fit(self, X, y, groups, **fit_params)
    419             self.best_estimator_ = clone(self.estimator)
    420             self.best_estimator_.set_params(**self.best_params)
--> 421             self.best_estimator_.fit(X, y, **fit_params)
    422 
    423             best_result = analysis.get_best_trial(

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/regressor.py in fit(self, X, y, **fit_params)
     89         # this is actually a pylint bug:
     90         # https://github.com/PyCQA/pylint/issues/1085
---> 91         return super(NeuralNetRegressor, self).fit(X, y, **fit_params)

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/net.py in fit(self, X, y, **fit_params)
    901             self.initialize()
    902 
--> 903         self.partial_fit(X, y, **fit_params)
    904         return self
    905 

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/net.py in partial_fit(self, X, y, classes, **fit_params)
    860         self.notify('on_train_begin', X=X, y=y)
    861         try:
--> 862             self.fit_loop(X, y, **fit_params)
    863         except KeyboardInterrupt:
    864             pass

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/net.py in fit_loop(self, X, y, epochs, **fit_params)
    774 
    775             self.run_single_epoch(dataset_train, training=True, prefix="train",
--> 776                                   step_fn=self.train_step, **fit_params)
    777 
    778             if dataset_valid is not None:

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/net.py in run_single_epoch(self, dataset, training, prefix, step_fn, **fit_params)
    810             yi_res = yi if not is_placeholder_y else None
    811             self.notify("on_batch_begin", X=Xi, y=yi_res, training=training)
--> 812             step = step_fn(Xi, yi, **fit_params)
    813             self.history.record_batch(prefix + "_loss", step["loss"].item())
    814             self.history.record_batch(prefix + "_batch_size", get_len(Xi))

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/skorch/net.py in train_step(self, Xi, yi, **fit_params)
    707             return step['loss']
    708 
--> 709         self.optimizer_.step(step_fn)
    710         return step_accumulator.get_step()
    711 

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
     24         def decorate_context(*args, **kwargs):
     25             with self.__class__():
---> 26                 return func(*args, **kwargs)
     27         return cast(F, decorate_context)
     28 

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/optim/adam.py in step(self, closure)
    117                    group['lr'],
    118                    group['weight_decay'],
--> 119                    group['eps']
    120                    )
    121         return loss

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/optim/functional.py in adam(params, grads, exp_avgs, exp_avg_sqs, max_exp_avg_sqs, state_steps, amsgrad, beta1, beta2, lr, weight_decay, eps)
     84 
     85         # Decay the first and second moment running average coefficient
---> 86         exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
     87         exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
     88         if amsgrad:

RuntimeError: The size of tensor a (80) must match the size of tensor b (145) at non-singleton dimension 0
richardliaw commented 3 years ago

@sungreong can you provide the train_x_np too?

richardliaw commented 3 years ago

Hmm, unfortunately I cannot repro because I need train_x_np. I'll close this for now.