-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
178 lines (146 loc) · 5.11 KB
/
utilities.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.utils.data as data_utils
import torchvision
import torchvision.transforms
from torch.nn.init import calculate_gain
def weights_init_1d(m):
classname = m.__class__.__name__
if isinstance(m, nn.BatchNorm1d):
m.weight.data.normal_(0, 1)
m.bias.data.fill_(0)
elif isinstance(m, nn.Linear):
nn.init.xavier_uniform(m.weight.data, gain=calculate_gain('relu'))
nn.init.constant(m.bias.data, 0)
def weights_init_2d(m):
classname = m.__class__.__name__
if isinstance(m, nn.Conv2d):
nn.init.kaiming_uniform(m.weight.data, mode='fan_in')
nn.init.constant(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.normal_(0, 1)
m.bias.data.fill_(0)
def set_parameters(model,param,factor):
for name, p in model.named_parameters():
if param in name:
p.data = factor * p.data
def L2_weights_norm(model):
weights = []
for name, parameter in model.named_parameters():
if 'weight' in name:
weights.append(parameter)
return (torch.norm(weights[0]).data[0] + torch.norm(weights[1]).data[0])**2
def adjust_lr(optimizer,lr, epoch, total_epochs):
lr = lr * (0.36 ** (epoch / float(total_epochs)))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def N_prediction(data_batch,model,N):
softmax = nn.Softmax(dim = 1)
correct = 0
correct2 = 0
total = 0
for x,y in data_batch:
out = 0
out2 = 0
x,y = model.input_shape(x,y)
for i in range(N):
out += model(x)
out2 += softmax(out)
out = softmax(out/N)
out2 = out/N
_, pred = torch.max(out.data, 1)
_, pred2 = torch.max(out2.data, 1)
pred = Variable(pred)
pred2 = Variable(pred2)
correct += float((pred == y).sum())
correct2 += float((pred2 == y).sum())
total += float(y.size(0))
return 100*correct/total, total - correct, 100*correct2/total, total - correct2
def N_prediction_2(data_batch,model,N):
softmax = nn.Softmax(dim = 1)
correct = 0
total = 0
for x,y in data_batch:
out = 0
x,y = model.input_shape(x,y)
for i in range(N):
out += softmax(model(x))
out = out/N
_, pred = torch.max(out, 1)
correct += float((pred == y).sum())
total += float(y.size(0))
return 100*correct/total, total - correct
def prediction(data_batch,model):
softmax = nn.Softmax(dim = 1)
correct = 0
total = 0
for x, y in data_batch:
x,y = model.input_shape(x,y)
out = softmax(model(x))
_, pred = torch.max(out.data, 1)
pred = Variable(pred)
correct += float((pred == y).sum())
total += float(y.size(0))
return 100*correct/total, total - correct
def prediction_conv(data_batch, model, cuda):
softmax = nn.Softmax(dim = 1)
correct = 0
total = 0
for x, y in data_batch:
x = Variable(x[:, None, :, :])
y = Variable(y)
if cuda:
x, y = x.cuda(), y.cuda()
out = softmax(model(x))
_, pred = torch.max(out.data, 1)
pred = Variable(pred)
correct += float((pred == y).sum())
total += float(y.size(0))
return 100*correct/total, total - correct
def batch_accuracy(data_batch,model, cuda):
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(data_batch):
targets = targets.squeeze()
if cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs), Variable(targets)
outputs = model(inputs)
total += targets.size(0)
_, pred = torch.max(outputs.data, 1)
pred = Variable(pred)
correct += float((pred == targets).sum())
return 100*correct/total, total - correct
def batch_loss(data_batch,model,criterion):
loss = 0
total = 0
for batch_idx, (images,labels) in enumerate(data_batch):
# for images, labels in data_batch:
images = Variable(images.view(-1, 28*28))
labels = Variable(labels)
outputs = model(images)
loss += criterion(outputs, labels)
total += labels.size(0)
return loss.data[0]/total
def linear_ini(LL,initialization):
'''
inputs : linear layer (LL) and the initialization
output : linear layer with the chosen initialization
'''
if initialization == 'zero':
LL.weight.data = nn.init.constant(LL.weight.data, 0)
LL.bias.data = nn.init.constant(LL.bias.data, 0)
if initialization == 'normal':
LL.weight.data = nn.init.normal(LL.weight.data, 0,1)
LL.bias.data = nn.init.normal(LL.bias.data, 0,1)
if initialization == 'glorot':
LL.weight.data = nn.init.xavier_uniform(LL.weight.data, gain=1)
# that is important, see paper.
LL.bias.data = nn.init.constant(LL.bias.data, 0)
if initialization == 'default':
pass
return LL