forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfft_ops_test.py
More file actions
126 lines (107 loc) · 3.93 KB
/
fft_ops_test.py
File metadata and controls
126 lines (107 loc) · 3.93 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
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for fft operations.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow.python.platform
import numpy as np
import tensorflow as tf
class FFT2DOpsTest(tf.test.TestCase):
def _tfFFT2D(self, x, use_gpu=False):
with self.test_session(use_gpu=use_gpu):
return tf.fft2d(x).eval()
def _npFFT2D(self, x):
return np.fft.fft2(x)
def _tfIFFT2D(self, x, use_gpu=False):
with self.test_session(use_gpu=use_gpu):
return tf.ifft2d(x).eval()
def _npIFFT2D(self, x):
return np.fft.ifft2(x)
def _Compare(self, x):
if tf.test.IsBuiltWithCuda():
# GPU/Forward
self.assertAllClose(
self._npFFT2D(x),
self._tfFFT2D(x,
use_gpu=True),
rtol=1e-4,
atol=1e-4)
# GPU/Backward
self.assertAllClose(
self._npIFFT2D(x),
self._tfIFFT2D(x,
use_gpu=True),
rtol=1e-4,
atol=1e-4)
def testBasic(self):
self._Compare(np.arange(60).reshape([6, 10]))
self._Compare(np.arange(60).reshape([10, 6]))
def testRandom(self):
np.random.seed(12345)
def gen(shape):
n = np.prod(shape)
re = np.random.uniform(size=n)
im = np.random.uniform(size=n)
return (re + im * 1j).reshape(shape)
for shape in [(1, 1), (5, 5), (5, 7), (7, 5), (100, 250)]:
self._Compare(gen(shape))
def testEmpty(self):
if tf.test.IsBuiltWithCuda():
x = np.zeros([40, 0]).astype(np.complex64)
self.assertEqual(x.shape, self._tfFFT2D(x).shape)
self.assertEqual(x.shape, self._tfIFFT2D(x).shape)
def testError(self):
if tf.test.IsBuiltWithCuda():
x = np.zeros([1, 2, 3]).astype(np.complex64)
with self.assertRaisesOpError("Input is not a matrix"):
self._tfFFT2D(x)
with self.assertRaisesOpError("Input is not a matrix"):
self._tfIFFT2D(x)
def _checkGrad(self, func, x, y, use_gpu=False):
with self.test_session(use_gpu=use_gpu):
inx = tf.convert_to_tensor(x)
iny = tf.convert_to_tensor(y)
# func = fft2d or ifft2d
z = func(tf.complex(inx, iny))
# loss = sum(|z|^2)
loss = tf.reduce_sum(tf.real(z * tf.conj(z)))
((x_jacob_t, x_jacob_n),
(y_jacob_t, y_jacob_n)) = tf.test.compute_gradient(
[inx, iny],
[list(x.shape), list(y.shape)],
loss,
[1],
x_init_value=[x, y],
delta=1e-2)
self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=1e-2)
self.assertAllClose(y_jacob_t, y_jacob_n, rtol=1e-2, atol=1e-2)
def testGrad_Simple(self):
if tf.test.IsBuiltWithCuda():
re = np.array([[1., 0.], [0., 1.]]).astype(np.float32)
im = np.array([[0., 0.], [0., 0.]]).astype(np.float32)
self._checkGrad(tf.fft2d, re, im, use_gpu=True)
self._checkGrad(tf.ifft2d, re, im, use_gpu=True)
def testGrad_Random(self):
if tf.test.IsBuiltWithCuda():
shape = (4, 8)
np.random.seed(54321)
re = np.random.rand(*shape).astype(np.float32) * 2 - 1
im = np.random.rand(*shape).astype(np.float32) * 2 - 1
self._checkGrad(tf.fft2d, re, im, use_gpu=True)
self._checkGrad(tf.ifft2d, re, im, use_gpu=True)
if __name__ == "__main__":
tf.test.main()