Closed brunomorampc closed 4 years ago
Yes, skorch just passes the module's output to get_loss
and, subsequently, to the criterion. You have several options:
get_loss
that extracts the prediction and discards the context / hidden stateget_loss
/ the criterion :)I think (1) is the easiest. Example:
class ContextlessMSE(torch.nn.MSELoss):
def forward(self, y_pred, y_true):
y, (h, c) = y_pred # extract prediction and context information
return super().forward(y, y_true)
net_regr = NeuralNetRegressor(
module=MyLSTM,
module__ni=ni,
module__no=no,
module__nh=nh,
module__nlayers=nlayers,
max_epochs=20,
lr=0.1,
criterion=ContextlessMSE,
)
Additional comments:
forward()
: don't use a global device
variable when you can use device=x.device
if train: ...
to disable dropout, it is automatically skipped during inferencegreat! it works thanks!
OK, great this solution works for you. I'm closing this issue, feel free to re-open it, if this should not be in your interest :)
Another maybe relate question. Now I want to use GridSearchCV
. I got a similar error AttributeError: 'tuple' object has no attribute 'to'
so, I did the same thing you suggested, but with a custom loss function that sklearn accepts:
from sklearn.metrics import mean_squared_error, make_scorer
def my_custom_loss(y_true, y_pred):
y, (h, c) = y_pred
return mean_squared_error(y, y_true)
and then used this one as score:
loss = make_scorer(my_custom_loss, greater_is_better=False)
# Load toy regressor
X_regr, y_regr = make_regression(1000, 20, n_informative=10, random_state=0)
X_regr = X_regr.astype(np.float32)
y_regr = y_regr.astype(np.float32) / 100
y_regr = y_regr.reshape(-1, 1)
ni = 20
no = 1
nh = 10
nlayers = 3
net_regr = NeuralNetRegressor(
module=MyLSTM,
module__ni=ni,
module__no=no,
module__nh=nh,
module__nlayers=nlayers,
max_epochs=20,
lr=0.1,
criterion=ContextlessMSE,
iterator_train__shuffle=True, # this shuffles the training data
# device='cuda', # uncomment this to train with CUDA
)
params = {
'lr': [0.1, 0.2],
'max_epochs': [10, 20],
'module__nh': [10, 20],
}
gs = GridSearchCV(net_regr, params, refit=False, cv=2, scoring=loss)
gs.fit(X_regr, y_regr)
print(gs.best_score_, gs.best_params_)
but I still get the same error AttributeError: 'tuple' object has no attribute 'to'
I am testing Skorch with LSTM cells for a regression problem. Here is the code
where
MyLSTM
iswhen I try to run I get this error:
I am using Python 3.6, torch 1.4.0 and skorch 0.8.
EDIT: I guess that the problem comes from the fact that lstm takes a tuple (h,c) with the hidden states... is there a way to solve this?