-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil_test.py
187 lines (159 loc) · 7.7 KB
/
util_test.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
179
180
181
182
183
184
185
186
187
import numpy as np
import tensorflow as tf
import util
def randn(shape, dtype='complex64'):
if np.issubdtype(dtype, np.complex):
real = np.random.normal(size=shape, scale=1 / (2 ** 0.5))
imag = np.random.normal(size=shape, scale=1 / (2 ** 0.5))
return (real + 1j * imag).astype(dtype)
else:
return np.random.normal(size=shape).astype(dtype)
class Test_util(tf.test.TestCase):
def test_dirac(self):
with self.test_session():
output = util.dirac([5])
truth = [0, 0, 1, 0, 0]
self.assertAllClose(output.eval(), truth)
with self.test_session():
output = util.dirac([4])
truth = [0, 0, 1, 0]
self.assertAllClose(output.eval(), truth)
def test_dot(self):
for dtype in ['float32', 'complex64']:
shape = [3, 4]
x1 = randn(shape, dtype)
x2 = randn(shape, dtype)
with self.test_session():
self.assertAllClose(np.vdot(x1, x2).real,
util.dot(x1, x2).eval())
def test_norm2(self):
shape = [3, 4]
x_ = randn(shape)
x = tf.constant(x_)
with self.test_session():
self.assertAllClose(np.linalg.norm(x_.ravel())**2,
util.norm2(x).eval())
def test_convolve(self):
with self.test_session():
l = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1, 0], dtype='float32').reshape([3, 1, 1])
r_ = tf.constant(r)
y_ = np.array([2, 3], dtype='float32').reshape([1, 2, 1])
y = util.convolve(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([1, 2], dtype='float32').reshape([1, 2, 1])
y = util.convolve(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_convolve_complex(self):
with self.test_session():
l = np.array([1j, 2j, 3j, 4j], dtype='complex64').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1j, 0], dtype='complex64').reshape([3, 1, 1])
r_ = tf.constant(r)
y_ = np.array([-2, -3], dtype='complex64').reshape([1, 2, 1])
y = util.convolve(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1j], dtype='complex64').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([-1, -2], dtype='complex64').reshape([1, 2, 1])
y = util.convolve(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_convolve_full(self):
with self.test_session():
l_ = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1])
l = tf.constant(l_)
r_ = np.array([0, 1, 0], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([0, 1, 2, 3, 4, 0], dtype='float32').reshape([1, 6, 1])
y = util.convolve(l, r, mode='full')
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([0, 0, 1, 2, 3, 4], dtype='float32').reshape([1, 6, 1])
y = util.convolve(l, r, mode='full')
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_correlate(self):
with self.test_session():
l = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1, 0], dtype='float32').reshape([3, 1, 1])
r_ = tf.constant(r)
y_ = np.array([2, 3], dtype='float32').reshape([1, 2, 1])
y = util.correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([3, 4], dtype='float32').reshape([1, 2, 1])
y = util.correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_correlate_complex(self):
with self.test_session():
l = np.array([1j, 2j, 3j, 4j], dtype='complex64').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1j, 0], dtype='complex64').reshape([3, 1, 1])
r_ = tf.constant(r)
y_ = np.array([-2, -3], dtype='complex64').reshape([1, 2, 1])
y = util.correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1j], dtype='complex64').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([-3, -4], dtype='complex64').reshape([1, 2, 1])
y = util.correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_correlate_full(self):
with self.test_session():
l_ = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1])
l = tf.constant(l_)
r_ = np.array([0, 1, 0], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([0, 1, 2, 3, 4, 0], dtype='float32').reshape([1, 6, 1])
y = util.correlate(l, r, mode='full')
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1], dtype='float32').reshape([3, 1, 1])
r = tf.constant(r_)
y_ = np.array([1, 2, 3, 4, 0, 0], dtype='float32').reshape([1, 6, 1])
y = util.correlate(l, r, mode='full')
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_outer_correlate(self):
with self.test_session():
l = np.array([1, 2, 3, 4], dtype='float32').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1, 0], dtype='float32').reshape([1, 3, 1])
r_ = tf.constant(r)
y_ = np.array([2, 3], dtype='float32').reshape([2, 1, 1])
y = util.outer_correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1], dtype='float32').reshape([1, 3, 1])
r = tf.constant(r_)
y_ = np.array([3, 4], dtype='float32').reshape([2, 1, 1])
y = util.outer_correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_outer_correlate_complex(self):
with self.test_session():
l = np.array([1j, 2j, 3j, 4j], dtype='complex64').reshape([1, 4, 1])
l_ = tf.constant(l)
r = np.array([0, 1j, 0], dtype='complex64').reshape([1, 3, 1])
r_ = tf.constant(r)
y_ = np.array([-2, -3], dtype='complex64').reshape([2, 1, 1])
y = util.outer_correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
r_ = np.array([0, 0, 1j], dtype='complex64').reshape([1, 3, 1])
r = tf.constant(r_)
y_ = np.array([-3, -4], dtype='complex64').reshape([2, 1, 1])
y = util.outer_correlate(l, r)
self.assertAllClose(y.eval(), y_, atol=1e-5, rtol=1e-5)
def test_fft(self):
x = randn([3, 4], 'complex64')
y = np.fft.fftn(x, norm='ortho')
y_ = util.fft(x)
with self.test_session():
self.assertAllClose(y_.eval(), y)
def test_ifft(self):
x = randn([3, 4], 'complex64')
y = np.fft.ifftn(x, norm='ortho')
y_ = util.ifft(x)
with self.test_session():
self.assertAllClose(y_.eval(), y)