forked from HIPS/autograd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fft.py
159 lines (137 loc) · 4.3 KB
/
test_fft.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
from __future__ import absolute_import
import autograd.numpy as np
import autograd.numpy.random as npr
from autograd.util import *
from autograd import grad
npr.seed(1)
def test_fft():
def fun(x): return to_scalar(np.fft.fft(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fft_axis():
def fun(x): return to_scalar(np.fft.fft(x, axis=0))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fft_n_smaller():
D = 5
for fft_fun in (np.fft.fft, np.fft.ifft):
def fun(x): return to_scalar(fft_fun(x, D - 2))
d_fun = lambda x : to_scalar(grad(fun)(x))
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fft_n_bigger():
D = 5
for fft_fun in (np.fft.fft, np.fft.ifft):
def fun(x): return to_scalar(fft_fun(x, D + 2))
d_fun = lambda x : to_scalar(grad(fun)(x))
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def check_fft_s(fft_fun):
D = 5
def fun(x): return to_scalar(fft_fun(x, s=s, axes=axes))
d_fun = lambda x : to_scalar(grad(fun)(x))
mat = npr.randn(D,D,D) / 10.0
s = [D + 2, D - 2]
axes = [0,2]
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fft2_s(): check_fft_s(np.fft.fft2)
def test_ifft2_s(): check_fft_s(np.fft.ifft2)
def test_fftn_s(): check_fft_s(np.fft.fftn)
def test_ifftn_s(): check_fft_s(np.fft.ifftn)
## TODO: fft gradient not implemented for repeated axes
# def test_fft_repeated_axis():
# D = 5
# for fft_fun in (np.fft.fft2,np.fft.ifft2,np.fft.fftn, np.fft.ifftn):
# def fun(x): return to_scalar(fft_fun(x, s=s, axes=axes))
# d_fun = lambda x : to_scalar(grad(fun)(x))
# mat = npr.randn(D,D,D) / 10.0
# s = [D + 2, D - 2]
# axes = [0,0]
# check_grads(fun, mat)
# check_grads(d_fun, mat)
def test_ifft():
def fun(x): return to_scalar(np.fft.ifft(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fft2():
def fun(x): return to_scalar(np.fft.fft2(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D) / 10.0
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_ifft2():
def fun(x): return to_scalar(np.fft.ifft2(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fftn():
def fun(x): return to_scalar(np.fft.fftn(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D) / 10.0
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_ifftn():
def fun(x): return to_scalar(np.fft.ifftn(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fftshift():
def fun(x): return to_scalar(np.fft.fftshift(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D) / 10.0
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fftshift_even():
def fun(x): return to_scalar(np.fft.fftshift(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 4
mat = npr.randn(D, D) / 10.0
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_fftshift_axes():
def fun(x): return to_scalar(np.fft.fftshift(x, axes=1))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D) / 10.0
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_ifftshift():
def fun(x): return to_scalar(np.fft.ifftshift(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_ifftshift_even():
def fun(x): return to_scalar(np.fft.ifftshift(x))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 4
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)
def test_ifftshift_axes():
def fun(x): return to_scalar(np.fft.ifftshift(x, axes=1))
d_fun = lambda x : to_scalar(grad(fun)(x))
D = 5
mat = npr.randn(D, D)
check_grads(fun, mat)
check_grads(d_fun, mat)