提交 429a8fae authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #5843 from Amrithasuresh/master

Updated numpy as np #4218
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)]
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
""" """
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
from numpy.random import randn from numpy.random import randn
import theano import theano
...@@ -41,10 +41,10 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -41,10 +41,10 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
batchSize = 2 batchSize = 2
input = randn(batchSize, inputWindowSize, inputSize).astype('float32') input = randn(batchSize, inputWindowSize, inputSize).astype('float32')
permutation = numpy.random.permutation permutation = np.random.permutation
inputIndice = numpy.vstack(permutation(nInputBlock)[:inputWindowSize] inputIndice = np.vstack(permutation(nInputBlock)[:inputWindowSize]
for _ in range(batchSize)).astype('int32') for _ in range(batchSize)).astype('int32')
outputIndice = numpy.vstack( outputIndice = np.vstack(
permutation(nOutputBlock)[:outputWindowSize] permutation(nOutputBlock)[:outputWindowSize]
for _ in range(batchSize)).astype('int32') for _ in range(batchSize)).astype('int32')
weight = randn(nInputBlock, nOutputBlock, weight = randn(nInputBlock, nOutputBlock,
...@@ -66,11 +66,11 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -66,11 +66,11 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
o = randn(nInputBlock, nOutputBlock, xSize, ySize).astype('float32') o = randn(nInputBlock, nOutputBlock, xSize, ySize).astype('float32')
x = randn(batchSize, xWindowSize, xSize).astype('float32') x = randn(batchSize, xWindowSize, xSize).astype('float32')
y = randn(batchSize, yWindowSize, ySize).astype('float32') y = randn(batchSize, yWindowSize, ySize).astype('float32')
randint = numpy.random.randint randint = np.random.randint
xIdx = numpy.vstack(randint(0, nInputBlock, size=xWindowSize) xIdx = np.vstack(randint(0, nInputBlock, size=xWindowSize)
for _ in range(batchSize)).astype('int32') for _ in range(batchSize)).astype('int32')
yIdx = numpy.vstack(randint(0, nOutputBlock, size=yWindowSize) yIdx = np.vstack(randint(0, nOutputBlock, size=yWindowSize)
for _ in range(batchSize)).astype('int32') for _ in range(batchSize)).astype('int32')
return o, x, y, xIdx, yIdx return o, x, y, xIdx, yIdx
...@@ -82,7 +82,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -82,7 +82,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for i in range(h.shape[1]): for i in range(h.shape[1]):
inputIdx = iIdx[b, i] inputIdx = iIdx[b, i]
w = W[inputIdx, outputIdx] w = W[inputIdx, outputIdx]
o[b, j, :] += numpy.dot(h[b, i], w) o[b, j, :] += np.dot(h[b, i], w)
return o return o
@staticmethod @staticmethod
...@@ -94,7 +94,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -94,7 +94,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for b in range(o.shape[0]): for b in range(o.shape[0]):
w = W[ix_(iIdx[b], oIdx[b])].swapaxes(1, 2) w = W[ix_(iIdx[b], oIdx[b])].swapaxes(1, 2)
w = w.reshape((w.shape[0] * w.shape[1], w.shape[2] * w.shape[3])) w = w.reshape((w.shape[0] * w.shape[1], w.shape[2] * w.shape[3]))
o[b] += numpy.dot(h[b].ravel(), w).reshape(o.shape[1:]) o[b] += np.dot(h[b].ravel(), w).reshape(o.shape[1:])
return o return o
@staticmethod @staticmethod
...@@ -108,8 +108,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -108,8 +108,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
# The next three lines do the same operation. The last one is the # The next three lines do the same operation. The last one is the
# fastest # fastest
# o[b] += (h[b][:, None, :, None] * w).sum(axis=(0, 2)) # o[b] += (h[b][:, None, :, None] * w).sum(axis=(0, 2))
# o[b] += numpy.tensordot(h[b], w, [(0,1),(0,2)]) # o[b] += np.tensordot(h[b], w, [(0,1),(0,2)])
o[b] += numpy.einsum('ik,ijkl', h[b], w) o[b] += np.einsum('ik,ijkl', h[b], w)
return o return o
@staticmethod @staticmethod
...@@ -117,8 +117,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -117,8 +117,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
for b in range(x.shape[0]): for b in range(x.shape[0]):
for i in range(xIdx.shape[1]): for i in range(xIdx.shape[1]):
for j in range(yIdx.shape[1]): for j in range(yIdx.shape[1]):
o[xIdx[b, i], yIdx[b, j]] += numpy.outer(x[b, i, :], o[xIdx[b, i], yIdx[b, j]] += np.outer(x[b, i, :],
y[b, j, :]) y[b, j, :])
return o return o
def test_sparseblockdot(self): def test_sparseblockdot(self):
...@@ -190,7 +190,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -190,7 +190,7 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
W_val, h_val, iIdx_val, b_val, oIdx_val = \ W_val, h_val, iIdx_val, b_val, oIdx_val = \
BlockSparse_Gemv_and_Outer.gemv_data() BlockSparse_Gemv_and_Outer.gemv_data()
th_out = f(numpy.swapaxes(W_val, 2, 3), h_val, iIdx_val, b_val, th_out = f(np.swapaxes(W_val, 2, 3), h_val, iIdx_val, b_val,
oIdx_val) oIdx_val)
ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy( ref_out = BlockSparse_Gemv_and_Outer.gemv_numpy(
b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val) b_val.take(oIdx_val, axis=0), W_val, h_val, iIdx_val, oIdx_val)
...@@ -218,8 +218,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester): ...@@ -218,8 +218,8 @@ class BlockSparse_Gemv_and_Outer(utt.InferShapeTester):
def test_sparseblockgemv_grad_1(self): def test_sparseblockgemv_grad_1(self):
# Test that we correctly handle cases where dimensions are 1. # Test that we correctly handle cases where dimensions are 1.
h_val = randn(1, 1, 1).astype('float32') h_val = randn(1, 1, 1).astype('float32')
iIdx_val = numpy.random.permutation(1)[:1][None, :] iIdx_val = np.random.permutation(1)[:1][None, :]
oIdx_val = numpy.random.permutation(1)[:1][None, :] oIdx_val = np.random.permutation(1)[:1][None, :]
W_val = randn(1, 1, 1, 1).astype('float32') W_val = randn(1, 1, 1, 1).astype('float32')
b_val = randn(1, 1).astype('float32') b_val = randn(1, 1).astype('float32')
......
...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division ...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import theano import theano
import theano.tensor as T import theano.tensor as T
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
import numpy import numpy as np
from theano.tensor.nnet import bn from theano.tensor.nnet import bn
...@@ -17,12 +17,12 @@ def test_BNComposite(): ...@@ -17,12 +17,12 @@ def test_BNComposite():
n = (x - M) / V n = (x - M) / V
return n * G + B return n * G + B
numpy.random.seed(1234) np.random.seed(1234)
X = 1 + numpy.random.random([10, 20]).astype('float32') X = 1 + np.random.random([10, 20]).astype('float32')
B = 1 + numpy.random.random([20]).astype('float32') B = 1 + np.random.random([20]).astype('float32')
G = 1 + numpy.random.random([20]).astype('float32') G = 1 + np.random.random([20]).astype('float32')
M = 1 + numpy.random.random([20]).astype('float32') M = 1 + np.random.random([20]).astype('float32')
V = 1 + numpy.random.random([20]).astype('float32') V = 1 + np.random.random([20]).astype('float32')
x = theano.tensor.matrix('x') x = theano.tensor.matrix('x')
b = theano.tensor.vector('b') b = theano.tensor.vector('b')
...@@ -30,11 +30,11 @@ def test_BNComposite(): ...@@ -30,11 +30,11 @@ def test_BNComposite():
m = theano.tensor.vector('m') m = theano.tensor.vector('m')
v = theano.tensor.vector('v') v = theano.tensor.vector('v')
x.tag.test_value = numpy.random.rand(2, 2).astype(theano.config.floatX) x.tag.test_value = np.random.rand(2, 2).astype(theano.config.floatX)
b.tag.test_value = numpy.random.rand(2).astype(theano.config.floatX) b.tag.test_value = np.random.rand(2).astype(theano.config.floatX)
g.tag.test_value = numpy.random.rand(2).astype(theano.config.floatX) g.tag.test_value = np.random.rand(2).astype(theano.config.floatX)
m.tag.test_value = numpy.random.rand(2).astype(theano.config.floatX) m.tag.test_value = np.random.rand(2).astype(theano.config.floatX)
v.tag.test_value = numpy.random.rand(2).astype(theano.config.floatX) v.tag.test_value = np.random.rand(2).astype(theano.config.floatX)
bn_ref_op = bn_ref(x, g, b, m, v) bn_ref_op = bn_ref(x, g, b, m, v)
f_ref = theano.function([x, b, g, m, v], [bn_ref_op]) f_ref = theano.function([x, b, g, m, v], [bn_ref_op])
...@@ -54,12 +54,12 @@ def test_batch_normalization(): ...@@ -54,12 +54,12 @@ def test_batch_normalization():
n = (x - M) / V n = (x - M) / V
return n * G + B return n * G + B
numpy.random.seed(1234) np.random.seed(1234)
X = 1 + numpy.random.random([10, 20]).astype('float32') X = 1 + np.random.random([10, 20]).astype('float32')
B = 1 + numpy.random.random([20]).astype('float32') B = 1 + np.random.random([20]).astype('float32')
G = 1 + numpy.random.random([20]).astype('float32') G = 1 + np.random.random([20]).astype('float32')
M = 1 + numpy.random.random([20]).astype('float32') M = 1 + np.random.random([20]).astype('float32')
V = 1 + numpy.random.random([20]).astype('float32') V = 1 + np.random.random([20]).astype('float32')
x = theano.tensor.matrix('x') x = theano.tensor.matrix('x')
b = theano.tensor.vector('b') b = theano.tensor.vector('b')
...@@ -92,7 +92,7 @@ def test_batch_normalization(): ...@@ -92,7 +92,7 @@ def test_batch_normalization():
def bn_f(inputs, gamma, beta, mean, std): def bn_f(inputs, gamma, beta, mean, std):
return bn.batch_normalization(inputs, gamma, beta, mean, std, mode=mode) return bn.batch_normalization(inputs, gamma, beta, mean, std, mode=mode)
utt.verify_grad(bn_f, [X, G, B, utt.verify_grad(bn_f, [X, G, B,
X.mean(axis=0)[numpy.newaxis], X.std(axis=0)[numpy.newaxis]]) X.mean(axis=0)[np.newaxis], X.std(axis=0)[np.newaxis]])
def test_bn_feature_maps(): def test_bn_feature_maps():
...@@ -101,12 +101,12 @@ def test_bn_feature_maps(): ...@@ -101,12 +101,12 @@ def test_bn_feature_maps():
n = (x - M) / V n = (x - M) / V
return n * G + B return n * G + B
numpy.random.seed(1234) np.random.seed(1234)
X = 1 + numpy.random.random([2, 3, 4, 4]).astype('float32') X = 1 + np.random.random([2, 3, 4, 4]).astype('float32')
B = 1 + numpy.random.random([3]).astype('float32') B = 1 + np.random.random([3]).astype('float32')
G = 1 + numpy.random.random([3]).astype('float32') G = 1 + np.random.random([3]).astype('float32')
M = 1 + numpy.random.random([3]).astype('float32') M = 1 + np.random.random([3]).astype('float32')
V = 1 + numpy.random.random([3]).astype('float32') V = 1 + np.random.random([3]).astype('float32')
x = theano.tensor.tensor4('x') x = theano.tensor.tensor4('x')
b = theano.tensor.vector('b') b = theano.tensor.vector('b')
...@@ -205,20 +205,20 @@ def test_batch_normalization_train(): ...@@ -205,20 +205,20 @@ def test_batch_normalization_train():
data_shape = data_shape[:ndim] data_shape = data_shape[:ndim]
param_shape = tuple(1 if d in axes2 else s param_shape = tuple(1 if d in axes2 else s
for d, s in enumerate(data_shape)) for d, s in enumerate(data_shape))
X = 4 + 3 * numpy.random.randn(*data_shape).astype(theano.config.floatX) X = 4 + 3 * np.random.randn(*data_shape).astype(theano.config.floatX)
Dy = -1 + 2 * numpy.random.randn(*data_shape).astype(theano.config.floatX) Dy = -1 + 2 * np.random.randn(*data_shape).astype(theano.config.floatX)
Scale = numpy.random.randn(*param_shape).astype(theano.config.floatX) Scale = np.random.randn(*param_shape).astype(theano.config.floatX)
Bias = numpy.random.randn(*param_shape).astype(theano.config.floatX) Bias = np.random.randn(*param_shape).astype(theano.config.floatX)
Running_mean = numpy.random.randn(*param_shape).astype(theano.config.floatX) Running_mean = np.random.randn(*param_shape).astype(theano.config.floatX)
Running_var = numpy.random.randn(*param_shape).astype(theano.config.floatX) Running_var = np.random.randn(*param_shape).astype(theano.config.floatX)
outputs = f(X, Scale, Bias, Running_mean, Running_var, Dy) outputs = f(X, Scale, Bias, Running_mean, Running_var, Dy)
# compare outputs # compare outputs
utt.assert_allclose(outputs[0], outputs[0 + 5]) # out utt.assert_allclose(outputs[0], outputs[0 + 5]) # out
utt.assert_allclose(outputs[1], outputs[1 + 5]) # mean utt.assert_allclose(outputs[1], outputs[1 + 5]) # mean
utt.assert_allclose(outputs[2], outputs[2 + 5]) # invstd utt.assert_allclose(outputs[2], outputs[2 + 5]) # invstd
utt.assert_allclose(outputs[3], outputs[3 + 5]) # running_mean utt.assert_allclose(outputs[3], outputs[3 + 5]) # running_mean
utt.assert_allclose(numpy.nan_to_num(outputs[4]), utt.assert_allclose(np.nan_to_num(outputs[4]),
numpy.nan_to_num(outputs[4 + 5])) # running_var np.nan_to_num(outputs[4 + 5])) # running_var
# compare gradients # compare gradients
utt.assert_allclose(outputs[10], outputs[10 + 3], atol=1e-4) # dx utt.assert_allclose(outputs[10], outputs[10 + 3], atol=1e-4) # dx
utt.assert_allclose(outputs[11], outputs[11 + 3], rtol=2e-4, atol=1e-4) # dscale utt.assert_allclose(outputs[11], outputs[11 + 3], rtol=2e-4, atol=1e-4) # dscale
...@@ -245,10 +245,10 @@ def test_batch_normalization_train_without_running_averages(): ...@@ -245,10 +245,10 @@ def test_batch_normalization_train_without_running_averages():
bn.AbstractBatchNormTrainGrad)) bn.AbstractBatchNormTrainGrad))
for n in f.maker.fgraph.toposort()]) for n in f.maker.fgraph.toposort()])
# run # run
X = 4 + 3 * numpy.random.randn(*data_shape).astype(theano.config.floatX) X = 4 + 3 * np.random.randn(*data_shape).astype(theano.config.floatX)
Dy = -1 + 2 * numpy.random.randn(*data_shape).astype(theano.config.floatX) Dy = -1 + 2 * np.random.randn(*data_shape).astype(theano.config.floatX)
Scale = numpy.random.randn(*param_shape).astype(theano.config.floatX) Scale = np.random.randn(*param_shape).astype(theano.config.floatX)
Bias = numpy.random.randn(*param_shape).astype(theano.config.floatX) Bias = np.random.randn(*param_shape).astype(theano.config.floatX)
f(X, Scale, Bias, Dy) f(X, Scale, Bias, Dy)
...@@ -330,7 +330,7 @@ def test_batch_normalization_train_broadcast(): ...@@ -330,7 +330,7 @@ def test_batch_normalization_train_broadcast():
if theano.config.mode != "FAST_COMPILE": if theano.config.mode != "FAST_COMPILE":
assert len(nodes) == 1 assert len(nodes) == 1
assert isinstance(nodes[0].op, theano.compile.DeepCopyOp) assert isinstance(nodes[0].op, theano.compile.DeepCopyOp)
inputs = [numpy.asarray(numpy.random.rand(*((4,) * n)), x.dtype) inputs = [np.asarray(np.random.rand(*((4,) * n)), x.dtype)
for n in [x.ndim, scale.ndim, bias.ndim, for n in [x.ndim, scale.ndim, bias.ndim,
running_mean.ndim, running_var.ndim]] running_mean.ndim, running_var.ndim]]
assert 0.0 == f(*inputs) assert 0.0 == f(*inputs)
...@@ -381,12 +381,12 @@ def test_batch_normalization_test(): ...@@ -381,12 +381,12 @@ def test_batch_normalization_test():
data_shape = data_shape[:ndim] data_shape = data_shape[:ndim]
param_shape = tuple(1 if d in axes2 else s param_shape = tuple(1 if d in axes2 else s
for d, s in enumerate(data_shape)) for d, s in enumerate(data_shape))
X = 4 + 3 * numpy.random.randn(*data_shape).astype(theano.config.floatX) X = 4 + 3 * np.random.randn(*data_shape).astype(theano.config.floatX)
Dy = -1 + 2 * numpy.random.randn(*data_shape).astype(theano.config.floatX) Dy = -1 + 2 * np.random.randn(*data_shape).astype(theano.config.floatX)
Scale = numpy.random.randn(*param_shape).astype(theano.config.floatX) Scale = np.random.randn(*param_shape).astype(theano.config.floatX)
Bias = numpy.random.randn(*param_shape).astype(theano.config.floatX) Bias = np.random.randn(*param_shape).astype(theano.config.floatX)
Mean = numpy.random.randn(*param_shape).astype(theano.config.floatX) Mean = np.random.randn(*param_shape).astype(theano.config.floatX)
Var = numpy.random.rand(*param_shape).astype(theano.config.floatX) Var = np.random.rand(*param_shape).astype(theano.config.floatX)
outputs = f(X, Scale, Bias, Mean, Var, Dy) outputs = f(X, Scale, Bias, Mean, Var, Dy)
# compare outputs # compare outputs
utt.assert_allclose(outputs[0], outputs[1]) # out utt.assert_allclose(outputs[0], outputs[1]) # out
......
...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division ...@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import time import time
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import numpy import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as T
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -79,8 +79,8 @@ class TestConv2D(utt.InferShapeTester): ...@@ -79,8 +79,8 @@ class TestConv2D(utt.InferShapeTester):
theano_conv = theano.function([input, filters], output, mode=self.mode) theano_conv = theano.function([input, filters], output, mode=self.mode)
# initialize input and compute result # initialize input and compute result
image_data = numpy.random.random(N_image_shape).astype(self.dtype) image_data = np.random.random(N_image_shape).astype(self.dtype)
filter_data = numpy.random.random(N_filter_shape).astype(self.dtype) filter_data = np.random.random(N_filter_shape).astype(self.dtype)
try: try:
theano_output = theano_conv(image_data, filter_data) theano_output = theano_conv(image_data, filter_data)
except ValueError: except ValueError:
...@@ -97,20 +97,20 @@ class TestConv2D(utt.InferShapeTester): ...@@ -97,20 +97,20 @@ class TestConv2D(utt.InferShapeTester):
orig_image_data = image_data orig_image_data = image_data
if border_mode is not 'full': if border_mode is not 'full':
s = -1. s = -1.
out_shape2d = numpy.array(N_image_shape[-2:]) +\ out_shape2d = np.array(N_image_shape[-2:]) +\
s * numpy.array(N_filter_shape[-2:]) - s s * np.array(N_filter_shape[-2:]) - s
out_shape2d = numpy.ceil(out_shape2d / numpy.array(subsample)) out_shape2d = np.ceil(out_shape2d / np.array(subsample))
# avoid numpy deprecation # avoid numpy deprecation
out_shape2d = out_shape2d.astype('int32') out_shape2d = out_shape2d.astype('int32')
out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape2d) out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape2d)
ref_output = numpy.zeros(out_shape) ref_output = np.zeros(out_shape)
# loop over output feature maps # loop over output feature maps
ref_output.fill(0) ref_output.fill(0)
if border_mode == 'full': if border_mode == 'full':
image_data2 = numpy.zeros((N_image_shape[0], N_image_shape[1], image_data2 = np.zeros((N_image_shape[0], N_image_shape[1],
N_image_shape[2] + 2 * N_filter_shape[2] - 2, N_image_shape[2] + 2 * N_filter_shape[2] - 2,
N_image_shape[3] + 2 * N_filter_shape[3] - 2)) N_image_shape[3] + 2 * N_filter_shape[3] - 2))
image_data2[ image_data2[
:, :, N_filter_shape[2] - 1:N_filter_shape[2] - 1 + N_image_shape[2], :, :, N_filter_shape[2] - 1:N_filter_shape[2] - 1 + N_image_shape[2],
N_filter_shape[3] - 1:N_filter_shape[3] - 1 + N_image_shape[3]] = image_data N_filter_shape[3] - 1:N_filter_shape[3] - 1 + N_image_shape[3]] = image_data
...@@ -160,17 +160,17 @@ class TestConv2D(utt.InferShapeTester): ...@@ -160,17 +160,17 @@ class TestConv2D(utt.InferShapeTester):
def test_uint_image_shape_datatype(self): def test_uint_image_shape_datatype(self):
"""Tests for uint datatype in image_shape. """Tests for uint datatype in image_shape.
""" """
self.validate((2, 2, 3, numpy.uint8(3)), (3, 2, 3, 3), 'valid', verify_grad=False) self.validate((2, 2, 3, np.uint8(3)), (3, 2, 3, 3), 'valid', verify_grad=False)
self.validate((numpy.uint16(2), 2, 3, 3), (3, 2, 3, 3), 'valid', verify_grad=False) self.validate((np.uint16(2), 2, 3, 3), (3, 2, 3, 3), 'valid', verify_grad=False)
self.validate((2, numpy.uint32(2), 3, 3), (3, 2, 3, 3), 'valid', verify_grad=False) self.validate((2, np.uint32(2), 3, 3), (3, 2, 3, 3), 'valid', verify_grad=False)
def test_uint_filter_shape_datatype(self): def test_uint_filter_shape_datatype(self):
"""Tests for uint datatype in filter_shape """Tests for uint datatype in filter_shape
""" """
self.validate((3, 2, 3, 3), (2, 2, 3, numpy.uint8(3)), 'valid', verify_grad=False) self.validate((3, 2, 3, 3), (2, 2, 3, np.uint8(3)), 'valid', verify_grad=False)
self.validate((3, 2, 3, 3), (numpy.uint16(2), 2, 3, 3), 'valid', verify_grad=False) self.validate((3, 2, 3, 3), (np.uint16(2), 2, 3, 3), 'valid', verify_grad=False)
self.validate((3, 2, 3, 3), (2, numpy.uint32(2), 3, 3), 'valid', verify_grad=False) self.validate((3, 2, 3, 3), (2, np.uint32(2), 3, 3), 'valid', verify_grad=False)
def test_img_kernel_same_shape(self): def test_img_kernel_same_shape(self):
self.validate((3, 2, 3, 3), (4, 2, 3, 3), 'full') self.validate((3, 2, 3, 3), (4, 2, 3, 3), 'full')
...@@ -474,8 +474,8 @@ class TestConv2D(utt.InferShapeTester): ...@@ -474,8 +474,8 @@ class TestConv2D(utt.InferShapeTester):
print("filter_shapes", filter_shapes) print("filter_shapes", filter_shapes)
for filter_shape in filter_shapes: for filter_shape in filter_shapes:
input = theano.shared(numpy.random.random(image_shape)) input = theano.shared(np.random.random(image_shape))
filters = theano.shared(numpy.random.random(filter_shape)) filters = theano.shared(np.random.random(filter_shape))
output = self.conv2d( output = self.conv2d(
input, filters, input, filters,
...@@ -498,7 +498,7 @@ class TestConv2D(utt.InferShapeTester): ...@@ -498,7 +498,7 @@ class TestConv2D(utt.InferShapeTester):
# must be provided explicitly # must be provided explicitly
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
adtens = T.dtensor4() adtens = T.dtensor4()
......
...@@ -3,7 +3,7 @@ import time ...@@ -3,7 +3,7 @@ import time
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose_parameterized import parameterized from nose_parameterized import parameterized
import numpy import numpy as np
try: try:
from scipy import ndimage from scipy import ndimage
except ImportError: except ImportError:
...@@ -17,14 +17,14 @@ import theano.tests.unittest_tools as utt ...@@ -17,14 +17,14 @@ import theano.tests.unittest_tools as utt
def test_get_diagonal_subtensor_view(wrap=lambda a: a): def test_get_diagonal_subtensor_view(wrap=lambda a: a):
x = numpy.arange(20).reshape(5, 4).astype('float32') x = np.arange(20).reshape(5, 4).astype('float32')
x = wrap(x) x = wrap(x)
xv01 = get_diagonal_subtensor_view(x, 0, 1) xv01 = get_diagonal_subtensor_view(x, 0, 1)
# test that it works in 2d # test that it works in 2d
assert numpy.all(numpy.asarray(xv01) == [[12, 9, 6, 3], [16, 13, 10, 7]]) assert np.all(np.asarray(xv01) == [[12, 9, 6, 3], [16, 13, 10, 7]])
x = numpy.arange(24).reshape(4, 3, 2) x = np.arange(24).reshape(4, 3, 2)
xv01 = get_diagonal_subtensor_view(x, 0, 1) xv01 = get_diagonal_subtensor_view(x, 0, 1)
xv02 = get_diagonal_subtensor_view(x, 0, 2) xv02 = get_diagonal_subtensor_view(x, 0, 2)
xv12 = get_diagonal_subtensor_view(x, 1, 2) xv12 = get_diagonal_subtensor_view(x, 1, 2)
...@@ -32,11 +32,11 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a): ...@@ -32,11 +32,11 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
# print 'x', x # print 'x', x
# print 'xv01', xv01 # print 'xv01', xv01
# print 'xv02', xv02 # print 'xv02', xv02
assert numpy.all(numpy.asarray(xv01) == [ assert np.all(np.asarray(xv01) == [
[[12, 13], [8, 9], [4, 5]], [[12, 13], [8, 9], [4, 5]],
[[18, 19], [14, 15], [10, 11]]]) [[18, 19], [14, 15], [10, 11]]])
assert numpy.all(numpy.asarray(xv02) == [ assert np.all(np.asarray(xv02) == [
[[6, 1], [8, 3], [10, 5]], [[6, 1], [8, 3], [10, 5]],
[[12, 7], [14, 9], [16, 11]], [[12, 7], [14, 9], [16, 11]],
[[18, 13], [20, 15], [22, 17]], [[18, 13], [20, 15], [22, 17]],
...@@ -45,7 +45,7 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a): ...@@ -45,7 +45,7 @@ def test_get_diagonal_subtensor_view(wrap=lambda a: a):
# diagonal views of each leading matrix is the same # diagonal views of each leading matrix is the same
# as the slices out of the diagonal view of the entire 3d tensor # as the slices out of the diagonal view of the entire 3d tensor
for xi, xvi in zip(x, xv12): for xi, xvi in zip(x, xv12):
assert numpy.all(xvi == get_diagonal_subtensor_view(xi, 0, 1)) assert np.all(xvi == get_diagonal_subtensor_view(xi, 0, 1))
def pyconv3d(signals, filters, border_mode='valid'): def pyconv3d(signals, filters, border_mode='valid'):
...@@ -68,8 +68,8 @@ def pyconv3d(signals, filters, border_mode='valid'): ...@@ -68,8 +68,8 @@ def pyconv3d(signals, filters, border_mode='valid'):
if Tpad > 0 or Hpad > 0 or Wpad > 0: if Tpad > 0 or Hpad > 0 or Wpad > 0:
# zero-pad signals # zero-pad signals
signals_padded = numpy.zeros((Ns, Ts + 2 * Tpad, C, signals_padded = np.zeros((Ns, Ts + 2 * Tpad, C,
Hs + 2 * Hpad, Ws + 2 * Wpad), 'float32') Hs + 2 * Hpad, Ws + 2 * Wpad), 'float32')
signals_padded[:, Tpad:(Ts + Tpad), :, Hpad:(Hs + Hpad), signals_padded[:, Tpad:(Ts + Tpad), :, Hpad:(Hs + Hpad),
Wpad:(Ws + Wpad)] = signals Wpad:(Ws + Wpad)] = signals
Ns, Ts, C, Hs, Ws = signals_padded.shape Ns, Ts, C, Hs, Ws = signals_padded.shape
...@@ -79,7 +79,7 @@ def pyconv3d(signals, filters, border_mode='valid'): ...@@ -79,7 +79,7 @@ def pyconv3d(signals, filters, border_mode='valid'):
Hf2 = Hf // 2 Hf2 = Hf // 2
Wf2 = Wf // 2 Wf2 = Wf // 2
rval = numpy.zeros((Ns, Ts - Tf + 1, Nf, Hs - Hf + 1, Ws - Wf + 1)) rval = np.zeros((Ns, Ts - Tf + 1, Nf, Hs - Hf + 1, Ws - Wf + 1))
for ns in xrange(Ns): for ns in xrange(Ns):
for nf in xrange(Nf): for nf in xrange(Nf):
for c in xrange(C): for c in xrange(C):
...@@ -113,8 +113,8 @@ def test_conv3d(border_mode): ...@@ -113,8 +113,8 @@ def test_conv3d(border_mode):
Ns, Ts, C, Hs, Ws = 3, 10, 3, 32, 32 Ns, Ts, C, Hs, Ws = 3, 10, 3, 32, 32
Nf, Tf, C, Hf, Wf = 32, 5, 3, 5, 5 Nf, Tf, C, Hf, Wf = 32, 5, 3, 5, 5
signals = numpy.arange(Ns * Ts * C * Hs * Ws).reshape(Ns, Ts, C, Hs, Ws).astype('float32') signals = np.arange(Ns * Ts * C * Hs * Ws).reshape(Ns, Ts, C, Hs, Ws).astype('float32')
filters = numpy.arange(Nf * Tf * C * Hf * Wf).reshape(Nf, Tf, C, Hf, Wf).astype('float32') filters = np.arange(Nf * Tf * C * Hf * Wf).reshape(Nf, Tf, C, Hf, Wf).astype('float32')
t0 = time.time() t0 = time.time()
pyres = pyconv3d(signals, filters, border_mode) pyres = pyconv3d(signals, filters, border_mode)
...@@ -153,8 +153,8 @@ def test_conv3d(border_mode): ...@@ -153,8 +153,8 @@ def test_conv3d(border_mode):
Ns, Ts, C, Hs, Ws = 3, 3, 3, 5, 5 Ns, Ts, C, Hs, Ws = 3, 3, 3, 5, 5
Nf, Tf, C, Hf, Wf = 4, 2, 3, 2, 2 Nf, Tf, C, Hf, Wf = 4, 2, 3, 2, 2
signals = numpy.random.rand(Ns, Ts, C, Hs, Ws).astype('float32') signals = np.random.rand(Ns, Ts, C, Hs, Ws).astype('float32')
filters = numpy.random.rand(Nf, Tf, C, Hf, Wf).astype('float32') filters = np.random.rand(Nf, Tf, C, Hf, Wf).astype('float32')
utt.verify_grad(lambda s, f: conv3d(s, f, border_mode=border_mode), utt.verify_grad(lambda s, f: conv3d(s, f, border_mode=border_mode),
[signals, filters], eps=1e-1, mode=mode) [signals, filters], eps=1e-1, mode=mode)
...@@ -162,8 +162,8 @@ def test_conv3d(border_mode): ...@@ -162,8 +162,8 @@ def test_conv3d(border_mode):
Ns, Ts, C, Hs, Ws = 3, 10, 3, 32, 32 Ns, Ts, C, Hs, Ws = 3, 10, 3, 32, 32
Nf, Tf, C, Hf, Wf = 32, 1, 3, 5, 5 Nf, Tf, C, Hf, Wf = 32, 1, 3, 5, 5
signals = numpy.arange(Ns * Ts * C * Hs * Ws).reshape(Ns, Ts, C, Hs, Ws).astype('float32') signals = np.arange(Ns * Ts * C * Hs * Ws).reshape(Ns, Ts, C, Hs, Ws).astype('float32')
filters = numpy.arange(Nf * Tf * C * Hf * Wf).reshape(Nf, Tf, C, Hf, Wf).astype('float32') filters = np.arange(Nf * Tf * C * Hf * Wf).reshape(Nf, Tf, C, Hf, Wf).astype('float32')
t0 = time.time() t0 = time.time()
pyres = pyconv3d(signals, filters, border_mode) pyres = pyconv3d(signals, filters, border_mode)
...@@ -200,7 +200,7 @@ def test_conv3d(border_mode): ...@@ -200,7 +200,7 @@ def test_conv3d(border_mode):
Ns, Ts, C, Hs, Ws = 3, 3, 3, 5, 5 Ns, Ts, C, Hs, Ws = 3, 3, 3, 5, 5
Nf, Tf, C, Hf, Wf = 4, 1, 3, 2, 2 Nf, Tf, C, Hf, Wf = 4, 1, 3, 2, 2
signals = numpy.random.rand(Ns, Ts, C, Hs, Ws).astype('float32') signals = np.random.rand(Ns, Ts, C, Hs, Ws).astype('float32')
filters = numpy.random.rand(Nf, Tf, C, Hf, Wf).astype('float32') filters = np.random.rand(Nf, Tf, C, Hf, Wf).astype('float32')
utt.verify_grad(lambda s, f: conv3d(s, f, border_mode=border_mode), utt.verify_grad(lambda s, f: conv3d(s, f, border_mode=border_mode),
[signals, filters], eps=1e-1, mode=mode) [signals, filters], eps=1e-1, mode=mode)
...@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division ...@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from nose.tools import assert_equals from nose.tools import assert_equals
import numpy import numpy as np
from six import integer_types from six import integer_types
import theano import theano
...@@ -66,15 +66,15 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -66,15 +66,15 @@ class TestCorr2D(utt.InferShapeTester):
theano_corr = theano.function([input, filters], output, mode=self.mode) theano_corr = theano.function([input, filters], output, mode=self.mode)
# initialize input and compute result # initialize input and compute result
image_data = numpy.random.random(N_image_shape).astype(self.dtype) image_data = np.random.random(N_image_shape).astype(self.dtype)
filter_data = numpy.random.random(N_filter_shape).astype(self.dtype) filter_data = np.random.random(N_filter_shape).astype(self.dtype)
if non_contiguous: if non_contiguous:
image_data = numpy.transpose(image_data, axes=(0, 1, 3, 2)) image_data = np.transpose(image_data, axes=(0, 1, 3, 2))
image_data = image_data.copy() image_data = image_data.copy()
image_data = numpy.transpose(image_data, axes=(0, 1, 3, 2)) image_data = np.transpose(image_data, axes=(0, 1, 3, 2))
filter_data = numpy.transpose(filter_data, axes=(0, 1, 3, 2)) filter_data = np.transpose(filter_data, axes=(0, 1, 3, 2))
filter_data = filter_data.copy() filter_data = filter_data.copy()
filter_data = numpy.transpose(filter_data, axes=(0, 1, 3, 2)) filter_data = np.transpose(filter_data, axes=(0, 1, 3, 2))
assert not image_data.flags['CONTIGUOUS'] assert not image_data.flags['CONTIGUOUS']
assert not filter_data.flags['CONTIGUOUS'] assert not filter_data.flags['CONTIGUOUS']
...@@ -82,38 +82,38 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -82,38 +82,38 @@ class TestCorr2D(utt.InferShapeTester):
# REFERENCE IMPLEMENTATION # REFERENCE IMPLEMENTATION
# Testing correlation, not convolution. Reverse filters. # Testing correlation, not convolution. Reverse filters.
filter_data_corr = numpy.array(filter_data[:, :, ::-1, ::-1], filter_data_corr = np.array(filter_data[:, :, ::-1, ::-1],
copy=True, copy=True,
order='C') order='C')
orig_image_data = image_data orig_image_data = image_data
img_shape2d = numpy.array(N_image_shape[-2:]) img_shape2d = np.array(N_image_shape[-2:])
fil_shape2d = numpy.array(N_filter_shape[-2:]) fil_shape2d = np.array(N_filter_shape[-2:])
dil_shape2d = numpy.array(filter_dilation) dil_shape2d = np.array(filter_dilation)
dil_fil_shape2d = (fil_shape2d - 1) * dil_shape2d + 1 dil_fil_shape2d = (fil_shape2d - 1) * dil_shape2d + 1
subsample2d = numpy.array(subsample) subsample2d = np.array(subsample)
if border_mode == 'full': if border_mode == 'full':
padHW = (dil_fil_shape2d - 1) padHW = (dil_fil_shape2d - 1)
elif border_mode == 'valid': elif border_mode == 'valid':
padHW = numpy.array([0, 0]) padHW = np.array([0, 0])
elif border_mode == 'half': elif border_mode == 'half':
padHW = numpy.floor(dil_fil_shape2d / 2).astype('int32') padHW = np.floor(dil_fil_shape2d / 2).astype('int32')
elif isinstance(border_mode, tuple): elif isinstance(border_mode, tuple):
padHW = numpy.array(border_mode) padHW = np.array(border_mode)
elif isinstance(border_mode, integer_types): elif isinstance(border_mode, integer_types):
padHW = numpy.array([border_mode, border_mode]) padHW = np.array([border_mode, border_mode])
else: else:
raise NotImplementedError('Unsupported border_mode {}'.format(border_mode)) raise NotImplementedError('Unsupported border_mode {}'.format(border_mode))
out_shape2d = numpy.floor((img_shape2d + 2 * (padHW) - dil_fil_shape2d) / subsample2d) + 1 out_shape2d = np.floor((img_shape2d + 2 * (padHW) - dil_fil_shape2d) / subsample2d) + 1
# avoid numpy deprecation # avoid numpy deprecation
out_shape2d = out_shape2d.astype('int32') out_shape2d = out_shape2d.astype('int32')
out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape2d) out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape2d)
ref_output = numpy.zeros(out_shape) ref_output = np.zeros(out_shape)
# loop over output feature maps # loop over output feature maps
ref_output.fill(0) ref_output.fill(0)
image_data2 = numpy.zeros((N_image_shape[0], N_image_shape[1], image_data2 = np.zeros((N_image_shape[0], N_image_shape[1],
N_image_shape[2] + 2 * padHW[0], N_image_shape[2] + 2 * padHW[0],
N_image_shape[3] + 2 * padHW[1])) N_image_shape[3] + 2 * padHW[1]))
image_data2[:, :, padHW[0]:padHW[0] + N_image_shape[2], image_data2[:, :, padHW[0]:padHW[0] + N_image_shape[2],
padHW[1]:padHW[1] + N_image_shape[3]] = image_data padHW[1]:padHW[1] + N_image_shape[3]] = image_data
image_data = image_data2 image_data = image_data2
...@@ -265,7 +265,7 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -265,7 +265,7 @@ class TestCorr2D(utt.InferShapeTester):
Checks dtype upcast for CorrMM methods. Checks dtype upcast for CorrMM methods.
""" """
def rand(shape, dtype='float64'): def rand(shape, dtype='float64'):
r = numpy.asarray(numpy.random.rand(*shape), dtype=dtype) r = np.asarray(np.random.rand(*shape), dtype=dtype)
return r * 2 - 1 return r * 2 - 1
if not theano.config.cxx: if not theano.config.cxx:
raise SkipTest("Need cxx to test conv2d") raise SkipTest("Need cxx to test conv2d")
...@@ -296,7 +296,7 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -296,7 +296,7 @@ class TestCorr2D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corrMM = corr.CorrMM corrMM = corr.CorrMM
...@@ -329,7 +329,7 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -329,7 +329,7 @@ class TestCorr2D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corrMM = corr.CorrMM corrMM = corr.CorrMM
gradW = corr.CorrMM_gradWeights gradW = corr.CorrMM_gradWeights
...@@ -369,7 +369,7 @@ class TestCorr2D(utt.InferShapeTester): ...@@ -369,7 +369,7 @@ class TestCorr2D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corrMM = corr.CorrMM corrMM = corr.CorrMM
gradI = corr.CorrMM_gradInputs gradI = corr.CorrMM_gradInputs
......
...@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division ...@@ -3,7 +3,7 @@ from __future__ import absolute_import, print_function, division
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from nose.tools import assert_equals from nose.tools import assert_equals
import numpy import numpy as np
from six import integer_types from six import integer_types
import theano import theano
...@@ -67,17 +67,17 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -67,17 +67,17 @@ class TestCorr3D(utt.InferShapeTester):
theano_corr = theano.function([input, filters], output, mode=self.mode) theano_corr = theano.function([input, filters], output, mode=self.mode)
# initialize input and compute result # initialize input and compute result
image_data = numpy.random.random(N_image_shape).astype(self.dtype) image_data = np.random.random(N_image_shape).astype(self.dtype)
filter_data = numpy.random.random(N_filter_shape).astype(self.dtype) filter_data = np.random.random(N_filter_shape).astype(self.dtype)
image_data /= 10 image_data /= 10
filter_data /= 10 filter_data /= 10
if non_contiguous: if non_contiguous:
image_data = numpy.transpose(image_data, axes=(0, 1, 4, 3, 2)) image_data = np.transpose(image_data, axes=(0, 1, 4, 3, 2))
image_data = image_data.copy() image_data = image_data.copy()
image_data = numpy.transpose(image_data, axes=(0, 1, 4, 3, 2)) image_data = np.transpose(image_data, axes=(0, 1, 4, 3, 2))
filter_data = numpy.transpose(filter_data, axes=(0, 1, 4, 3, 2)) filter_data = np.transpose(filter_data, axes=(0, 1, 4, 3, 2))
filter_data = filter_data.copy() filter_data = filter_data.copy()
filter_data = numpy.transpose(filter_data, axes=(0, 1, 4, 3, 2)) filter_data = np.transpose(filter_data, axes=(0, 1, 4, 3, 2))
assert not image_data.flags['CONTIGUOUS'] assert not image_data.flags['CONTIGUOUS']
assert not filter_data.flags['CONTIGUOUS'] assert not filter_data.flags['CONTIGUOUS']
...@@ -85,39 +85,39 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -85,39 +85,39 @@ class TestCorr3D(utt.InferShapeTester):
# REFERENCE IMPLEMENTATION # REFERENCE IMPLEMENTATION
# Testing correlation, not convolution. Reverse filters. # Testing correlation, not convolution. Reverse filters.
filter_data_corr = numpy.array(filter_data[:, :, ::-1, ::-1, ::-1], filter_data_corr = np.array(filter_data[:, :, ::-1, ::-1, ::-1],
copy=True, copy=True,
order='C') order='C')
orig_image_data = image_data orig_image_data = image_data
img_shape3d = numpy.array(N_image_shape[-3:]) img_shape3d = np.array(N_image_shape[-3:])
fil_shape3d = numpy.array(N_filter_shape[-3:]) fil_shape3d = np.array(N_filter_shape[-3:])
dil_shape3d = numpy.array(filter_dilation) dil_shape3d = np.array(filter_dilation)
dil_fil_shape3d = (fil_shape3d - 1) * dil_shape3d + 1 dil_fil_shape3d = (fil_shape3d - 1) * dil_shape3d + 1
subsample3d = numpy.array(subsample) subsample3d = np.array(subsample)
if border_mode == 'full': if border_mode == 'full':
padHWD = (dil_fil_shape3d - 1) padHWD = (dil_fil_shape3d - 1)
elif border_mode == 'valid': elif border_mode == 'valid':
padHWD = numpy.array([0, 0, 0]) padHWD = np.array([0, 0, 0])
elif border_mode == 'half': elif border_mode == 'half':
padHWD = numpy.floor(dil_fil_shape3d / 2).astype('int32') padHWD = np.floor(dil_fil_shape3d / 2).astype('int32')
elif isinstance(border_mode, tuple): elif isinstance(border_mode, tuple):
padHWD = numpy.array(border_mode) padHWD = np.array(border_mode)
elif isinstance(border_mode, integer_types): elif isinstance(border_mode, integer_types):
padHWD = numpy.array([border_mode, border_mode, border_mode]) padHWD = np.array([border_mode, border_mode, border_mode])
else: else:
raise NotImplementedError('Unsupported border_mode {}'.format(border_mode)) raise NotImplementedError('Unsupported border_mode {}'.format(border_mode))
out_shape3d = numpy.floor((img_shape3d + 2 * (padHWD) - dil_fil_shape3d) / subsample3d) + 1 out_shape3d = np.floor((img_shape3d + 2 * (padHWD) - dil_fil_shape3d) / subsample3d) + 1
# avoid numpy deprecation # avoid numpy deprecation
out_shape3d = out_shape3d.astype('int32') out_shape3d = out_shape3d.astype('int32')
out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape3d) out_shape = (N_image_shape[0], N_filter_shape[0]) + tuple(out_shape3d)
ref_output = numpy.zeros(out_shape) ref_output = np.zeros(out_shape)
# loop over output feature maps # loop over output feature maps
ref_output.fill(0) ref_output.fill(0)
image_data2 = numpy.zeros((N_image_shape[0], N_image_shape[1], image_data2 = np.zeros((N_image_shape[0], N_image_shape[1],
N_image_shape[2] + 2 * padHWD[0], N_image_shape[2] + 2 * padHWD[0],
N_image_shape[3] + 2 * padHWD[1], N_image_shape[3] + 2 * padHWD[1],
N_image_shape[4] + 2 * padHWD[2])) N_image_shape[4] + 2 * padHWD[2]))
image_data2[:, :, image_data2[:, :,
padHWD[0]:padHWD[0] + N_image_shape[2], padHWD[0]:padHWD[0] + N_image_shape[2],
padHWD[1]:padHWD[1] + N_image_shape[3], padHWD[1]:padHWD[1] + N_image_shape[3],
...@@ -283,7 +283,7 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -283,7 +283,7 @@ class TestCorr3D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(shape, dtype='float64'): def rand(shape, dtype='float64'):
r = numpy.asarray(numpy.random.rand(*shape), dtype=dtype) r = np.asarray(np.random.rand(*shape), dtype=dtype)
return r * 2 - 1 return r * 2 - 1
ops = [corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs] ops = [corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs]
...@@ -312,7 +312,7 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -312,7 +312,7 @@ class TestCorr3D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corr3dMM = corr3d.Corr3dMM corr3dMM = corr3d.Corr3dMM
...@@ -345,7 +345,7 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -345,7 +345,7 @@ class TestCorr3D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corr3dMM = corr3d.Corr3dMM corr3dMM = corr3d.Corr3dMM
gradW = corr3d.Corr3dMM_gradWeights gradW = corr3d.Corr3dMM_gradWeights
...@@ -386,7 +386,7 @@ class TestCorr3D(utt.InferShapeTester): ...@@ -386,7 +386,7 @@ class TestCorr3D(utt.InferShapeTester):
raise SkipTest("Need cxx for this test") raise SkipTest("Need cxx for this test")
def rand(*shape): def rand(*shape):
r = numpy.asarray(numpy.random.rand(*shape), dtype='float64') r = np.asarray(np.random.rand(*shape), dtype='float64')
return r * 2 - 1 return r * 2 - 1
corr3dMM = corr3d.Corr3dMM corr3dMM = corr3d.Corr3dMM
gradI = corr3d.Corr3dMM_gradInputs gradI = corr3d.Corr3dMM_gradInputs
......
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
from theano.compat import imap from theano.compat import imap
import theano.tensor.inplace import theano.tensor.inplace
...@@ -26,12 +26,12 @@ class T_sigmoid(unittest.TestCase): ...@@ -26,12 +26,12 @@ class T_sigmoid(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
utt.verify_grad(sigmoid, [numpy.random.rand(3, 4)]) utt.verify_grad(sigmoid, [np.random.rand(3, 4)])
SigmoidTester = makeBroadcastTester( SigmoidTester = makeBroadcastTester(
op=sigmoid, op=sigmoid,
expected=upcast_int8_nfunc(lambda inputs: check_floatX( expected=upcast_int8_nfunc(lambda inputs: check_floatX(
inputs, 1 / (1 + numpy.exp(-inputs)))), inputs, 1 / (1 + np.exp(-inputs)))),
good=copymod(_good_broadcast_unary_normal_no_complex, good=copymod(_good_broadcast_unary_normal_no_complex,
without=['uint16']), # The reason that 'uint16' is excluted is that without=['uint16']), # The reason that 'uint16' is excluted is that
# theano works well but numpy overflows resulting # theano works well but numpy overflows resulting
...@@ -43,7 +43,7 @@ SigmoidTester = makeBroadcastTester( ...@@ -43,7 +43,7 @@ SigmoidTester = makeBroadcastTester(
UltraFastSigmoidTester = makeBroadcastTester( UltraFastSigmoidTester = makeBroadcastTester(
op=ultra_fast_sigmoid, op=ultra_fast_sigmoid,
expected=upcast_int8_nfunc(lambda inputs: check_floatX( expected=upcast_int8_nfunc(lambda inputs: check_floatX(
inputs, 1 / (1 + numpy.exp(-inputs)))), inputs, 1 / (1 + np.exp(-inputs)))),
good=copymod(_good_broadcast_unary_normal_no_complex, good=copymod(_good_broadcast_unary_normal_no_complex,
without=['uint16']), # numpy fucnting overflows with uint16. without=['uint16']), # numpy fucnting overflows with uint16.
# grad=_grad_broadcast_unary_normal, # grad=_grad_broadcast_unary_normal,
...@@ -54,7 +54,7 @@ UltraFastSigmoidTester = makeBroadcastTester( ...@@ -54,7 +54,7 @@ UltraFastSigmoidTester = makeBroadcastTester(
HardSigmoidTester = makeBroadcastTester( HardSigmoidTester = makeBroadcastTester(
op=hard_sigmoid, op=hard_sigmoid,
expected=upcast_int8_nfunc(lambda inputs: check_floatX( expected=upcast_int8_nfunc(lambda inputs: check_floatX(
inputs, 1 / (1 + numpy.exp(-inputs)))), inputs, 1 / (1 + np.exp(-inputs)))),
good=copymod(_good_broadcast_unary_normal_no_complex, good=copymod(_good_broadcast_unary_normal_no_complex,
without=['uint16']), # numpy fucnting overflows with uint16. without=['uint16']), # numpy fucnting overflows with uint16.
# grad=_grad_broadcast_unary_normal, # grad=_grad_broadcast_unary_normal,
...@@ -66,11 +66,11 @@ HardSigmoidTester = makeBroadcastTester( ...@@ -66,11 +66,11 @@ HardSigmoidTester = makeBroadcastTester(
SoftplusTester = makeBroadcastTester( SoftplusTester = makeBroadcastTester(
op=softplus, op=softplus,
expected=upcast_int8_nfunc(lambda inputs: check_floatX( expected=upcast_int8_nfunc(lambda inputs: check_floatX(
inputs, numpy.log1p(numpy.exp(inputs)))), inputs, np.log1p(np.exp(inputs)))),
good=dict(copymod(_good_broadcast_unary_normal_no_complex, good=dict(copymod(_good_broadcast_unary_normal_no_complex,
without=['uint8', 'uint16']), # numpy fucnting overflows with uint16. without=['uint8', 'uint16']), # numpy fucnting overflows with uint16.
uint8=[numpy.arange(0, 89, dtype='uint8')], # the range is different in new added uint8. uint8=[np.arange(0, 89, dtype='uint8')], # the range is different in new added uint8.
int8=[numpy.arange(-127, 89, dtype='int8')]), int8=[np.arange(-127, 89, dtype='int8')]),
# grad=_grad_broadcast_unary_normal, # grad=_grad_broadcast_unary_normal,
name='SoftplusTester', name='SoftplusTester',
) )
...@@ -81,7 +81,7 @@ class T_softplus(unittest.TestCase): ...@@ -81,7 +81,7 @@ class T_softplus(unittest.TestCase):
utt.seed_rng() utt.seed_rng()
def test_elemwise(self): def test_elemwise(self):
utt.verify_grad(softplus, [numpy.random.rand(3, 4)]) utt.verify_grad(softplus, [np.random.rand(3, 4)])
class T_sigmoid_opts(unittest.TestCase): class T_sigmoid_opts(unittest.TestCase):
...@@ -112,7 +112,7 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -112,7 +112,7 @@ class T_sigmoid_opts(unittest.TestCase):
m = self.get_mode(excluding=['local_elemwise_fusion']) m = self.get_mode(excluding=['local_elemwise_fusion'])
x = T.vector() x = T.vector()
data = numpy.random.rand(54).astype(config.floatX) data = np.random.rand(54).astype(config.floatX)
backup = config.warn.identify_1pexp_bug backup = config.warn.identify_1pexp_bug
config.warn.identify_1pexp_bug = False config.warn.identify_1pexp_bug = False
...@@ -321,7 +321,7 @@ class T_sigmoid_opts(unittest.TestCase): ...@@ -321,7 +321,7 @@ class T_sigmoid_opts(unittest.TestCase):
if not isinstance(mode, theano.compile.DebugMode): if not isinstance(mode, theano.compile.DebugMode):
f = theano.function([x, lr], ux, mode=mode) f = theano.function([x, lr], ux, mode=mode)
ux_v = f([[50]], 0.1) ux_v = f([[50]], 0.1)
assert not numpy.isnan(ux_v) assert not np.isnan(ux_v)
def test_local_ultra_fast_sigmoid(self): def test_local_ultra_fast_sigmoid(self):
x = tensor.matrix('x') x = tensor.matrix('x')
...@@ -391,7 +391,7 @@ class T_softplus_opts(unittest.TestCase): ...@@ -391,7 +391,7 @@ class T_softplus_opts(unittest.TestCase):
assert isinstance(topo[1].op.scalar_op, assert isinstance(topo[1].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus) theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54).astype(config.floatX)) f(np.random.rand(54).astype(config.floatX))
def test_log1msigm_to_softplus(self): def test_log1msigm_to_softplus(self):
x = T.matrix() x = T.matrix()
...@@ -404,7 +404,7 @@ class T_softplus_opts(unittest.TestCase): ...@@ -404,7 +404,7 @@ class T_softplus_opts(unittest.TestCase):
theano.tensor.nnet.sigm.ScalarSoftplus) theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg)
# assert check_stack_trace(f, ops_to_check='all') # assert check_stack_trace(f, ops_to_check='all')
f(numpy.random.rand(54, 11).astype(config.floatX)) f(np.random.rand(54, 11).astype(config.floatX))
# Same test with a flatten # Same test with a flatten
out = T.log(1 - T.flatten(sigmoid(x))) out = T.log(1 - T.flatten(sigmoid(x)))
...@@ -417,7 +417,7 @@ class T_softplus_opts(unittest.TestCase): ...@@ -417,7 +417,7 @@ class T_softplus_opts(unittest.TestCase):
assert isinstance(topo[1].op.scalar_op, assert isinstance(topo[1].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus) theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg) assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54, 11).astype(config.floatX)) f(np.random.rand(54, 11).astype(config.floatX))
# Same test with a reshape # Same test with a reshape
out = T.log(1 - sigmoid(x).reshape([x.size])) out = T.log(1 - sigmoid(x).reshape([x.size]))
...@@ -428,7 +428,7 @@ class T_softplus_opts(unittest.TestCase): ...@@ -428,7 +428,7 @@ class T_softplus_opts(unittest.TestCase):
assert any(isinstance(getattr(node.op, 'scalar_op', None), assert any(isinstance(getattr(node.op, 'scalar_op', None),
theano.tensor.nnet.sigm.ScalarSoftplus) theano.tensor.nnet.sigm.ScalarSoftplus)
for node in topo) for node in topo)
f(numpy.random.rand(54, 11).astype(config.floatX)) f(np.random.rand(54, 11).astype(config.floatX))
def test_log1pexp_to_softplus(self): def test_log1pexp_to_softplus(self):
m = theano.config.mode m = theano.config.mode
...@@ -446,7 +446,7 @@ class T_softplus_opts(unittest.TestCase): ...@@ -446,7 +446,7 @@ class T_softplus_opts(unittest.TestCase):
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op.scalar_op, assert isinstance(topo[0].op.scalar_op,
theano.tensor.nnet.sigm.ScalarSoftplus) theano.tensor.nnet.sigm.ScalarSoftplus)
f(numpy.random.rand(54).astype(config.floatX)) f(np.random.rand(54).astype(config.floatX))
class T_sigmoid_utils(unittest.TestCase): class T_sigmoid_utils(unittest.TestCase):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论