-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
116 lines (105 loc) · 4.23 KB
/
models.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import torch
import torch.nn as nn
import torch.nn.functional as F
from metadatanorm import MetadataNorm
class BaselineNet(nn.Module):
def __init__(self):
""" Baseline CNN model with 2 convolutional layers and 2 linear layers. """
super(BaselineNet, self).__init__()
self.conv1 = nn.Conv2d(1, 16, 5)
self.conv2 = nn.Conv2d(16, 32, 5)
self.fc1 = nn.Linear(18432, 84)
self.fc2 = nn.Linear(84, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = x.view(-1, 18432)
x = self.fc1(x)
fc = x.cpu().detach().numpy()
x = F.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x, fc
class MDN_Linear(nn.Module):
def __init__(self, dataset_size, batch_size, kernel):
""" MDN-Linear model: Baseline CNN model with 2 convolutional and 2 linear layers with MDN applied
to the last linear layer before the output layer.
Args:
dataset_size (int): size of dataset
batch_size (int): batch size
kernel (2d vector): precalculated kernel for MDN based on the vector X of confounders (X^TX)^-1.
kernel needs to be set before training, and cfs needs to be set during training for each batch.
"""
super(MDN_Linear, self).__init__()
self.N = batch_size
self.C = kernel.shape[0]
self.kernel = kernel
self.cfs = nn.Parameter(torch.randn(batch_size, self.C), requires_grad=False)
self.dataset_size = dataset_size
# Convolutional and MDN layers
self.conv1 = nn.Conv2d(1, 16, 5)
self.conv2 = nn.Conv2d(16, 32, 5)
self.fc1 = nn.Linear(18432, 84)
self.metadatanorm = MetadataNorm(self.N, self.kernel, self.dataset_size, 84)
self.fc2 = nn.Linear(84, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = x.view(-1, 18432)
x = self.fc1(x)
self.metadatanorm.cfs = self.cfs
x = self.metadatanorm(x)
fc = x.cpu().detach().numpy()
x = F.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x, fc
class MDN_Conv(nn.Module):
def __init__(self, dataset_size, batch_size, kernel):
""" MDN-Conv model: Baseline CNN model with 2 convolutional and 2 linear layers with MDN applied
to every convolutional layer and the last linear layer before the output layer.
Args:
dataset_size (int): size of dataset
batch_size (int): batch size
kernel (2d vector): precalculated kernel for MDN based on the vector X of confounders (X^TX)^-1.
kernel needs to be set before training, and cfs needs to be set during training for each batch.
"""
super(MDN_Conv, self).__init__()
self.N = batch_size
self.C = kernel.shape[0]
self.kernel = kernel
self.cfs = nn.Parameter(torch.randn(batch_size, self.C), requires_grad=False)
self.dataset_size = dataset_size
# Convolutional and MDN layers
self.conv1 = nn.Conv2d(1, 16, 5)
self.metadatanorm1 = MetadataNorm(self.N, self.kernel, self.dataset_size, 16*28*28)
self.conv2 = nn.Conv2d(16, 32, 5)
self.metadatanorm2 = MetadataNorm(self.N, self.kernel, self.dataset_size, 32*24*24)
self.fc1 = nn.Linear(18432, 84)
self.metadatanorm3 = MetadataNorm(self.N, self.kernel, self.dataset_size, 84)
self.fc2 = nn.Linear(84, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
self.metadatanorm1.cfs = self.cfs
x = self.metadatanorm1(x)
x = F.relu(x)
x = self.conv2(x)
self.metadatanorm2.cfs = self.cfs
x = self.metadatanorm2(x)
x = F.relu(x)
x = x.view(-1, 18432)
x = self.fc1(x)
self.metadatanorm3.cfs = self.cfs
x = self.metadatanorm3(x)
fc = x.cpu().detach().numpy()
x = F.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x, fc