提交 3bfb50f2 authored 作者: amrithasuresh's avatar amrithasuresh

1. Updated numpy as np

2. Fixed indentation
上级 85dad09c
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import unittest import unittest
import theano import theano
...@@ -18,25 +18,25 @@ class TestFFT(unittest.TestCase): ...@@ -18,25 +18,25 @@ class TestFFT(unittest.TestCase):
def f_rfft(inp): def f_rfft(inp):
return fft.rfft(inp) return fft.rfft(inp)
inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
utt.verify_grad(f_rfft, [inputs_val], eps=eps) utt.verify_grad(f_rfft, [inputs_val], eps=eps)
def f_irfft(inp): def f_irfft(inp):
return fft.irfft(inp) return fft.irfft(inp)
inputs_val = numpy.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
utt.verify_grad(f_irfft, [inputs_val], eps=eps) utt.verify_grad(f_irfft, [inputs_val], eps=eps)
def test_1Drfft(self): def test_1Drfft(self):
inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
x = T.matrix('x') x = T.matrix('x')
rfft = fft.rfft(x) rfft = fft.rfft(x)
f_rfft = theano.function([x], rfft) f_rfft = theano.function([x], rfft)
res_rfft = f_rfft(inputs_val) res_rfft = f_rfft(inputs_val)
res_rfft_comp = (numpy.asarray(res_rfft[:, :, 0]) + res_rfft_comp = (np.asarray(res_rfft[:, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, 1])) 1j * np.asarray(res_rfft[:, :, 1]))
rfft_ref = numpy.fft.rfft(inputs_val, axis=1) rfft_ref = np.fft.rfft(inputs_val, axis=1)
utt.assert_allclose(rfft_ref, res_rfft_comp) utt.assert_allclose(rfft_ref, res_rfft_comp)
...@@ -46,7 +46,7 @@ class TestFFT(unittest.TestCase): ...@@ -46,7 +46,7 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([m], irfft) f_irfft = theano.function([m], irfft)
res_irfft = f_irfft(res_rfft) res_irfft = f_irfft(res_rfft)
utt.assert_allclose(inputs_val, numpy.asarray(res_irfft)) utt.assert_allclose(inputs_val, np.asarray(res_irfft))
# The numerical gradient of the FFT is sensitive, must set large # The numerical gradient of the FFT is sensitive, must set large
# enough epsilon to get good accuracy. # enough epsilon to get good accuracy.
...@@ -54,30 +54,30 @@ class TestFFT(unittest.TestCase): ...@@ -54,30 +54,30 @@ class TestFFT(unittest.TestCase):
def f_rfft(inp): def f_rfft(inp):
return fft.rfft(inp) return fft.rfft(inp)
inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
utt.verify_grad(f_rfft, [inputs_val], eps=eps) utt.verify_grad(f_rfft, [inputs_val], eps=eps)
def f_irfft(inp): def f_irfft(inp):
return fft.irfft(inp) return fft.irfft(inp)
inputs_val = numpy.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
utt.verify_grad(f_irfft, [inputs_val], eps=eps) utt.verify_grad(f_irfft, [inputs_val], eps=eps)
def test_rfft(self): def test_rfft(self):
inputs_val = numpy.random.random((1, N, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
rfft = fft.rfft(inputs) rfft = fft.rfft(inputs)
f_rfft = theano.function([], rfft) f_rfft = theano.function([], rfft)
res_rfft = f_rfft() res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) + res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1])) 1j * np.asarray(res_rfft[:, :, :, 1]))
rfft_ref = numpy.fft.rfftn(inputs_val, axes=(1, 2)) rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))
utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4) utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)
def test_irfft(self): def test_irfft(self):
inputs_val = numpy.random.random((1, N, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
rfft = fft.rfft(inputs) rfft = fft.rfft(inputs)
...@@ -89,9 +89,9 @@ class TestFFT(unittest.TestCase): ...@@ -89,9 +89,9 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([m], irfft) f_irfft = theano.function([m], irfft)
res_irfft = f_irfft(res_fft) res_irfft = f_irfft(res_fft)
utt.assert_allclose(inputs_val, numpy.asarray(res_irfft)) utt.assert_allclose(inputs_val, np.asarray(res_irfft))
inputs_val = numpy.random.random((1, N, N, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N, 2)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
irfft = fft.irfft(inputs) irfft = fft.irfft(inputs)
...@@ -99,22 +99,22 @@ class TestFFT(unittest.TestCase): ...@@ -99,22 +99,22 @@ class TestFFT(unittest.TestCase):
res_irfft = f_irfft() res_irfft = f_irfft()
inputs_ref = inputs_val[..., 0] + inputs_val[..., 1] * 1j inputs_ref = inputs_val[..., 0] + inputs_val[..., 1] * 1j
irfft_ref = numpy.fft.irfftn(inputs_ref, axes=(1, 2)) irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))
utt.assert_allclose(irfft_ref, res_irfft, atol=1e-4, rtol=1e-4) utt.assert_allclose(irfft_ref, res_irfft, atol=1e-4, rtol=1e-4)
def test_norm_rfft(self): def test_norm_rfft(self):
inputs_val = numpy.random.random((1, N, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
# Unitary normalization # Unitary normalization
rfft = fft.rfft(inputs, norm='ortho') rfft = fft.rfft(inputs, norm='ortho')
f_rfft = theano.function([], rfft) f_rfft = theano.function([], rfft)
res_rfft = f_rfft() res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) + res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1])) 1j * np.asarray(res_rfft[:, :, :, 1]))
rfft_ref = numpy.fft.rfftn(inputs_val, axes=(1, 2)) rfft_ref = np.fft.rfftn(inputs_val, axes=(1, 2))
utt.assert_allclose(rfft_ref / N, res_rfft_comp, atol=1e-4, rtol=1e-4) utt.assert_allclose(rfft_ref / N, res_rfft_comp, atol=1e-4, rtol=1e-4)
...@@ -122,13 +122,13 @@ class TestFFT(unittest.TestCase): ...@@ -122,13 +122,13 @@ class TestFFT(unittest.TestCase):
rfft = fft.rfft(inputs, norm='no_norm') rfft = fft.rfft(inputs, norm='no_norm')
f_rfft = theano.function([], rfft) f_rfft = theano.function([], rfft)
res_rfft = f_rfft() res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) + res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1])) 1j * np.asarray(res_rfft[:, :, :, 1]))
utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4) utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)
# Inverse FFT inputs # Inverse FFT inputs
inputs_val = numpy.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
inputs_ref = inputs_val[..., 0] + 1j * inputs_val[..., 1] inputs_ref = inputs_val[..., 0] + 1j * inputs_val[..., 1]
...@@ -137,7 +137,7 @@ class TestFFT(unittest.TestCase): ...@@ -137,7 +137,7 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([], irfft) f_irfft = theano.function([], irfft)
res_irfft = f_irfft() res_irfft = f_irfft()
irfft_ref = numpy.fft.irfftn(inputs_ref, axes=(1, 2)) irfft_ref = np.fft.irfftn(inputs_ref, axes=(1, 2))
utt.assert_allclose(irfft_ref * N, res_irfft, atol=1e-4, rtol=1e-4) utt.assert_allclose(irfft_ref * N, res_irfft, atol=1e-4, rtol=1e-4)
...@@ -149,12 +149,12 @@ class TestFFT(unittest.TestCase): ...@@ -149,12 +149,12 @@ class TestFFT(unittest.TestCase):
utt.assert_allclose(irfft_ref * N**2, res_irfft, atol=1e-4, rtol=1e-4) utt.assert_allclose(irfft_ref * N**2, res_irfft, atol=1e-4, rtol=1e-4)
def test_params(self): def test_params(self):
inputs_val = numpy.random.random((1, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
self.assertRaises(ValueError, fft.rfft, inputs, norm=123) self.assertRaises(ValueError, fft.rfft, inputs, norm=123)
inputs_val = numpy.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N // 2 + 1, 2)).astype(theano.config.floatX)
inputs = theano.shared(inputs_val) inputs = theano.shared(inputs_val)
self.assertRaises(ValueError, fft.irfft, inputs, norm=123) self.assertRaises(ValueError, fft.irfft, inputs, norm=123)
...@@ -167,20 +167,20 @@ class TestFFT(unittest.TestCase): ...@@ -167,20 +167,20 @@ class TestFFT(unittest.TestCase):
def f_rfft(inp): def f_rfft(inp):
return fft.rfft(inp) return fft.rfft(inp)
inputs_val = numpy.random.random((1, N, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
utt.verify_grad(f_rfft, [inputs_val], eps=eps) utt.verify_grad(f_rfft, [inputs_val], eps=eps)
def f_irfft(inp): def f_irfft(inp):
return fft.irfft(inp) return fft.irfft(inp)
inputs_val = numpy.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX)
utt.verify_grad(f_irfft, [inputs_val], eps=eps) utt.verify_grad(f_irfft, [inputs_val], eps=eps)
def f_rfft(inp): def f_rfft(inp):
return fft.rfft(inp, norm='ortho') return fft.rfft(inp, norm='ortho')
inputs_val = numpy.random.random((1, N, N)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N)).astype(theano.config.floatX)
utt.verify_grad(f_rfft, [inputs_val], eps=eps) utt.verify_grad(f_rfft, [inputs_val], eps=eps)
def f_irfft(inp): def f_irfft(inp):
return fft.irfft(inp, norm='no_norm') return fft.irfft(inp, norm='no_norm')
inputs_val = numpy.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX) inputs_val = np.random.random((1, N, N // 2 + 1, 2)).astype(theano.config.floatX)
utt.verify_grad(f_irfft, [inputs_val], eps=eps) utt.verify_grad(f_irfft, [inputs_val], eps=eps)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论