I have the following model defined, that I would like to apply Hyperparameter tuning to. I want to use GridSearchCV and change the number of layers etc.
class Regressor(nn.Module):
def __init__(self, n_layers=3, n_features=10, activation=nn.ReLU):
super().__init__()
self.layers = []
self.activation_functions = []
for i in range(n_layers):
self.layers.append(nn.Linear(n_features, n_features))
self.activation_functions.append(activation())
self.add_module(f"layer{i}", self.layers[-1])
self.add_module(f"act{i}", self.activation_functions[-1])
self.output = nn.Linear(n_features, 1)
def forward(self, x):
for layer, act in zip(self.layers, self.activation_functions):
x=act(layer(x))
x = self.output(x)
return x
I have defined the Skorch NeuralNetRegressor as follows:
model = NeuralNetRegressor(
module=FatRegressor,
max_epochs=100,
batch_size=10,
module__n_layers=2,
criterion=nn.MSELoss,
)
print(model.initialize())
My parameter grid is
param_grid = {
'model__optimizer': [optim.Adam, optim.Adamax, optim.NAdam],
'model__max_epochs': list(range(30,40)), # Want to ramp between 10 and 100
'module__activation': [nn.Identity, nn.ReLU, nn.ELU, nn.ReLU6, nn.GELU, nn.Softplus, nn.Softsign, nn.Tanh,
nn.Sigmoid, nn.Hardsigmoid],
'model__batch_size': [10,12,15,20],
'model__n_layers': list(range(11,30)),
'model__lr': [0.0001, 0.0008, 0.009, 0.001, 0.002, 0.003, 0.004, 0.01],
}
When using the Pipeline:
pipeline = Pipeline(steps=[('scaler', StandardScaler()),
('model', NeuralNetRegressor(module=FatRegressor, device='cuda'))])
grid = GridSearchCV(
estimator = pipeline,
param_grid=param_grid,
n_jobs=-1,
cv=3,
error_score='raise',
return_train_score=True,
verbose=3
)
I get the following error:
Invalid parameter 'n_layers' for estimator <class 'skorch.regressor.NeuralNetRegressor'>[uninitialized](
module=<class '__main__.Regressor'>,
). Valid parameters are: ['module', 'criterion', 'optimizer', 'lr', 'max_epochs', 'batch_size', 'iterator_train', 'iterator_valid', 'dataset', 'train_split', 'callbacks', 'predict_nonlinearity', 'warm_start', 'verbose', 'device', 'compile', '_params_to_validate'].
Is there any way to use custom parameter names?