-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_model_mlp_softmax.py
More file actions
68 lines (59 loc) · 1.9 KB
/
_model_mlp_softmax.py
File metadata and controls
68 lines (59 loc) · 1.9 KB
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
from torch import nn
from _model_mlp_base import MLPBaseClassifier
class MLPModel(nn.Module):
def __init__(
self,
n_hidden_layers,
n_hidden_units,
input_shape,
num_classes,
):
super().__init__()
self.flatten = nn.Flatten()
self.activation = nn.Sigmoid()
self.hidden_first = nn.Linear(input_shape, n_hidden_units)
self.hidden_layers = None
if n_hidden_layers - 1 > 0:
self.hidden_layers = nn.ModuleList(
[
nn.Linear(n_hidden_units, n_hidden_units)
for _ in range(n_hidden_layers - 1)
]
)
self.classification = nn.Linear(n_hidden_units, num_classes)
self.output = nn.Softmax(dim=-1)
def forward(self, x):
x = self.flatten(x)
h = self.activation(self.hidden_first(x))
if self.hidden_layers is not None:
for hidden_layer in self.hidden_layers:
h = self.activation(hidden_layer(h))
classification = self.classification(h)
out = self.output(classification)
return out
class MLPClassifier(MLPBaseClassifier):
def __init__(
self,
n_hidden_layers=1,
n_hidden_units=4,
learning_rate=1e-3,
class_weights=None,
max_iter=100,
random_state=None,
):
super().__init__(
n_hidden_layers=n_hidden_layers,
n_hidden_units=n_hidden_units,
learning_rate=learning_rate,
class_weights=class_weights,
max_iter=max_iter,
random_state=random_state,
)
def _setup_model(self):
self.model = MLPModel(
n_hidden_layers=self.n_hidden_layers,
n_hidden_units=self.n_hidden_units,
input_shape=self.input_shape,
num_classes=self.num_classes,
)
return self