forked from suriyadeepan/torchtest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.py
109 lines (86 loc) · 2.1 KB
/
examples.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchtest as tt
"""
[1] Variables Change
"""
inputs = Variable(torch.randn(20, 20))
targets = Variable(torch.randint(0, 2, (20,))).long()
batch = [inputs, targets]
model = nn.Linear(20, 2)
# what are the variables?
print('Our list of parameters', [ np[0] for np in model.named_parameters() ])
# do they change after a training step?
# let's run a train step and see
tt.assert_vars_change(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(model.parameters()),
batch=batch)
# let's try to break this, so the test fails
params_to_train = [ np[1] for np in model.named_parameters() if np[0] is not 'bias' ]
# run test now
""" FAILURE
tt.assert_vars_change(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(params_to_train),
batch=batch)
"""
# YES! bias did not change
# What if bias is not supposed to change, by design?
"""
[2] Variables Don't Change
"""
# test to see if bias remains the same after training
tt.assert_vars_same(
model=model,
loss_fn=F.cross_entropy,
optim=torch.optim.Adam(params_to_train),
batch=batch,
params=[('bias', model.bias)]
)
# it does? good. let's move on
"""
[3] Output Range
"""
# we are keeping the bias fixed for a reason
optim = torch.optim.Adam(params_to_train)
loss_fn=F.cross_entropy
tt.test_suite(model, loss_fn, optim, batch,
output_range=(-2, 2),
test_output_range=True
)
# seems to work
# let's tweak the model to fail the test
model.bias = nn.Parameter(2 + torch.randn(2, ))
"""FAILURE
tt.test_suite(
model,
loss_fn, optim, batch,
output_range=(-1, 1),
test_output_range=True
)
"""
# as expected, it fails; yay!
"""FAILURE
[4] NaN
model.bias = nn.Parameter(float('NaN') * torch.randn(2, ))
tt.test_suite(
model,
loss_fn, optim, batch,
test_nan_vals=True
)
"""
# okay, then
""""FAILURE
[4] Inf
model.bias = nn.Parameter(float('Inf') * torch.randn(2, ))
tt.test_suite(
model,
loss_fn, optim, batch,
test_inf_vals=True
)
"""