-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_flows.py
133 lines (95 loc) · 4.99 KB
/
test_flows.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
# inspired by https://github.com/ikostrikov/pytorch-flows/blob/master/flow_test.py
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.utils.weight_norm as wn
import numpy as np
import unittest
import pdb
from invertible_layers import *
EPS = 1e-5
BATCH_SIZE = 17
NUM_CHANNELS = 64
H = 32
W = 48
class TestFlow(unittest.TestCase):
def test_shuffle(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = Shuffle(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'Shuffle Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Shuffle Layer is wrong')
def test_reverse(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = Reverse(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'Shuffle Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Shuffle Layer is wrong')
def test_conv(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = Invertible1x1Conv(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'Conv Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Conv Layer is wrong')
self.assertTrue((log_det - inv_log_det).abs().max() > 0.01 * EPS, 'Determinant was not changed!')
def test_squeeze(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = Squeeze([int(y) for y in x.size()])
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'Squeeze Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Squeeze Layer is wrong')
def test_split(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = Split([int(y) for y in x.size()])
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone(), use_stored_sample=True)
self.assertTrue((log_det_ - log_det).abs().max() < 1e-2,
'Squeeze Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Squeeze Layer is wrong')
self.assertTrue((log_det - inv_log_det).abs().max() > EPS, 'Determinant was not changed!')
def test_add(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = AdditiveCoupling(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'Additive Coupling Layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'Additive Coupling Layer is wrong')
def test_affine(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = AffineCoupling(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
# import pdb; pdb.set_trace()
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < 1e-3,
'affine coupling layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'affine coupling layer is wrong')
self.assertTrue((log_det - inv_log_det).abs().max() > EPS, 'determinant was not changed!')
def test_actnorm(self):
x = torch.randn(BATCH_SIZE, NUM_CHANNELS, H, W)
layer = ActNorm(NUM_CHANNELS)
log_det = torch.randn(BATCH_SIZE)
# import pdb; pdb.set_trace()
y, inv_log_det = layer.forward_(x.clone(), log_det.clone())
x_, log_det_ = layer.reverse_(y.clone(), inv_log_det.clone())
self.assertTrue((log_det_ - log_det).abs().max() < EPS,
'actnorm layer det is not zero.')
self.assertTrue((x - x_).abs().max() < EPS, 'actnorm layer is wrong')
self.assertTrue((log_det - inv_log_det).abs().max() > EPS, 'determinant was not changed!')
if __name__ == '__main__':
unittest.main()