-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctional.py
More file actions
129 lines (88 loc) · 2.79 KB
/
functional.py
File metadata and controls
129 lines (88 loc) · 2.79 KB
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
import torch
import numpy as np
import torch.nn.functional as F
def matrix_log(B, terms=10):
assert B.size(0) == B.size(1)
I = torch.eye(B.size(0))
B_min_I = B - I
# for k = 1.
product = B_min_I
result = B_min_I
is_minus = -1
for k in range(2, terms):
# Reweighing with k term.
product = torch.matmul(product, B_min_I) * (k - 1) / k
result = result + is_minus * product
is_minus *= -1
return result
def matrix_exp(M, terms=10):
assert M.size(0) == M.size(1)
I = torch.eye(M.size(0))
# for i = 0.
result = I
product = I
for i in range(1, terms + 1):
product = torch.matmul(product, M) / i
result = result + product
return result
def conv_exp(input, kernel, terms=10, dynamic_truncation=0, verbose=False):
B, C, H, W = input.size()
assert kernel.size(0) == kernel.size(1)
assert kernel.size(0) == C, '{} != {}'.format(kernel.size(0), C)
padding = (kernel.size(2) - 1) // 2, (kernel.size(3) - 1) // 2
result = input
product = input
for i in range(1, terms + 1):
product = F.conv2d(product, kernel, padding=padding, stride=(1, 1)) / i
result = result + product
if dynamic_truncation != 0 and i > 5:
if product.abs().max().item() < dynamic_truncation:
break
if verbose:
print(
'Maximum element size in term: {}'.format(
torch.max(torch.abs(product))))
return result
def inv_conv_exp(input, kernel, terms=10, dynamic_truncation=0, verbose=False):
return conv_exp(input, -kernel, terms, dynamic_truncation, verbose)
def log_det(kernel):
Cout, C, K1, K2 = kernel.size()
assert Cout == C
M1 = (K1 - 1) // 2
M2 = (K2 - 1) // 2
diagonal = kernel[torch.arange(C), torch.arange(C), M1, M2]
trace = torch.sum(diagonal)
return trace
def convergence_scale(c, kernel_size):
C_out, C_in, K1, K2 = kernel_size
d = C_in * K1 * K2
return c / d
# def power_iteration(v, u, kernel)
if __name__ == '__main__':
x = np.random.randn(2, 2)
I = torch.eye(2)
q = np.linalg.qr(x)[0].astype('float32')
q = torch.from_numpy(q)
print(torch.matmul(q, q.t()))
W = I + q
log_W = matrix_log(W, terms=10000)
print(log_W)
W_recon = matrix_exp(log_W, terms=100)
print(W - W_recon)
# x = torch.randn(127, 12, 16, 16)
#
# kernel = torch.randn(12, 12, 3, 3)
#
# allowed_range = convergence_scale(c=2., kernel_size=kernel.size())
#
# print('Allowed range:', allowed_range)
#
# kernel = torch.tanh(kernel) * allowed_range
#
# terms = 10
#
# z = conv_exp(x, kernel, terms=terms)
#
# x_recon = inv_conv_exp(z, kernel, terms=terms)
#
# print(torch.mean(torch.abs(x - x_recon)))