/
regressor.py
91 lines (72 loc) · 2.92 KB
/
regressor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""NeuralNet subclasses for regression tasks."""
import re
from sklearn.base import RegressorMixin
import torch
from torch.utils.data import DataLoader
from skorch import NeuralNet
from skorch.utils import get_dim
from skorch.utils import is_dataset
neural_net_reg_doc_start = """NeuralNet for regression tasks
Use this specifically if you have a standard regression task,
with input data X and target y. y must be 2d.
"""
neural_net_reg_criterion_text = """
criterion : torch criterion (class, default=torch.nn.MSELoss)
Mean squared error loss."""
def get_neural_net_reg_doc(doc):
doc = neural_net_reg_doc_start + " " + doc.split("\n ", 4)[-1]
pattern = re.compile(r'(\n\s+)(criterion .*\n)(\s.+){1,99}')
start, end = pattern.search(doc).span()
doc = doc[:start] + neural_net_reg_criterion_text + doc[end:]
return doc
# pylint: disable=missing-docstring
class NeuralNetRegressor(NeuralNet, RegressorMixin):
__doc__ = get_neural_net_reg_doc(NeuralNet.__doc__)
def __init__(
self,
module,
*args,
criterion=torch.nn.MSELoss,
**kwargs
):
super(NeuralNetRegressor, self).__init__(
module,
*args,
criterion=criterion,
**kwargs
)
# pylint: disable=signature-differs
def check_data(self, X, y):
if (
(y is None) and
(not is_dataset(X)) and
(self.iterator_train is DataLoader)
):
raise ValueError("No y-values are given (y=None). You must "
"implement your own DataLoader for training "
"(and your validation) and supply it using the "
"``iterator_train`` and ``iterator_valid`` "
"parameters respectively.")
if y is None:
# The user implements its own mechanism for generating y.
return
if get_dim(y) == 1:
msg = (
"The target data shouldn't be 1-dimensional but instead have "
"2 dimensions, with the second dimension having the same size "
"as the number of regression targets (usually 1). Please "
"reshape your target data to be 2-dimensional "
"(e.g. y = y.reshape(-1, 1).")
raise ValueError(msg)
# pylint: disable=signature-differs
def fit(self, X, y, **fit_params):
"""See ``NeuralNet.fit``.
In contrast to ``NeuralNet.fit``, ``y`` is non-optional to
avoid mistakenly forgetting about ``y``. However, ``y`` can be
set to ``None`` in case it is derived dynamically from
``X``.
"""
# pylint: disable=useless-super-delegation
# this is actually a pylint bug:
# https://github.com/PyCQA/pylint/issues/1085
return super(NeuralNetRegressor, self).fit(X, y, **fit_params)