forked from marctom/elrgvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenet_300_100.py
77 lines (70 loc) · 2.35 KB
/
lenet_300_100.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from bias_transfer.models.elrg.linear import ELRGLinear
from nntransfer.models.utils import concatenate_flattened
class LeNet300100(nn.Module):
def __init__(
self,
num_classes: int = 10,
input_height: int = 28,
input_width: int = 28,
input_channels: int = 1,
dropout: float = 0.0,
rank: int = 1,
alpha: float = None,
train_var: bool = True,
initial_var: float = 1e-12,
):
super(LeNet300100, self).__init__()
self.rank = rank
self.alpha = alpha if alpha is not None else 1 / rank
self.input_size = (input_height, input_width)
self.flat_input_size = input_width * input_height * input_channels
self.fc1 = ELRGLinear(
self.flat_input_size,
300,
rank=rank,
alpha=self.alpha,
train_var=train_var,
initial_posterior_var=initial_var,
)
self.fc2 = ELRGLinear(
300,
100,
rank=rank,
alpha=self.alpha,
train_var=train_var,
initial_posterior_var=initial_var,
)
self.fc3 = ELRGLinear(
100,
num_classes,
rank=rank,
alpha=self.alpha,
train_var=train_var,
initial_posterior_var=initial_var,
)
self.dropout = nn.Dropout(p=dropout) if dropout else None
def forward(self, x, num_samples=1):
x = x.view(x.size(0), self.flat_input_size)
y = []
for s in range(num_samples):
z = F.relu(self.fc1(x))
z = self.dropout(z) if self.dropout else z
z = F.relu(self.fc2(z))
z = self.dropout(z) if self.dropout else z
y.append(self.fc3(z))
return torch.cat(y)
def get_parameters(self, name, keep_first_dim=False):
return concatenate_flattened(
[
self.fc1._parameters.get(f"w_{name}"),
self.fc2._parameters.get(f"w_{name}"),
self.fc3._parameters.get(f"w_{name}"),
self.fc1._parameters.get(f"b_{name}"),
self.fc2._parameters.get(f"b_{name}"),
self.fc3._parameters.get(f"b_{name}"),
],
keep_first_dim=keep_first_dim,
)