提交 cf81ce7f authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 e79c4e4c
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import unittest import unittest
import numpy
import numpy as np import numpy as np
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.tools import assert_raises, assert_true from nose.tools import assert_raises, assert_true
...@@ -238,8 +237,8 @@ class TestAssertShape(unittest.TestCase): ...@@ -238,8 +237,8 @@ class TestAssertShape(unittest.TestCase):
expected_shape = [None, s1, s2, None] expected_shape = [None, s1, s2, None]
f = theano.function([x, s1, s2], assert_shape(x, expected_shape)) f = theano.function([x, s1, s2], assert_shape(x, expected_shape))
v = numpy.zeros((3, 5, 7, 11), dtype='float32') v = np.zeros((3, 5, 7, 11), dtype='float32')
self.assertEqual(0, numpy.sum(f(v, 5, 7))) self.assertEqual(0, np.sum(f(v, 5, 7)))
assert_raises(AssertionError, f, v, 5, 0) assert_raises(AssertionError, f, v, 5, 0)
assert_raises(AssertionError, f, v, 5, 9) assert_raises(AssertionError, f, v, 5, 9)
...@@ -257,12 +256,12 @@ class TestAssertShape(unittest.TestCase): ...@@ -257,12 +256,12 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([input, filters], out) f = theano.function([input, filters], out)
# mismatched input_shape # mismatched input_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 5, 9, 11), dtype='float32'), np.zeros((3, 5, 9, 11), dtype='float32'),
numpy.zeros((7, 5, 3, 3), dtype='float32')) np.zeros((7, 5, 3, 3), dtype='float32'))
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 5, 7, 11), dtype='float32'), np.zeros((3, 5, 7, 11), dtype='float32'),
numpy.zeros((7, 5, 2, 2), dtype='float32')) np.zeros((7, 5, 2, 2), dtype='float32'))
@change_flags([("conv.assert_shape", True)]) @change_flags([("conv.assert_shape", True)])
def test_shape_check_conv3d(self): def test_shape_check_conv3d(self):
...@@ -275,12 +274,12 @@ class TestAssertShape(unittest.TestCase): ...@@ -275,12 +274,12 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([input, filters], out) f = theano.function([input, filters], out)
# mismatched input_shape # mismatched input_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 5, 9, 11, 13), dtype='float32'), np.zeros((3, 5, 9, 11, 13), dtype='float32'),
numpy.zeros((7, 5, 3, 3, 3), dtype='float32')) np.zeros((7, 5, 3, 3, 3), dtype='float32'))
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 5, 7, 11, 13), dtype='float32'), np.zeros((3, 5, 7, 11, 13), dtype='float32'),
numpy.zeros((7, 5, 2, 2, 2), dtype='float32')) np.zeros((7, 5, 2, 2, 2), dtype='float32'))
@change_flags([("conv.assert_shape", True)]) @change_flags([("conv.assert_shape", True)])
def test_shape_check_conv2d_grad_wrt_inputs(self): def test_shape_check_conv2d_grad_wrt_inputs(self):
...@@ -293,8 +292,8 @@ class TestAssertShape(unittest.TestCase): ...@@ -293,8 +292,8 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([output_grad, filters], out) f = theano.function([output_grad, filters], out)
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 6, 5, 9), dtype='float32'), np.zeros((3, 6, 5, 9), dtype='float32'),
numpy.zeros((7, 6, 3, 3), dtype='float32')) np.zeros((7, 6, 3, 3), dtype='float32'))
@change_flags([("conv.assert_shape", True)]) @change_flags([("conv.assert_shape", True)])
def test_shape_check_conv3d_grad_wrt_inputs(self): def test_shape_check_conv3d_grad_wrt_inputs(self):
...@@ -307,8 +306,8 @@ class TestAssertShape(unittest.TestCase): ...@@ -307,8 +306,8 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([output_grad, filters], out) f = theano.function([output_grad, filters], out)
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 6, 5, 9, 11), dtype='float32'), np.zeros((3, 6, 5, 9, 11), dtype='float32'),
numpy.zeros((7, 6, 3, 3, 3), dtype='float32')) np.zeros((7, 6, 3, 3, 3), dtype='float32'))
@change_flags([("conv.assert_shape", True)]) @change_flags([("conv.assert_shape", True)])
def test_shape_check_conv2d_grad_wrt_weights(self): def test_shape_check_conv2d_grad_wrt_weights(self):
...@@ -321,8 +320,8 @@ class TestAssertShape(unittest.TestCase): ...@@ -321,8 +320,8 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([input, output_grad], out) f = theano.function([input, output_grad], out)
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 6, 7, 11), dtype='float32'), np.zeros((3, 6, 7, 11), dtype='float32'),
numpy.zeros((3, 7, 5, 9), dtype='float32')) np.zeros((3, 7, 5, 9), dtype='float32'))
@change_flags([("conv.assert_shape", True)]) @change_flags([("conv.assert_shape", True)])
def test_shape_check_conv3d_grad_wrt_weights(self): def test_shape_check_conv3d_grad_wrt_weights(self):
...@@ -335,8 +334,8 @@ class TestAssertShape(unittest.TestCase): ...@@ -335,8 +334,8 @@ class TestAssertShape(unittest.TestCase):
f = theano.function([input, output_grad], out) f = theano.function([input, output_grad], out)
# mismatched filter_shape # mismatched filter_shape
assert_raises(AssertionError, f, assert_raises(AssertionError, f,
numpy.zeros((3, 6, 7, 11, 13), dtype='float32'), np.zeros((3, 6, 7, 11, 13), dtype='float32'),
numpy.zeros((3, 7, 5, 9, 11), dtype='float32')) np.zeros((3, 7, 5, 9, 11), dtype='float32'))
class BaseTestConv(object): class BaseTestConv(object):
...@@ -371,8 +370,8 @@ class BaseTestConv(object): ...@@ -371,8 +370,8 @@ class BaseTestConv(object):
if filter_dilation is None: if filter_dilation is None:
filter_dilation = (1,) * (len(inputs_shape) - 2) filter_dilation = (1,) * (len(inputs_shape) - 2)
inputs_val = numpy.random.random(inputs_shape).astype('float32') inputs_val = np.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32') filters_val = np.random.random(filters_shape).astype('float32')
# scale down values to prevent rounding errors # scale down values to prevent rounding errors
inputs_val /= 10 inputs_val /= 10
...@@ -414,8 +413,8 @@ class BaseTestConv(object): ...@@ -414,8 +413,8 @@ class BaseTestConv(object):
if check_trace: if check_trace:
assert_true(check_stack_trace(f, ops_to_check=target_op)) assert_true(check_stack_trace(f, ops_to_check=target_op))
res_ref = numpy.array(f_ref()) res_ref = np.array(f_ref())
res = numpy.array(f()) res = np.array(f())
utt.assert_allclose(res_ref, res) utt.assert_allclose(res_ref, res)
if verify_grad and inputs_val.size > 0 and filters_val.size > 0 and res.size > 0: if verify_grad and inputs_val.size > 0 and filters_val.size > 0 and res.size > 0:
utt.verify_grad(conv_op(border_mode=border_mode, utt.verify_grad(conv_op(border_mode=border_mode,
...@@ -436,8 +435,8 @@ class BaseTestConv(object): ...@@ -436,8 +435,8 @@ class BaseTestConv(object):
if filter_dilation is None: if filter_dilation is None:
filter_dilation = (1,) * (len(inputs_shape) - 2) filter_dilation = (1,) * (len(inputs_shape) - 2)
inputs_val = numpy.random.random(inputs_shape).astype('float32') inputs_val = np.random.random(inputs_shape).astype('float32')
output_val = numpy.random.random(output_shape).astype('float32') output_val = np.random.random(output_shape).astype('float32')
inputs = self.shared(inputs_val) inputs = self.shared(inputs_val)
output = self.shared(output_val) output = self.shared(output_val)
...@@ -473,8 +472,8 @@ class BaseTestConv(object): ...@@ -473,8 +472,8 @@ class BaseTestConv(object):
if check_trace: if check_trace:
assert_true(check_stack_trace(f, ops_to_check=target_op)) assert_true(check_stack_trace(f, ops_to_check=target_op))
res_ref = numpy.array(f_ref()) res_ref = np.array(f_ref())
res = numpy.array(f()) res = np.array(f())
utt.assert_allclose(res_ref, res) utt.assert_allclose(res_ref, res)
def abstract_conv_gradweight(inputs_val, output_val): def abstract_conv_gradweight(inputs_val, output_val):
...@@ -499,8 +498,8 @@ class BaseTestConv(object): ...@@ -499,8 +498,8 @@ class BaseTestConv(object):
if filter_dilation is None: if filter_dilation is None:
filter_dilation = (1,) * (len(inputs_shape) - 2) filter_dilation = (1,) * (len(inputs_shape) - 2)
output_val = numpy.random.random(output_shape).astype('float32') output_val = np.random.random(output_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32') filters_val = np.random.random(filters_shape).astype('float32')
output = self.shared(output_val) output = self.shared(output_val)
filters = self.shared(filters_val) filters = self.shared(filters_val)
...@@ -537,10 +536,10 @@ class BaseTestConv(object): ...@@ -537,10 +536,10 @@ class BaseTestConv(object):
if check_trace: if check_trace:
assert_true(check_stack_trace(f, ops_to_check=target_op)) assert_true(check_stack_trace(f, ops_to_check=target_op))
res = numpy.array(f()) res = np.array(f())
if ref is not None: if ref is not None:
res_ref = numpy.array(f_ref()) res_ref = np.array(f_ref())
utt.assert_allclose(res_ref, res) utt.assert_allclose(res_ref, res)
def abstract_conv_gradinputs(filters_val, output_val): def abstract_conv_gradinputs(filters_val, output_val):
...@@ -1272,7 +1271,7 @@ class TestConvTypes(unittest.TestCase): ...@@ -1272,7 +1271,7 @@ class TestConvTypes(unittest.TestCase):
self.filters = tensor.ftensor4() self.filters = tensor.ftensor4()
self.topgrad = tensor.ftensor4() self.topgrad = tensor.ftensor4()
self.constant_tensor = numpy.zeros((3, 5, 7, 11), dtype='float32') self.constant_tensor = np.zeros((3, 5, 7, 11), dtype='float32')
def test_grad_types(self): def test_grad_types(self):
# This function simply tests the behaviour of the AbstractConv # This function simply tests the behaviour of the AbstractConv
...@@ -1582,7 +1581,7 @@ class TestConv2dTranspose(unittest.TestCase): ...@@ -1582,7 +1581,7 @@ class TestConv2dTranspose(unittest.TestCase):
output_shape=(2, 1, 10, 10), output_shape=(2, 1, 10, 10),
input_dilation=(2, 2)), input_dilation=(2, 2)),
mode=mode)() mode=mode)()
expected_output = numpy.array( expected_output = np.array(
[[[[2, 2, 4, 4, 4, 4, 4, 4, 2, 2], [[[[2, 2, 4, 4, 4, 4, 4, 4, 2, 2],
[2, 2, 4, 4, 4, 4, 4, 4, 2, 2], [2, 2, 4, 4, 4, 4, 4, 4, 2, 2],
[4, 4, 8, 8, 8, 8, 8, 8, 4, 4], [4, 4, 8, 8, 8, 8, 8, 8, 4, 4],
...@@ -1593,7 +1592,7 @@ class TestConv2dTranspose(unittest.TestCase): ...@@ -1593,7 +1592,7 @@ class TestConv2dTranspose(unittest.TestCase):
[4, 4, 8, 8, 8, 8, 8, 8, 4, 4], [4, 4, 8, 8, 8, 8, 8, 8, 4, 4],
[2, 2, 4, 4, 4, 4, 4, 4, 2, 2], [2, 2, 4, 4, 4, 4, 4, 4, 2, 2],
[2, 2, 4, 4, 4, 4, 4, 4, 2, 2]]]] * 2) [2, 2, 4, 4, 4, 4, 4, 4, 2, 2]]]] * 2)
numpy.testing.assert_equal(output, expected_output) np.testing.assert_equal(output, expected_output)
class TestConv2dGrads(unittest.TestCase): class TestConv2dGrads(unittest.TestCase):
...@@ -1604,7 +1603,7 @@ class TestConv2dGrads(unittest.TestCase): ...@@ -1604,7 +1603,7 @@ class TestConv2dGrads(unittest.TestCase):
theano.config.mode == "FAST_COMPILE"): theano.config.mode == "FAST_COMPILE"):
raise SkipTest("Need blas to test conv2d") raise SkipTest("Need blas to test conv2d")
self.random_stream = numpy.random.RandomState(utt.fetch_seed()) self.random_stream = np.random.RandomState(utt.fetch_seed())
self.inputs_shapes = [(8, 1, 12, 12), (1, 1, 5, 5), (1, 1, 5, 6), (1, 1, 6, 6)] self.inputs_shapes = [(8, 1, 12, 12), (1, 1, 5, 5), (1, 1, 5, 6), (1, 1, 6, 6)]
self.filters_shapes = [(5, 1, 2, 2), (1, 1, 3, 3)] self.filters_shapes = [(5, 1, 2, 2), (1, 1, 3, 3)]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论