-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffn_baseline.py
79 lines (65 loc) · 2.34 KB
/
ffn_baseline.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
from __future__ import annotations
import torch
import torch.nn as nn
class FeedforwardNeuralNetModel(nn.Module):
"""
Feed-forward Neural Network model with ReLU activation layers for
classification.
"""
def __init__(
self,
input_dim: int,
hidden_dim: list[int] | tuple[int] | int,
output_dim: int,
dropout_rate: float,
):
"""
Feed-forward Neural Network model with ReLU activation layers for
classification.
Parameters
----------
input_dim : int
Dimension of input layer.
hidden_dim : list[int] | tuple[int] | int
Dimension of the hidden layers in the FFN.
output_dim : int
Dimension of output layer.
dropout_rate : float
Probability of dropout.
"""
super().__init__()
if type(hidden_dim) == int:
hidden_dim = [hidden_dim]
self.hidden_dim = hidden_dim
# FFN: input layer
self.input_layer = nn.Linear(input_dim, self.hidden_dim[0])
self.relu = nn.ReLU()
self.dropout = nn.Dropout(dropout_rate)
input_dim = self.hidden_dim[0]
# FFN: hidden layers
self.linear_layers = []
self.non_linear_layers = []
self.dropout_layers = []
for layer in range(1, len(self.hidden_dim)):
self.linear_layers.append(nn.Linear(input_dim, self.hidden_dim[layer]))
self.non_linear_layers.append(nn.ReLU())
self.dropout_layers.append(nn.Dropout(dropout_rate))
input_dim = self.hidden_dim[layer]
self.linear_layers = nn.ModuleList(self.linear_layers)
self.non_linear_layers = nn.ModuleList(self.non_linear_layers)
self.dropout_layers = nn.ModuleList(self.dropout_layers)
# FFN: readout
self.final_layer = nn.Linear(input_dim, output_dim)
def forward(self, x: torch.Tensor):
# FFN: input layer
out = self.input_layer(x)
out = self.relu(out)
out = self.dropout(out)
# FFN: hidden layers
for layer in range(len(self.linear_layers)):
out = self.linear_layers[layer](out)
out = self.non_linear_layers[layer](out)
out = self.dropout_layers[layer](out)
# FFN: readout
out = self.final_layer(out)
return out