提交 85dad09c authored 作者: amrithasuresh's avatar amrithasuresh

1. Updated numpy as np

2. Fixed indentation
上级 c71d9d5c
...@@ -2,7 +2,6 @@ from __future__ import absolute_import, print_function, division ...@@ -2,7 +2,6 @@ from __future__ import absolute_import, print_function, division
from functools import partial from functools import partial
import numpy as np import numpy as np
import numpy
import theano import theano
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -23,7 +22,7 @@ from theano.tests.unittest_tools import attr ...@@ -23,7 +22,7 @@ from theano.tests.unittest_tools import attr
def test_cpu_contiguous(): def test_cpu_contiguous():
a = T.fmatrix('a') a = T.fmatrix('a')
i = T.iscalar('i') i = T.iscalar('i')
a_val = numpy.asarray(numpy.random.rand(4, 5), dtype='float32') a_val = np.asarray(np.random.rand(4, 5), dtype='float32')
f = theano.function([a, i], cpu_contiguous(a.reshape((5, 4))[::i])) f = theano.function([a, i], cpu_contiguous(a.reshape((5, 4))[::i]))
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert any([isinstance(node.op, CpuContiguous) for node in topo]) assert any([isinstance(node.op, CpuContiguous) for node in topo])
...@@ -33,7 +32,7 @@ def test_cpu_contiguous(): ...@@ -33,7 +32,7 @@ def test_cpu_contiguous():
# Test the grad: # Test the grad:
theano.tests.unittest_tools.verify_grad(cpu_contiguous, theano.tests.unittest_tools.verify_grad(cpu_contiguous,
[numpy.random.rand(5, 7, 2)]) [np.random.rand(5, 7, 2)])
class TestSearchsortedOp(utt.InferShapeTester): class TestSearchsortedOp(utt.InferShapeTester):
...@@ -280,20 +279,20 @@ class SqueezeTester(utt.InferShapeTester): ...@@ -280,20 +279,20 @@ class SqueezeTester(utt.InferShapeTester):
def test_op(self): def test_op(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list): for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX) data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)() variable = tensor.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], self.op(variable)) f = theano.function([variable], self.op(variable))
expected = numpy.squeeze(data) expected = np.squeeze(data)
tested = f(data) tested = f(data)
assert tested.shape == expected.shape assert tested.shape == expected.shape
assert numpy.allclose(tested, expected) assert np.allclose(tested, expected)
def test_infer_shape(self): def test_infer_shape(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list): for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX) data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)() variable = tensor.TensorType(theano.config.floatX, broadcast)()
self._compile_and_check([variable], self._compile_and_check([variable],
...@@ -304,23 +303,23 @@ class SqueezeTester(utt.InferShapeTester): ...@@ -304,23 +303,23 @@ class SqueezeTester(utt.InferShapeTester):
def test_grad(self): def test_grad(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list): for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX) data = np.random.random(size=shape).astype(theano.config.floatX)
utt.verify_grad(self.op, [data]) utt.verify_grad(self.op, [data])
def test_var_interface(self): def test_var_interface(self):
# same as test_op, but use a_theano_var.squeeze. # same as test_op, but use a_theano_var.squeeze.
for shape, broadcast in zip(self.shape_list, self.broadcast_list): for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX) data = np.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)() variable = tensor.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], variable.squeeze()) f = theano.function([variable], variable.squeeze())
expected = numpy.squeeze(data) expected = np.squeeze(data)
tested = f(data) tested = f(data)
assert tested.shape == expected.shape assert tested.shape == expected.shape
assert numpy.allclose(tested, expected) assert np.allclose(tested, expected)
class CompressTester(utt.InferShapeTester): class CompressTester(utt.InferShapeTester):
...@@ -351,17 +350,17 @@ class CompressTester(utt.InferShapeTester): ...@@ -351,17 +350,17 @@ class CompressTester(utt.InferShapeTester):
for axis, cond, shape in zip(self.axis_list, self.cond_list, for axis, cond, shape in zip(self.axis_list, self.cond_list,
self.shape_list): self.shape_list):
cond_var = theano.tensor.ivector() cond_var = theano.tensor.ivector()
data = numpy.random.random(size=shape).astype(theano.config.floatX) data = np.random.random(size=shape).astype(theano.config.floatX)
data_var = theano.tensor.matrix() data_var = theano.tensor.matrix()
f = theano.function([cond_var, data_var], f = theano.function([cond_var, data_var],
self.op(cond_var, data_var, axis=axis)) self.op(cond_var, data_var, axis=axis))
expected = numpy.compress(cond, data, axis=axis) expected = np.compress(cond, data, axis=axis)
tested = f(cond, data) tested = f(cond, data)
assert tested.shape == expected.shape assert tested.shape == expected.shape
assert numpy.allclose(tested, expected) assert np.allclose(tested, expected)
class TestRepeatOp(utt.InferShapeTester): class TestRepeatOp(utt.InferShapeTester):
...@@ -388,7 +387,7 @@ class TestRepeatOp(utt.InferShapeTester): ...@@ -388,7 +387,7 @@ class TestRepeatOp(utt.InferShapeTester):
for axis in self._possible_axis(ndim): for axis in self._possible_axis(ndim):
for dtype in tensor.integer_dtypes: for dtype in tensor.integer_dtypes:
r_var = T.scalar(dtype=dtype) r_var = T.scalar(dtype=dtype)
r = numpy.asarray(3, dtype=dtype) r = np.asarray(3, dtype=dtype)
if (dtype == 'uint64' or if (dtype == 'uint64' or
(dtype in self.numpy_unsupported_dtypes and (dtype in self.numpy_unsupported_dtypes and
r_var.ndim == 1)): r_var.ndim == 1)):
...@@ -441,13 +440,13 @@ class TestRepeatOp(utt.InferShapeTester): ...@@ -441,13 +440,13 @@ class TestRepeatOp(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
for ndim in range(4): for ndim in range(4):
x = T.TensorType(config.floatX, [False] * ndim)() x = T.TensorType(config.floatX, [False] * ndim)()
shp = (numpy.arange(ndim) + 1) * 5 shp = (np.arange(ndim) + 1) * 5
a = np.random.random(shp).astype(config.floatX) a = np.random.random(shp).astype(config.floatX)
for axis in self._possible_axis(ndim): for axis in self._possible_axis(ndim):
for dtype in tensor.integer_dtypes: for dtype in tensor.integer_dtypes:
r_var = T.scalar(dtype=dtype) r_var = T.scalar(dtype=dtype)
r = numpy.asarray(3, dtype=dtype) r = np.asarray(3, dtype=dtype)
if dtype in self.numpy_unsupported_dtypes: if dtype in self.numpy_unsupported_dtypes:
r_var = T.vector(dtype=dtype) r_var = T.vector(dtype=dtype)
self.assertRaises(TypeError, repeat, x, r_var) self.assertRaises(TypeError, repeat, x, r_var)
...@@ -501,17 +500,17 @@ class TestBartlett(utt.InferShapeTester): ...@@ -501,17 +500,17 @@ class TestBartlett(utt.InferShapeTester):
def test_perform(self): def test_perform(self):
x = tensor.lscalar() x = tensor.lscalar()
f = function([x], self.op(x)) f = function([x], self.op(x))
M = numpy.random.randint(3, 51, size=()) M = np.random.randint(3, 51, size=())
assert numpy.allclose(f(M), numpy.bartlett(M)) assert np.allclose(f(M), np.bartlett(M))
assert numpy.allclose(f(0), numpy.bartlett(0)) assert np.allclose(f(0), np.bartlett(0))
assert numpy.allclose(f(-1), numpy.bartlett(-1)) assert np.allclose(f(-1), np.bartlett(-1))
b = numpy.array([17], dtype='uint8') b = np.array([17], dtype='uint8')
assert numpy.allclose(f(b[0]), numpy.bartlett(b[0])) assert np.allclose(f(b[0]), np.bartlett(b[0]))
def test_infer_shape(self): def test_infer_shape(self):
x = tensor.lscalar() x = tensor.lscalar()
self._compile_and_check([x], [self.op(x)], self._compile_and_check([x], [self.op(x)],
[numpy.random.randint(3, 51, size=())], [np.random.randint(3, 51, size=())],
self.op_class) self.op_class)
self._compile_and_check([x], [self.op(x)], [0], self.op_class) self._compile_and_check([x], [self.op(x)], [0], self.op_class)
self._compile_and_check([x], [self.op(x)], [1], self.op_class) self._compile_and_check([x], [self.op(x)], [1], self.op_class)
...@@ -519,7 +518,7 @@ class TestBartlett(utt.InferShapeTester): ...@@ -519,7 +518,7 @@ class TestBartlett(utt.InferShapeTester):
class TestFillDiagonal(utt.InferShapeTester): class TestFillDiagonal(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestFillDiagonal, self).setUp() super(TestFillDiagonal, self).setUp()
...@@ -531,21 +530,21 @@ class TestFillDiagonal(utt.InferShapeTester): ...@@ -531,21 +530,21 @@ class TestFillDiagonal(utt.InferShapeTester):
y = tensor.scalar() y = tensor.scalar()
f = function([x, y], fill_diagonal(x, y)) f = function([x, y], fill_diagonal(x, y))
for shp in [(8, 8), (5, 8), (8, 5)]: for shp in [(8, 8), (5, 8), (8, 5)]:
a = numpy.random.rand(*shp).astype(config.floatX) a = np.random.rand(*shp).astype(config.floatX)
val = numpy.cast[config.floatX](numpy.random.rand()) val = np.cast[config.floatX](np.random.rand())
out = f(a, val) out = f(a, val)
# We can't use numpy.fill_diagonal as it is bugged. # We can't use np.fill_diagonal as it is bugged.
assert numpy.allclose(numpy.diag(out), val) assert np.allclose(np.diag(out), val)
assert (out == val).sum() == min(a.shape) assert (out == val).sum() == min(a.shape)
# test for 3d tensor # test for 3d tensor
a = numpy.random.rand(3, 3, 3).astype(config.floatX) a = np.random.rand(3, 3, 3).astype(config.floatX)
x = tensor.tensor3() x = tensor.tensor3()
y = tensor.scalar() y = tensor.scalar()
f = function([x, y], fill_diagonal(x, y)) f = function([x, y], fill_diagonal(x, y))
val = numpy.cast[config.floatX](numpy.random.rand() + 10) val = np.cast[config.floatX](np.random.rand() + 10)
out = f(a, val) out = f(a, val)
# We can't use numpy.fill_diagonal as it is bugged. # We can't use np.fill_diagonal as it is bugged.
assert out[0, 0, 0] == val assert out[0, 0, 0] == val
assert out[1, 1, 1] == val assert out[1, 1, 1] == val
assert out[2, 2, 2] == val assert out[2, 2, 2] == val
...@@ -553,11 +552,11 @@ class TestFillDiagonal(utt.InferShapeTester): ...@@ -553,11 +552,11 @@ class TestFillDiagonal(utt.InferShapeTester):
@attr('slow') @attr('slow')
def test_gradient(self): def test_gradient(self):
utt.verify_grad(fill_diagonal, [numpy.random.rand(5, 8), utt.verify_grad(fill_diagonal, [np.random.rand(5, 8),
numpy.random.rand()], np.random.rand()],
n_tests=1, rng=TestFillDiagonal.rng) n_tests=1, rng=TestFillDiagonal.rng)
utt.verify_grad(fill_diagonal, [numpy.random.rand(8, 5), utt.verify_grad(fill_diagonal, [np.random.rand(8, 5),
numpy.random.rand()], np.random.rand()],
n_tests=1, rng=TestFillDiagonal.rng) n_tests=1, rng=TestFillDiagonal.rng)
def test_infer_shape(self): def test_infer_shape(self):
...@@ -565,20 +564,20 @@ class TestFillDiagonal(utt.InferShapeTester): ...@@ -565,20 +564,20 @@ class TestFillDiagonal(utt.InferShapeTester):
x = tensor.dmatrix() x = tensor.dmatrix()
y = tensor.dscalar() y = tensor.dscalar()
self._compile_and_check([x, y], [self.op(x, y)], self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(8, 5), [np.random.rand(8, 5),
numpy.random.rand()], np.random.rand()],
self.op_class) self.op_class)
self._compile_and_check([z, y], [self.op(z, y)], self._compile_and_check([z, y], [self.op(z, y)],
# must be square when nd>2 # must be square when nd>2
[numpy.random.rand(8, 8, 8), [np.random.rand(8, 8, 8),
numpy.random.rand()], np.random.rand()],
self.op_class, self.op_class,
warn=False) warn=False)
class TestFillDiagonalOffset(utt.InferShapeTester): class TestFillDiagonalOffset(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestFillDiagonalOffset, self).setUp() super(TestFillDiagonalOffset, self).setUp()
...@@ -593,11 +592,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -593,11 +592,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
f = function([x, y, z], fill_diagonal_offset(x, y, z)) f = function([x, y, z], fill_diagonal_offset(x, y, z))
for test_offset in (-5, -4, -1, 0, 1, 4, 5): for test_offset in (-5, -4, -1, 0, 1, 4, 5):
for shp in [(8, 8), (5, 8), (8, 5), (5, 5)]: for shp in [(8, 8), (5, 8), (8, 5), (5, 5)]:
a = numpy.random.rand(*shp).astype(config.floatX) a = np.random.rand(*shp).astype(config.floatX)
val = numpy.cast[config.floatX](numpy.random.rand()) val = np.cast[config.floatX](np.random.rand())
out = f(a, val, test_offset) out = f(a, val, test_offset)
# We can't use numpy.fill_diagonal as it is bugged. # We can't use np.fill_diagonal as it is bugged.
assert numpy.allclose(numpy.diag(out, test_offset), val) assert np.allclose(np.diag(out, test_offset), val)
if test_offset >= 0: if test_offset >= 0:
assert (out == val).sum() == min(min(a.shape), assert (out == val).sum() == min(min(a.shape),
a.shape[1] - test_offset) a.shape[1] - test_offset)
...@@ -612,13 +611,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -612,13 +611,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
return fill_diagonal_offset(a, val, test_offset) return fill_diagonal_offset(a, val, test_offset)
utt.verify_grad(fill_diagonal_with_fix_offset, utt.verify_grad(fill_diagonal_with_fix_offset,
[numpy.random.rand(5, 8), numpy.random.rand()], [np.random.rand(5, 8), np.random.rand()],
n_tests=1, rng=TestFillDiagonalOffset.rng) n_tests=1, rng=TestFillDiagonalOffset.rng)
utt.verify_grad(fill_diagonal_with_fix_offset, utt.verify_grad(fill_diagonal_with_fix_offset,
[numpy.random.rand(8, 5), numpy.random.rand()], [np.random.rand(8, 5), np.random.rand()],
n_tests=1, rng=TestFillDiagonalOffset.rng) n_tests=1, rng=TestFillDiagonalOffset.rng)
utt.verify_grad(fill_diagonal_with_fix_offset, utt.verify_grad(fill_diagonal_with_fix_offset,
[numpy.random.rand(5, 5), numpy.random.rand()], [np.random.rand(5, 5), np.random.rand()],
n_tests=1, rng=TestFillDiagonalOffset.rng) n_tests=1, rng=TestFillDiagonalOffset.rng)
def test_infer_shape(self): def test_infer_shape(self):
...@@ -627,13 +626,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester): ...@@ -627,13 +626,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
z = tensor.iscalar() z = tensor.iscalar()
for test_offset in (-5, -4, -1, 0, 1, 4, 5): for test_offset in (-5, -4, -1, 0, 1, 4, 5):
self._compile_and_check([x, y, z], [self.op(x, y, z)], self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(8, 5), [np.random.rand(8, 5),
numpy.random.rand(), np.random.rand(),
test_offset], test_offset],
self.op_class) self.op_class)
self._compile_and_check([x, y, z], [self.op(x, y, z)], self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(5, 8), [np.random.rand(5, 8),
numpy.random.rand(), np.random.rand(),
test_offset], test_offset],
self.op_class) self.op_class)
...@@ -644,7 +643,7 @@ def test_to_one_hot(): ...@@ -644,7 +643,7 @@ def test_to_one_hot():
f = theano.function([v], o) f = theano.function([v], o)
out = f([1, 2, 3, 5, 6]) out = f([1, 2, 3, 5, 6])
assert out.dtype == theano.config.floatX assert out.dtype == theano.config.floatX
assert numpy.allclose( assert np.allclose(
out, out,
[[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
...@@ -657,7 +656,7 @@ def test_to_one_hot(): ...@@ -657,7 +656,7 @@ def test_to_one_hot():
f = theano.function([v], o) f = theano.function([v], o)
out = f([1, 2, 3, 5, 6]) out = f([1, 2, 3, 5, 6])
assert out.dtype == "int32" assert out.dtype == "int32"
assert numpy.allclose( assert np.allclose(
out, out,
[[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论