提交 51869d9e authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #5759 from Amrithasuresh/master

Updated numpy as np #4218
...@@ -8,7 +8,7 @@ __docformat__ = 'restructedtext en' ...@@ -8,7 +8,7 @@ __docformat__ = 'restructedtext en'
from collections import OrderedDict from collections import OrderedDict
import numpy import numpy as np
import theano import theano
import theano.tensor as T import theano.tensor as T
...@@ -17,12 +17,12 @@ import theano.tensor as T ...@@ -17,12 +17,12 @@ import theano.tensor as T
def gen_data(): def gen_data():
# generate the dataset # generate the dataset
train_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'), train_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64')) np.asarray(np.random.rand(10000)*10, dtype='int64'))
valid_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'), valid_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64')) np.asarray(np.random.rand(10000)*10, dtype='int64'))
test_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'), test_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64')) np.asarray(np.random.rand(10000)*10, dtype='int64'))
def shared_dataset(data_xy): def shared_dataset(data_xy):
""" Function that loads the dataset into shared variables """ Function that loads the dataset into shared variables
...@@ -33,8 +33,8 @@ def gen_data(): ...@@ -33,8 +33,8 @@ def gen_data():
variable) would lead to a large decrease in performance. variable) would lead to a large decrease in performance.
""" """
data_x, data_y = data_xy data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX)) shared_x = theano.shared(np.asarray(data_x, dtype=theano.config.floatX))
shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX)) shared_y = theano.shared(np.asarray(data_y, dtype=theano.config.floatX))
# When storing data on the GPU it has to be stored as floats # When storing data on the GPU it has to be stored as floats
# therefore we will store the labels as ``floatX`` as well # therefore we will store the labels as ``floatX`` as well
# (``shared_y`` does exactly that). But during our computations # (``shared_y`` does exactly that). But during our computations
...@@ -79,7 +79,7 @@ class LogisticRegression(object): ...@@ -79,7 +79,7 @@ class LogisticRegression(object):
""" """
# initialize with 0 the weights W as a matrix of shape (n_in, n_out) # initialize with 0 the weights W as a matrix of shape (n_in, n_out)
self.W = theano.shared(value=numpy.zeros((n_in, n_out), dtype=theano.config.floatX), self.W = theano.shared(value=np.zeros((n_in, n_out), dtype=theano.config.floatX),
name=name_prefix+'W') name=name_prefix+'W')
# compute vector of class-membership probabilities in symbolic form # compute vector of class-membership probabilities in symbolic form
...@@ -129,7 +129,7 @@ class HiddenLayer(object): ...@@ -129,7 +129,7 @@ class HiddenLayer(object):
Hidden unit activation is given by: tanh(dot(input,W) + b) Hidden unit activation is given by: tanh(dot(input,W) + b)
:type rng: numpy.random.RandomState :type rng: np.random.RandomState
:param rng: a random number generator used to initialize weights :param rng: a random number generator used to initialize weights
:type input: theano.tensor.dmatrix :type input: theano.tensor.dmatrix
...@@ -151,9 +151,9 @@ class HiddenLayer(object): ...@@ -151,9 +151,9 @@ class HiddenLayer(object):
# from -6./sqrt(n_in+n_hidden) and 6./sqrt(n_in+n_hidden) # from -6./sqrt(n_in+n_hidden) and 6./sqrt(n_in+n_hidden)
# the output of uniform if converted using asarray to dtype # the output of uniform if converted using asarray to dtype
# theano.config.floatX so that the code is runable on GPU # theano.config.floatX so that the code is runable on GPU
W_values = numpy.asarray( rng.uniform( \ W_values = np.asarray( rng.uniform( \
low=-numpy.sqrt(6./(n_in+n_out)), \ low=-np.sqrt(6./(n_in+n_out)), \
high=numpy.sqrt(6./(n_in+n_out)), \ high=np.sqrt(6./(n_in+n_out)), \
size=(n_in, n_out)), dtype=theano.config.floatX) size=(n_in, n_out)), dtype=theano.config.floatX)
self.W = theano.shared(value=W_values, name=name_prefix+'W') self.W = theano.shared(value=W_values, name=name_prefix+'W')
...@@ -176,7 +176,7 @@ class MLP(object): ...@@ -176,7 +176,7 @@ class MLP(object):
def __init__(self, rng, input, n_in, n_hidden, n_out): def __init__(self, rng, input, n_in, n_hidden, n_out):
"""Initialize the parameters for the multilayer perceptron """Initialize the parameters for the multilayer perceptron
:type rng: numpy.random.RandomState :type rng: np.random.RandomState
:param rng: a random number generator used to initialize weights :param rng: a random number generator used to initialize weights
:type input: theano.tensor.TensorType :type input: theano.tensor.TensorType
...@@ -265,7 +265,7 @@ def test_mlp(): ...@@ -265,7 +265,7 @@ def test_mlp():
y = T.ivector('y') # the labels are presented as 1D vector of y = T.ivector('y') # the labels are presented as 1D vector of
# [int] labels # [int] labels
rng = numpy.random.RandomState(1234) rng = np.random.RandomState(1234)
# construct the MLP class # construct the MLP class
classifier = MLP( rng=rng, input=x, n_in=28*28, n_hidden=500, n_out=10) classifier = MLP( rng=rng, input=x, n_in=28*28, n_hidden=500, n_out=10)
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -3,7 +3,7 @@ from copy import copy ...@@ -3,7 +3,7 @@ from copy import copy
from itertools import product as itertools_product from itertools import product as itertools_product
from unittest import TestCase from unittest import TestCase
import numpy import numpy as np
from numpy import (arange, array, common_type, complex64, complex128, float32, from numpy import (arange, array, common_type, complex64, complex128, float32,
float64, newaxis, shape, transpose, zeros) float64, newaxis, shape, transpose, zeros)
from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_almost_equal
...@@ -42,7 +42,7 @@ def test_dot_eq(): ...@@ -42,7 +42,7 @@ def test_dot_eq():
def sharedX(x, name): def sharedX(x, name):
return theano.shared(numpy.asarray(x, config.floatX), name=name) return theano.shared(np.asarray(x, config.floatX), name=name)
class t_gemm(TestCase): class t_gemm(TestCase):
...@@ -58,22 +58,22 @@ class t_gemm(TestCase): ...@@ -58,22 +58,22 @@ class t_gemm(TestCase):
def _gemm(z, a, x, y, b): def _gemm(z, a, x, y, b):
assert a.shape == () assert a.shape == ()
assert b.shape == () assert b.shape == ()
return b * z + a * numpy.dot(x, y) return b * z + a * np.dot(x, y)
@staticmethod @staticmethod
def rand(*args): def rand(*args):
return numpy.random.rand(*args) return np.random.rand(*args)
def cmp(self, z_, a_, x_, y_, b_): def cmp(self, z_, a_, x_, y_, b_):
for dtype in ['float32', 'float64', 'complex64', 'complex128']: for dtype in ['float32', 'float64', 'complex64', 'complex128']:
z = numpy.asarray(z_, dtype=dtype) z = np.asarray(z_, dtype=dtype)
a = numpy.asarray(a_, dtype=dtype) a = np.asarray(a_, dtype=dtype)
x = numpy.asarray(x_, dtype=dtype) x = np.asarray(x_, dtype=dtype)
y = numpy.asarray(y_, dtype=dtype) y = np.asarray(y_, dtype=dtype)
b = numpy.asarray(b_, dtype=dtype) b = np.asarray(b_, dtype=dtype)
def cmp_linker(z, a, x, y, b, l): def cmp_linker(z, a, x, y, b, l):
z, a, x, y, b = [numpy.asarray(p) for p in (z, a, x, y, b)] z, a, x, y, b = [np.asarray(p) for p in (z, a, x, y, b)]
z_orig = z.copy() z_orig = z.copy()
tz, ta, tx, ty, tb = [as_tensor_variable(p).type() tz, ta, tx, ty, tb = [as_tensor_variable(p).type()
for p in (z, a, x, y, b)] for p in (z, a, x, y, b)]
...@@ -91,7 +91,7 @@ class t_gemm(TestCase): ...@@ -91,7 +91,7 @@ class t_gemm(TestCase):
elif z_orig.size == 0: elif z_orig.size == 0:
self.assertTrue(z.size == 0) self.assertTrue(z.size == 0)
else: else:
self.assertFalse(numpy.all(z_orig == z)) self.assertFalse(np.all(z_orig == z))
cmp_linker(copy(z), a, x, y, b, 'c|py') cmp_linker(copy(z), a, x, y, b, 'c|py')
cmp_linker(copy(z), a, x, y, b, 'py') cmp_linker(copy(z), a, x, y, b, 'py')
...@@ -172,7 +172,7 @@ class t_gemm(TestCase): ...@@ -172,7 +172,7 @@ class t_gemm(TestCase):
a = T.matrix() a = T.matrix()
b = T.matrix() b = T.matrix()
c = T.matrix() c = T.matrix()
s = theano.shared(numpy.zeros((5, 5)).astype(config.floatX)) s = theano.shared(np.zeros((5, 5)).astype(config.floatX))
lr1 = T.constant(0.01).astype(config.floatX) lr1 = T.constant(0.01).astype(config.floatX)
lr2 = T.constant(2).astype(config.floatX) lr2 = T.constant(2).astype(config.floatX)
...@@ -331,7 +331,7 @@ class t_gemm(TestCase): ...@@ -331,7 +331,7 @@ class t_gemm(TestCase):
z, a, x, y, b = [theano._asarray(p, dtype=dt) z, a, x, y, b = [theano._asarray(p, dtype=dt)
for p in (z, a, x, y, b)] for p in (z, a, x, y, b)]
z_orig = z.copy() z_orig = z.copy()
z_after = numpy.zeros_like(z_orig) z_after = np.zeros_like(z_orig)
for i in xrange(3): for i in xrange(3):
z_after[:, :, i] = self._gemm(z[:, :, i], a, z_after[:, :, i] = self._gemm(z[:, :, i], a,
x[:, :, i], y[:, :, i], b) x[:, :, i], y[:, :, i], b)
...@@ -388,7 +388,7 @@ class t_as_scalar(TestCase): ...@@ -388,7 +388,7 @@ class t_as_scalar(TestCase):
def test0(self): def test0(self):
"""Test that it works on scalar constants""" """Test that it works on scalar constants"""
a = T.constant(2.5) a = T.constant(2.5)
b = T.constant(numpy.asarray([[[0.5]]])) b = T.constant(np.asarray([[[0.5]]]))
b2 = b.dimshuffle() b2 = b.dimshuffle()
assert b2.ndim == 0 assert b2.ndim == 0
d_a = T.DimShuffle([], [])(a) d_a = T.DimShuffle([], [])(a)
...@@ -403,7 +403,7 @@ class t_as_scalar(TestCase): ...@@ -403,7 +403,7 @@ class t_as_scalar(TestCase):
def test1(self): def test1(self):
"""Test that it fails on nonscalar constants""" """Test that it fails on nonscalar constants"""
a = T.constant(numpy.ones(5)) a = T.constant(np.ones(5))
self.assertTrue(None == _as_scalar(a)) self.assertTrue(None == _as_scalar(a))
self.assertTrue(None == _as_scalar(T.DimShuffle([False], [0, 'x'])(a))) self.assertTrue(None == _as_scalar(T.DimShuffle([False], [0, 'x'])(a)))
...@@ -482,13 +482,13 @@ def just_gemm(i, o, ishapes=[(4, 3), (3, 5), (4, 5), (), ()], ...@@ -482,13 +482,13 @@ def just_gemm(i, o, ishapes=[(4, 3), (3, 5), (4, 5), (), ()],
# theano.printing.debugprint(f) # theano.printing.debugprint(f)
assert False, 'graphlen=%i>%i' % (graphlen, max_graphlen) assert False, 'graphlen=%i>%i' % (graphlen, max_graphlen)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234)) rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[numpy.asarray(rng.randn(*sh), config.floatX) r0 = f(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes]) for sh in ishapes])
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234)) rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[numpy.asarray(rng.randn(*sh), config.floatX) r1 = g(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes]) for sh in ishapes])
max_abs_err = numpy.max(numpy.abs(r0[0] - r1[0])) max_abs_err = np.max(np.abs(r0[0] - r1[0]))
eps = 1.0e-8 eps = 1.0e-8
if config.floatX == 'float32': if config.floatX == 'float32':
eps = 1.0e-6 eps = 1.0e-6
...@@ -556,13 +556,13 @@ def test_gemm_opt_double_gemm(): ...@@ -556,13 +556,13 @@ def test_gemm_opt_double_gemm():
# for node in g.maker.fgraph.apply_nodes: # for node in g.maker.fgraph.apply_nodes:
# if node.op == gemm_inplace: raise Failure('gemm_inplace in graph') # if node.op == gemm_inplace: raise Failure('gemm_inplace in graph')
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234)) rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[numpy.asarray(rng.randn(*sh), config.floatX) r0 = f(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes]) for sh in ishapes])
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234)) rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[numpy.asarray(rng.randn(*sh), config.floatX) r1 = g(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes]) for sh in ishapes])
max_abs_err = numpy.max(numpy.abs(r0[0] - r1[0])) max_abs_err = np.max(np.abs(r0[0] - r1[0]))
eps = 1.0e-8 eps = 1.0e-8
if config.floatX == 'float32': if config.floatX == 'float32':
eps = 1.0e-6 eps = 1.0e-6
...@@ -652,7 +652,7 @@ def test_upcasting_scalar_nogemm(): ...@@ -652,7 +652,7 @@ def test_upcasting_scalar_nogemm():
f = theano.function([w, v, t, alpha], rval) f = theano.function([w, v, t, alpha], rval)
t = f.maker.fgraph.toposort() t = f.maker.fgraph.toposort()
assert numpy.sum([isinstance(n.op, Gemm) for n in t]) == 0 assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
v = T.fmatrix('v') v = T.fmatrix('v')
...@@ -669,7 +669,7 @@ def test_upcasting_scalar_nogemm(): ...@@ -669,7 +669,7 @@ def test_upcasting_scalar_nogemm():
config.on_opt_error = on_opt_error config.on_opt_error = on_opt_error
t = f.maker.fgraph.toposort() t = f.maker.fgraph.toposort()
assert numpy.sum([isinstance(n.op, Gemm) for n in t]) == 0 assert np.sum([isinstance(n.op, Gemm) for n in t]) == 0
#theano.printing.debugprint(f, print_type=True) #theano.printing.debugprint(f, print_type=True)
...@@ -763,13 +763,13 @@ def test_gemm_unrolled(): ...@@ -763,13 +763,13 @@ def test_gemm_unrolled():
""" """
batch_size = 100 batch_size = 100
rep_size = 40 rep_size = 40
rng = numpy.random.RandomState([1, 2, 3]) rng = np.random.RandomState([1, 2, 3])
for num_rounds in range(1, 10): for num_rounds in range(1, 10):
W = sharedX(rng.randn(rep_size, rep_size), name='W') W = sharedX(rng.randn(rep_size, rep_size), name='W')
V = sharedX(numpy.zeros((batch_size, rep_size)), name='V') V = sharedX(np.zeros((batch_size, rep_size)), name='V')
H = sharedX(numpy.zeros((batch_size, rep_size)), name='H') H = sharedX(np.zeros((batch_size, rep_size)), name='H')
G = sharedX(numpy.zeros((batch_size, rep_size)), name='G') G = sharedX(np.zeros((batch_size, rep_size)), name='G')
init_V = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_V') init_V = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_V')
init_H = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_H') init_H = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_H')
...@@ -844,7 +844,7 @@ def test_dot22(): ...@@ -844,7 +844,7 @@ def test_dot22():
else: else:
check = [isinstance(x.op, T.Dot) for x in topo] check = [isinstance(x.op, T.Dot) for x in topo]
assert any(check), (dtype1, dtype2) assert any(check), (dtype1, dtype2)
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
def cmp(a_shp, b_shp): def cmp(a_shp, b_shp):
av = rng.uniform(size=a_shp).astype(dtype1) av = rng.uniform(size=a_shp).astype(dtype1)
...@@ -867,7 +867,7 @@ def test_dot22scalar(): ...@@ -867,7 +867,7 @@ def test_dot22scalar():
# m = theano.compile.get_default_mode().including('local_dot_to_dot22', # m = theano.compile.get_default_mode().including('local_dot_to_dot22',
# 'local_dot22_to_dot22scalar','specialize') # 'local_dot22_to_dot22scalar','specialize')
#m = theano.compile.get_default_mode().including('BlasOpt', 'specialize') #m = theano.compile.get_default_mode().including('BlasOpt', 'specialize')
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
for dtype1 in ['complex64', 'complex128']: for dtype1 in ['complex64', 'complex128']:
a = T.matrix('a', dtype=dtype1) a = T.matrix('a', dtype=dtype1)
for dtype2 in ['complex64', 'complex128']: for dtype2 in ['complex64', 'complex128']:
...@@ -1069,7 +1069,7 @@ def test_dot_w_self(): ...@@ -1069,7 +1069,7 @@ def test_dot_w_self():
# normally be a gemm must not be because the output is aliased to # normally be a gemm must not be because the output is aliased to
# one of the inputs. # one of the inputs.
A = shared(value=numpy.ones((2, 2))) A = shared(value=np.ones((2, 2)))
B = T.matrix() B = T.matrix()
p = T.dot(A, A) * B p = T.dot(A, A) * B
...@@ -1078,7 +1078,7 @@ def test_dot_w_self(): ...@@ -1078,7 +1078,7 @@ def test_dot_w_self():
f = theano.function([B], p, updates=[(A, A - grad)]) f = theano.function([B], p, updates=[(A, A - grad)])
# tests correctness in debugmode # tests correctness in debugmode
f(numpy.asarray([[0, 1], [2, 3]], dtype=config.floatX)) f(np.asarray([[0, 1], [2, 3]], dtype=config.floatX))
############################################################################### ###############################################################################
...@@ -1088,9 +1088,9 @@ def test_dot_w_self(): ...@@ -1088,9 +1088,9 @@ def test_dot_w_self():
class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
def test_dot_vv(self): def test_dot_vv(self):
''' Currently we generate a gemv for that case''' ''' Currently we generate a gemv for that case'''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
w = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32')) w = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
f = theano.function([], theano.dot(v, w), mode=mode_blas_opt) f = theano.function([], theano.dot(v, w), mode=mode_blas_opt)
# Assert that the dot was optimized somehow # Assert that the dot was optimized somehow
...@@ -1098,13 +1098,13 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1098,13 +1098,13 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True)) self.assertFunctionContains1(f, Gemv(True))
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), numpy.dot(v.get_value(), w.get_value())) assert np.allclose(f(), np.dot(v.get_value(), w.get_value()))
def test_dot_vm(self): def test_dot_vm(self):
''' Test vector dot matrix ''' ''' Test vector dot matrix '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(numpy.array(rng.uniform(size=(2, 3)), m = theano.shared(np.array(rng.uniform(size=(2, 3)),
dtype='float32')) dtype='float32'))
f = theano.function([], theano.dot(v, m), mode=mode_blas_opt) f = theano.function([], theano.dot(v, m), mode=mode_blas_opt)
...@@ -1113,18 +1113,18 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1113,18 +1113,18 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True)) self.assertFunctionContains1(f, Gemv(True))
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), numpy.dot(v.get_value(), m.get_value())) assert np.allclose(f(), np.dot(v.get_value(), m.get_value()))
# Assert it works when m has no contiguous dimension # Assert it works when m has no contiguous dimension
m.set_value( m.set_value(
m.get_value(borrow=True)[::-1, ::-1], m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
assert numpy.allclose(f(), numpy.dot(v.get_value(), m.get_value())) assert np.allclose(f(), np.dot(v.get_value(), m.get_value()))
def test_dot_mv(self): def test_dot_mv(self):
''' Test matrix dot vector ''' ''' Test matrix dot vector '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32')) v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(numpy.array(rng.uniform(size=(3, 2)), m = theano.shared(np.array(rng.uniform(size=(3, 2)),
dtype='float32')) dtype='float32'))
f = theano.function([], theano.dot(m, v), mode=mode_blas_opt) f = theano.function([], theano.dot(m, v), mode=mode_blas_opt)
...@@ -1133,29 +1133,29 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1133,29 +1133,29 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True)) self.assertFunctionContains1(f, Gemv(True))
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), numpy.dot(m.get_value(), v.get_value())) assert np.allclose(f(), np.dot(m.get_value(), v.get_value()))
# Assert it works when m has no contiguous dimension # Assert it works when m has no contiguous dimension
m.set_value( m.set_value(
m.get_value(borrow=True)[::-1, ::-1], m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
assert numpy.allclose(f(), numpy.dot(m.get_value(), v.get_value())) assert np.allclose(f(), np.dot(m.get_value(), v.get_value()))
@staticmethod @staticmethod
def t_gemv1(m_shp): def t_gemv1(m_shp):
''' test vector2+dot(matrix,vector1) ''' ''' test vector2+dot(matrix,vector1) '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(m_shp[1],) v1 = theano.shared(np.array(rng.uniform(size=(m_shp[1],)
), dtype='float32')) ), dtype='float32'))
v2_orig = numpy.array(rng.uniform(size=(m_shp[0],)), dtype='float32') v2_orig = np.array(rng.uniform(size=(m_shp[0],)), dtype='float32')
v2 = theano.shared(v2_orig) v2 = theano.shared(v2_orig)
m = theano.shared(numpy.array(rng.uniform(size=m_shp), m = theano.shared(np.array(rng.uniform(size=m_shp),
dtype='float32')) dtype='float32'))
f = theano.function([], v2 + theano.dot(m, v1), mode=mode_blas_opt) f = theano.function([], v2 + theano.dot(m, v1), mode=mode_blas_opt)
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, Gemv) assert isinstance(topo[0].op, Gemv)
...@@ -1167,8 +1167,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1167,8 +1167,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = g.maker.fgraph.toposort() topo = g.maker.fgraph.toposort()
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, Gemv) assert isinstance(topo[0].op, Gemv)
...@@ -1180,11 +1180,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1180,11 +1180,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
m.get_value(borrow=True)[::-1, ::-1], m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
v2.set_value(v2_orig) v2.set_value(v2_orig)
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig) np.dot(m.get_value(), v1.get_value()) + v2_orig)
@attr('slow') @attr('slow')
def test_gemv1(self): def test_gemv1(self):
...@@ -1195,19 +1195,19 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1195,19 +1195,19 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
def test_gemv2(self): def test_gemv2(self):
''' test vector2+dot(vector1,matrix) ''' ''' test vector2+dot(vector1,matrix) '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(2,)), v1 = theano.shared(np.array(rng.uniform(size=(2,)),
dtype='float32')) dtype='float32'))
v2_orig = numpy.array(rng.uniform(size=(3,)), dtype='float32') v2_orig = np.array(rng.uniform(size=(3,)), dtype='float32')
v2 = theano.shared(v2_orig) v2 = theano.shared(v2_orig)
m = theano.shared(numpy.array(rng.uniform(size=(2, 3)), m = theano.shared(np.array(rng.uniform(size=(2, 3)),
dtype='float32')) dtype='float32'))
f = theano.function([], v2 + theano.dot(v1, m), mode=mode_blas_opt) f = theano.function([], v2 + theano.dot(v1, m), mode=mode_blas_opt)
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(v1.get_value(), m.get_value()) + v2.get_value()) np.dot(v1.get_value(), m.get_value()) + v2.get_value())
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
assert topo[-1].op.inplace == False assert topo[-1].op.inplace == False
...@@ -1218,8 +1218,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1218,8 +1218,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output # Assert they produce the same output
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(v1.get_value(), m.get_value()) + v2_orig) np.dot(v1.get_value(), m.get_value()) + v2_orig)
topo = g.maker.fgraph.toposort() topo = g.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
if config.mode != 'FAST_COMPILE': if config.mode != 'FAST_COMPILE':
...@@ -1230,38 +1230,38 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1230,38 +1230,38 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
m.get_value(borrow=True)[::-1, ::-1], m.get_value(borrow=True)[::-1, ::-1],
borrow=True) borrow=True)
v2.set_value(v2_orig) v2.set_value(v2_orig)
assert numpy.allclose(f(), assert np.allclose(f(),
numpy.dot(v1.get_value(), m.get_value()) + v2.get_value()) np.dot(v1.get_value(), m.get_value()) + v2.get_value())
g() g()
assert numpy.allclose(v2.get_value(), assert np.allclose(v2.get_value(),
numpy.dot(v1.get_value(), m.get_value()) + v2_orig) np.dot(v1.get_value(), m.get_value()) + v2_orig)
def test_gemv_broadcast(self): def test_gemv_broadcast(self):
''' test gemv with some broadcasted input ''' ''' test gemv with some broadcasted input '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(2,)), v1 = theano.shared(np.array(rng.uniform(size=(2,)),
dtype='float32')) dtype='float32'))
v2_orig = numpy.array(rng.uniform(size=(1,)), dtype='float32') v2_orig = np.array(rng.uniform(size=(1,)), dtype='float32')
v2 = theano.shared(v2_orig) v2 = theano.shared(v2_orig)
m = theano.shared(numpy.array(rng.uniform(size=(1, 2)), m = theano.shared(np.array(rng.uniform(size=(1, 2)),
dtype='float32'), dtype='float32'),
broadcastable=(True, False)) broadcastable=(True, False))
o = theano.dot(m, v1) o = theano.dot(m, v1)
f = theano.function([], o + v2, mode=mode_blas_opt) f = theano.function([], o + v2, mode=mode_blas_opt)
# Assert they produce the same output # Assert they produce the same output
assert numpy.allclose( assert np.allclose(
f(), f(),
numpy.dot(m.get_value(), v1.get_value()) + v2.get_value()) np.dot(m.get_value(), v1.get_value()) + v2.get_value())
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
# call gemv directly for mixed broadcast pattern. # call gemv directly for mixed broadcast pattern.
o = theano.tensor.blas.gemv_no_inplace(v2, 0.5, m, v1, 0.25) o = theano.tensor.blas.gemv_no_inplace(v2, 0.5, m, v1, 0.25)
f = theano.function([], o, mode=mode_blas_opt) f = theano.function([], o, mode=mode_blas_opt)
assert numpy.allclose( assert np.allclose(
f(), f(),
0.5*numpy.dot(m.get_value(), v1.get_value()) + 0.25*v2.get_value()) 0.5*np.dot(m.get_value(), v1.get_value()) + 0.25*v2.get_value())
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1 assert sum(isinstance(node.op, Gemv) for node in topo) == 1
...@@ -1277,12 +1277,12 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1277,12 +1277,12 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
f = theano.function([A, x, y], z) f = theano.function([A, x, y], z)
# Matrix value # Matrix value
A_val = numpy.ones((5, 3), dtype=config.floatX) A_val = np.ones((5, 3), dtype=config.floatX)
# Different vector length # Different vector length
ones_3 = numpy.ones(3, dtype=config.floatX) ones_3 = np.ones(3, dtype=config.floatX)
ones_4 = numpy.ones(4, dtype=config.floatX) ones_4 = np.ones(4, dtype=config.floatX)
ones_5 = numpy.ones(5, dtype=config.floatX) ones_5 = np.ones(5, dtype=config.floatX)
ones_6 = numpy.ones(6, dtype=config.floatX) ones_6 = np.ones(6, dtype=config.floatX)
f(A_val, ones_3, ones_5) f(A_val, ones_3, ones_5)
f(A_val[::-1, ::-1], ones_3, ones_5) f(A_val[::-1, ::-1], ones_3, ones_5)
...@@ -1322,7 +1322,7 @@ class BaseGemv(object): ...@@ -1322,7 +1322,7 @@ class BaseGemv(object):
shared = staticmethod(theano.shared) shared = staticmethod(theano.shared)
def get_data(self, x_stride=1, y_stride=1): def get_data(self, x_stride=1, y_stride=1):
rng = numpy.random.RandomState(unittest_tools.fetch_seed()) rng = np.random.RandomState(unittest_tools.fetch_seed())
mult = array(1, dtype=self.dtype) mult = array(1, dtype=self.dtype)
if self.dtype in [complex64, complex128]: if self.dtype in [complex64, complex128]:
mult = array(1 + 1j, dtype=self.dtype) mult = array(1 + 1j, dtype=self.dtype)
...@@ -1652,7 +1652,7 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1652,7 +1652,7 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
return theano.function(inputs, outputs, self.mode, updates=updates) return theano.function(inputs, outputs, self.mode, updates=updates)
def b(self, bval): def b(self, bval):
return T.as_tensor_variable(numpy.asarray(bval, dtype=self.dtype)) return T.as_tensor_variable(np.asarray(bval, dtype=self.dtype))
def test_b_0_triggers_ger(self): def test_b_0_triggers_ger(self):
""" test local_gemm_to_ger opt""" """ test local_gemm_to_ger opt"""
...@@ -1685,45 +1685,45 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1685,45 +1685,45 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
def test_outer(self): def test_outer(self):
f = self.function([self.x, self.y], T.outer(self.x, self.y)) f = self.function([self.x, self.y], T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger_destructive) self.assertFunctionContains(f, self.ger_destructive)
f(numpy.random.rand(5).astype(self.dtype), f(np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
def test_A_plus_outer(self): def test_A_plus_outer(self):
f = self.function([self.A, self.x, self.y], f = self.function([self.A, self.x, self.y],
self.A + T.outer(self.x, self.y)) self.A + T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger) self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(5, 4).astype(self.dtype), f(np.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1], f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
def test_A_plus_scaled_outer(self): def test_A_plus_scaled_outer(self):
f = self.function([self.A, self.x, self.y], f = self.function([self.A, self.x, self.y],
self.A + 0.1 * T.outer(self.x, self.y)) self.A + 0.1 * T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger) self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(5, 4).astype(self.dtype), f(np.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1], f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
def test_scaled_A_plus_scaled_outer(self): def test_scaled_A_plus_scaled_outer(self):
f = self.function([self.A, self.x, self.y], f = self.function([self.A, self.x, self.y],
numpy.asarray(0.2, self.dtype) * self.A + np.asarray(0.2, self.dtype) * self.A +
numpy.asarray(0.1, self.dtype) * T.outer( np.asarray(0.1, self.dtype) * T.outer(
self.x, self.y)) self.x, self.y))
# Why gemm? This make the graph simpler did we test that it # Why gemm? This make the graph simpler did we test that it
# make it faster? # make it faster?
self.assertFunctionContains(f, self.gemm) self.assertFunctionContains(f, self.gemm)
f(numpy.random.rand(5, 4).astype(self.dtype), f(np.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1], f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype), np.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype)) np.random.rand(4).astype(self.dtype))
def given_dtype(self, dtype, M, N): def given_dtype(self, dtype, M, N):
""" test corner case shape and dtype""" """ test corner case shape and dtype"""
...@@ -1731,12 +1731,12 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1731,12 +1731,12 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
f = self.function([self.A, self.x, self.y], f = self.function([self.A, self.x, self.y],
self.A + 0.1 * T.outer(self.x, self.y)) self.A + 0.1 * T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger) self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(M, N).astype(self.dtype), f(np.random.rand(M, N).astype(self.dtype),
numpy.random.rand(M).astype(self.dtype), np.random.rand(M).astype(self.dtype),
numpy.random.rand(N).astype(self.dtype)) np.random.rand(N).astype(self.dtype))
f(numpy.random.rand(M, N).astype(self.dtype)[::-1, ::-1], f(np.random.rand(M, N).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(M).astype(self.dtype), np.random.rand(M).astype(self.dtype),
numpy.random.rand(N).astype(self.dtype)) np.random.rand(N).astype(self.dtype))
def test_f32_0_0(self): def test_f32_0_0(self):
return self.given_dtype('float32', 0, 0) return self.given_dtype('float32', 0, 0)
...@@ -1769,19 +1769,19 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin): ...@@ -1769,19 +1769,19 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
return self.given_dtype('complex128', 1, 9) return self.given_dtype('complex128', 1, 9)
def test_inplace(self): def test_inplace(self):
A = self.shared(numpy.random.rand(4, 5).astype(self.dtype)) A = self.shared(np.random.rand(4, 5).astype(self.dtype))
f = self.function([self.x, self.y], [], f = self.function([self.x, self.y], [],
updates=[(A, A + T.constant(0.1, dtype=self.dtype) * updates=[(A, A + T.constant(0.1, dtype=self.dtype) *
T.outer(self.x, self.y))]) T.outer(self.x, self.y))])
self.assertFunctionContains(f, self.ger_destructive) self.assertFunctionContains(f, self.ger_destructive)
f(numpy.random.rand(4).astype(self.dtype), f(np.random.rand(4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype)) np.random.rand(5).astype(self.dtype))
A.set_value( A.set_value(
A.get_value(borrow=True, return_internal_type=True)[::-1, ::-1], A.get_value(borrow=True, return_internal_type=True)[::-1, ::-1],
borrow=True) borrow=True)
f(numpy.random.rand(4).astype(self.dtype), f(np.random.rand(4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype)) np.random.rand(5).astype(self.dtype))
class TestBlasStrides(TestCase): class TestBlasStrides(TestCase):
...@@ -1789,13 +1789,13 @@ class TestBlasStrides(TestCase): ...@@ -1789,13 +1789,13 @@ class TestBlasStrides(TestCase):
shared = staticmethod(tensor._shared) shared = staticmethod(tensor._shared)
mode = theano.compile.get_default_mode() mode = theano.compile.get_default_mode()
mode = mode.including('fast_run').excluding('gpu', 'c_blas', 'scipy_blas') mode = mode.including('fast_run').excluding('gpu', 'c_blas', 'scipy_blas')
rng = numpy.random.RandomState(seed=unittest_tools.fetch_seed()) rng = np.random.RandomState(seed=unittest_tools.fetch_seed())
def rand(self, *shape): def rand(self, *shape):
return theano._asarray(self.rng.rand(*shape), dtype=self.dtype) return theano._asarray(self.rng.rand(*shape), dtype=self.dtype)
def cmp_dot22(self, b_shp, c_shp): def cmp_dot22(self, b_shp, c_shp):
av = numpy.zeros((0, 0), dtype=self.dtype) av = np.zeros((0, 0), dtype=self.dtype)
bv = self.rand(*b_shp) bv = self.rand(*b_shp)
cv = self.rand(*c_shp) cv = self.rand(*c_shp)
...@@ -1834,20 +1834,20 @@ class TestBlasStrides(TestCase): ...@@ -1834,20 +1834,20 @@ class TestBlasStrides(TestCase):
c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True) c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True)
# Numpy result # Numpy result
a_n = numpy.dot(bv[::b_step1, ::b_step2], a_n = np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]) cv[::c_step1, ::c_step2])
f_nn() f_nn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_nt() f_nt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_tn() f_tn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_tt() f_tt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
def test_dot22(self): def test_dot22(self):
self.cmp_dot22((3, 4), (4, 5)) self.cmp_dot22((3, 4), (4, 5))
...@@ -1865,10 +1865,10 @@ class TestBlasStrides(TestCase): ...@@ -1865,10 +1865,10 @@ class TestBlasStrides(TestCase):
self.cmp_dot22((0, 0), (0, 0)) self.cmp_dot22((0, 0), (0, 0))
def cmp_dot22scalar(self, b_shp, c_shp): def cmp_dot22scalar(self, b_shp, c_shp):
av = numpy.zeros((0, 0), dtype=self.dtype) av = np.zeros((0, 0), dtype=self.dtype)
bv = self.rand(*b_shp) bv = self.rand(*b_shp)
cv = self.rand(*c_shp) cv = self.rand(*c_shp)
l = numpy.float32(0.2) l = np.float32(0.2)
a = self.shared(av, 'a') a = self.shared(av, 'a')
b = self.shared(bv, 'b') b = self.shared(bv, 'b')
...@@ -1904,20 +1904,20 @@ class TestBlasStrides(TestCase): ...@@ -1904,20 +1904,20 @@ class TestBlasStrides(TestCase):
c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True) c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True)
# Numpy result # Numpy result
a_n = l * numpy.dot(bv[::b_step1, ::b_step2], a_n = l * np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]) cv[::c_step1, ::c_step2])
f_nn() f_nn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_nt() f_nt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_tn() f_tn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
f_tt() f_tt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
def test_dot22scalar(self): def test_dot22scalar(self):
self.cmp_dot22scalar((3, 4), (4, 5)) self.cmp_dot22scalar((3, 4), (4, 5))
...@@ -1938,7 +1938,7 @@ class TestBlasStrides(TestCase): ...@@ -1938,7 +1938,7 @@ class TestBlasStrides(TestCase):
av = self.rand(*a_shp) av = self.rand(*a_shp)
bv = self.rand(*b_shp) bv = self.rand(*b_shp)
cv = self.rand(*c_shp) cv = self.rand(*c_shp)
l = numpy.float32(0.2) l = np.float32(0.2)
a = self.shared(av, 'a') a = self.shared(av, 'a')
b = self.shared(bv, 'b') b = self.shared(bv, 'b')
...@@ -1992,48 +1992,48 @@ class TestBlasStrides(TestCase): ...@@ -1992,48 +1992,48 @@ class TestBlasStrides(TestCase):
# Numpy results # Numpy results
a_n = (l * av[::a_step1, ::a_step2] a_n = (l * av[::a_step1, ::a_step2]
+ numpy.dot(bv[::b_step1, ::b_step2], + np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2])) cv[::c_step1, ::c_step2]))
at_n = (l * av[::a_step1, ::a_step2].T at_n = (l * av[::a_step1, ::a_step2].T
+ numpy.dot(bv[::b_step1, ::b_step2], + np.dot(bv[::b_step1, ::b_step2],
cv[::c_step1, ::c_step2]).T) cv[::c_step1, ::c_step2]).T)
# a's value is updated, so we need to reinitialize it each time # a's value is updated, so we need to reinitialize it each time
a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True) a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True)
f_nnn() f_nnn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True) a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True)
f_nnt() f_nnt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True) a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True)
f_ntn() f_ntn()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True) a.set_value(a_dev.copy()[::a_step1, ::a_step2], borrow=True)
f_ntt() f_ntt()
assert numpy.allclose(a.get_value(), a_n) assert np.allclose(a.get_value(), a_n)
a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1], a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1],
borrow=True) borrow=True)
f_tnn() f_tnn()
assert numpy.allclose(a_t.get_value(), at_n) assert np.allclose(a_t.get_value(), at_n)
a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1], a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1],
borrow=True) borrow=True)
f_tnt() f_tnt()
assert numpy.allclose(a_t.get_value(), at_n) assert np.allclose(a_t.get_value(), at_n)
a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1], a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1],
borrow=True) borrow=True)
f_ttn() f_ttn()
assert numpy.allclose(a_t.get_value(), at_n) assert np.allclose(a_t.get_value(), at_n)
a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1], a_t.set_value(transpose(a_dev.copy())[::a_step2, ::a_step1],
borrow=True) borrow=True)
f_ttt() f_ttt()
assert numpy.allclose(a_t.get_value(), at_n) assert np.allclose(a_t.get_value(), at_n)
def test_gemm(self): def test_gemm(self):
self.cmp_gemm((3, 5), (3, 4), (4, 5)) self.cmp_gemm((3, 5), (3, 4), (4, 5))
...@@ -2054,7 +2054,7 @@ class TestBlasStrides(TestCase): ...@@ -2054,7 +2054,7 @@ class TestBlasStrides(TestCase):
av = self.rand(a_shp) av = self.rand(a_shp)
bv = self.rand(*b_shp) bv = self.rand(*b_shp)
cv = self.rand(c_shp) cv = self.rand(c_shp)
l = numpy.float32(0.2) l = np.float32(0.2)
a = self.shared(av, 'a') a = self.shared(av, 'a')
b = self.shared(bv, 'b') b = self.shared(bv, 'b')
...@@ -2086,14 +2086,14 @@ class TestBlasStrides(TestCase): ...@@ -2086,14 +2086,14 @@ class TestBlasStrides(TestCase):
c.set_value(c_dev.copy()[::c_step], borrow=True) c.set_value(c_dev.copy()[::c_step], borrow=True)
a_n = (av[::a_step] a_n = (av[::a_step]
+ l * numpy.dot(bv[::b_step1, ::b_step2], + l * np.dot(bv[::b_step1, ::b_step2],
cv[::c_step])) cv[::c_step]))
f_n() f_n()
assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n) assert np.allclose(a.get_value(), a_n), (a.get_value(), a_n)
a.set_value(a_dev.copy()[::a_step], borrow=True) a.set_value(a_dev.copy()[::a_step], borrow=True)
f_t() f_t()
assert numpy.allclose(a.get_value(), a_n), (a.get_value(), a_n) assert np.allclose(a.get_value(), a_n), (a.get_value(), a_n)
def test_gemv(self): def test_gemv(self):
self.cmp_gemv(3, (3, 5), 5) self.cmp_gemv(3, (3, 5), 5)
...@@ -2109,7 +2109,7 @@ class TestBlasStrides(TestCase): ...@@ -2109,7 +2109,7 @@ class TestBlasStrides(TestCase):
av = self.rand(*a_shp) av = self.rand(*a_shp)
bv = self.rand(b_shp) bv = self.rand(b_shp)
cv = self.rand(c_shp) cv = self.rand(c_shp)
l = numpy.float32(0.2) l = np.float32(0.2)
a = self.shared(av, 'a') a = self.shared(av, 'a')
b = self.shared(bv, 'b') b = self.shared(bv, 'b')
...@@ -2142,13 +2142,13 @@ class TestBlasStrides(TestCase): ...@@ -2142,13 +2142,13 @@ class TestBlasStrides(TestCase):
f_n() f_n()
n_n = (av[::a_step1, ::a_step2] n_n = (av[::a_step1, ::a_step2]
+ l * numpy.outer(bv[::b_step], cv[::c_step])) + l * np.outer(bv[::b_step], cv[::c_step]))
assert numpy.allclose(a.get_value(), n_n), (a.get_value(), n_n) assert np.allclose(a.get_value(), n_n), (a.get_value(), n_n)
f_t() f_t()
n_t = (av.T[::a_step1, ::a_step2] n_t = (av.T[::a_step1, ::a_step2]
+ l * numpy.outer(bv[::b_step], cv[::c_step]).T) + l * np.outer(bv[::b_step], cv[::c_step]).T)
assert numpy.allclose(a_t.get_value(), n_t),\ assert np.allclose(a_t.get_value(), n_t),\
(a_t.get_value(), n_t) (a_t.get_value(), n_t)
def test_ger_strides(self): def test_ger_strides(self):
...@@ -2163,9 +2163,9 @@ class TestBlasStrides(TestCase): ...@@ -2163,9 +2163,9 @@ class TestBlasStrides(TestCase):
def test_gemm_non_contiguous(self): def test_gemm_non_contiguous(self):
"""test_gemm_non_contiguous: Test if GEMM works well with non-contiguous matrices.""" """test_gemm_non_contiguous: Test if GEMM works well with non-contiguous matrices."""
aval = numpy.ones((6, 2)) aval = np.ones((6, 2))
bval = numpy.ones((2, 7)) bval = np.ones((2, 7))
cval = numpy.arange(7) + numpy.arange(0, .6, .1)[:, numpy.newaxis] cval = np.arange(7) + np.arange(0, .6, .1)[:, np.newaxis]
a = theano.shared(aval[:3], borrow=True) a = theano.shared(aval[:3], borrow=True)
b = theano.shared(bval[:, :5], borrow=True) b = theano.shared(bval[:, :5], borrow=True)
...@@ -2176,7 +2176,7 @@ class TestBlasStrides(TestCase): ...@@ -2176,7 +2176,7 @@ class TestBlasStrides(TestCase):
f = theano.function([s], [], updates={c: upd_c}) f = theano.function([s], [], updates={c: upd_c})
f(0) f(0)
ref_output = numpy.ones((3, 5)) * 2 ref_output = np.ones((3, 5)) * 2
unittest_tools.assert_allclose(c.get_value(), ref_output) unittest_tools.assert_allclose(c.get_value(), ref_output)
...@@ -2185,8 +2185,8 @@ class test_infer_shape(unittest_tools.InferShapeTester): ...@@ -2185,8 +2185,8 @@ class test_infer_shape(unittest_tools.InferShapeTester):
x, y = T.matrices('xy') x, y = T.matrices('xy')
self._compile_and_check( self._compile_and_check(
[x, y], [T.blas._dot22(x, y)], [x, y], [T.blas._dot22(x, y)],
[numpy.random.random((2, 3)).astype(config.floatX), [np.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX)], np.random.random((3, 4)).astype(config.floatX)],
T.blas.Dot22) T.blas.Dot22)
def test_dot22scalar(self): def test_dot22scalar(self):
...@@ -2194,9 +2194,9 @@ class test_infer_shape(unittest_tools.InferShapeTester): ...@@ -2194,9 +2194,9 @@ class test_infer_shape(unittest_tools.InferShapeTester):
a = T.scalar('a') a = T.scalar('a')
self._compile_and_check( self._compile_and_check(
[x, y, a], [T.blas._dot22scalar(x, y, a)], [x, y, a], [T.blas._dot22scalar(x, y, a)],
[numpy.random.random((2, 3)).astype(config.floatX), [np.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX), np.random.random((3, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)], np.asarray(0.5, dtype=config.floatX)],
T.blas.Dot22Scalar) T.blas.Dot22Scalar)
def test_gemm(self): def test_gemm(self):
...@@ -2205,11 +2205,11 @@ class test_infer_shape(unittest_tools.InferShapeTester): ...@@ -2205,11 +2205,11 @@ class test_infer_shape(unittest_tools.InferShapeTester):
b = T.scalar('b') b = T.scalar('b')
self._compile_and_check( self._compile_and_check(
[x, y, a, z, b], [T.blas.gemm(z, a, x, y, b)], [x, y, a, z, b], [T.blas.gemm(z, a, x, y, b)],
[numpy.random.random((2, 3)).astype(config.floatX), [np.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX), np.random.random((3, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX), np.asarray(0.5, dtype=config.floatX),
numpy.random.random((2, 4)).astype(config.floatX), np.random.random((2, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)], np.asarray(0.5, dtype=config.floatX)],
T.blas.Gemm) T.blas.Gemm)
def test_gemv(self): def test_gemv(self):
...@@ -2219,11 +2219,11 @@ class test_infer_shape(unittest_tools.InferShapeTester): ...@@ -2219,11 +2219,11 @@ class test_infer_shape(unittest_tools.InferShapeTester):
b = T.scalar('b') b = T.scalar('b')
self._compile_and_check( self._compile_and_check(
[y, a, A, x, b], [T.blas.gemv(y, a, A, x, b)], [y, a, A, x, b], [T.blas.gemv(y, a, A, x, b)],
[numpy.random.random((2,)).astype(config.floatX), [np.random.random((2,)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX), np.asarray(0.5, dtype=config.floatX),
numpy.random.random((2, 3)).astype(config.floatX), np.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3,)).astype(config.floatX), np.random.random((3,)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)], np.asarray(0.5, dtype=config.floatX)],
T.blas.Gemv) T.blas.Gemv)
def test_ger(self): def test_ger(self):
...@@ -2232,8 +2232,8 @@ class test_infer_shape(unittest_tools.InferShapeTester): ...@@ -2232,8 +2232,8 @@ class test_infer_shape(unittest_tools.InferShapeTester):
a = T.scalar('a') a = T.scalar('a')
self._compile_and_check( self._compile_and_check(
[A, a, x, y], [T.blas.ger(A, a, x, y)], [A, a, x, y], [T.blas.ger(A, a, x, y)],
[numpy.random.random((2, 3)).astype(config.floatX), [np.random.random((2, 3)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX), np.asarray(0.5, dtype=config.floatX),
numpy.random.random((2,)).astype(config.floatX), np.random.random((2,)).astype(config.floatX),
numpy.random.random((3,)).astype(config.floatX)], np.random.random((3,)).astype(config.floatX)],
T.blas.Ger) T.blas.Ger)
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import sys import sys
import numpy import numpy as np
import theano import theano
import theano.tensor as tensor import theano.tensor as tensor
from theano.tensor.blas_scipy import ScipyGer from theano.tensor.blas_scipy import ScipyGer
...@@ -20,9 +20,9 @@ class TestScipyGer(TestCase, TestOptimizationMixin): ...@@ -20,9 +20,9 @@ class TestScipyGer(TestCase, TestOptimizationMixin):
self.a = tensor.tensor(dtype=dtype, broadcastable=()) self.a = tensor.tensor(dtype=dtype, broadcastable=())
self.x = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.x = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.y = tensor.tensor(dtype=dtype, broadcastable=(False,)) self.y = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.Aval = numpy.ones((2, 3), dtype=dtype) self.Aval = np.ones((2, 3), dtype=dtype)
self.xval = numpy.asarray([1, 2], dtype=dtype) self.xval = np.asarray([1, 2], dtype=dtype)
self.yval = numpy.asarray([1.5, 2.7, 3.9], dtype=dtype) self.yval = np.asarray([1.5, 2.7, 3.9], dtype=dtype)
if not theano.tensor.blas_scipy.have_fblas: if not theano.tensor.blas_scipy.have_fblas:
self.SkipTest() self.SkipTest()
...@@ -34,7 +34,7 @@ class TestScipyGer(TestCase, TestOptimizationMixin): ...@@ -34,7 +34,7 @@ class TestScipyGer(TestCase, TestOptimizationMixin):
f(self.Aval[::-1, ::-1], self.xval[::-1], self.yval[::-1]) f(self.Aval[::-1, ::-1], self.xval[::-1], self.yval[::-1])
def b(self, bval): def b(self, bval):
return tensor.as_tensor_variable(numpy.asarray(bval, dtype=self.dtype)) return tensor.as_tensor_variable(np.asarray(bval, dtype=self.dtype))
def test_outer(self): def test_outer(self):
f = self.function([self.x, self.y], tensor.outer(self.x, self.y)) f = self.function([self.x, self.y], tensor.outer(self.x, self.y))
......
...@@ -2,6 +2,7 @@ from __future__ import absolute_import, print_function, division ...@@ -2,6 +2,7 @@ from __future__ import absolute_import, print_function, division
import unittest import unittest
from six.moves import xrange from six.moves import xrange
import theano import theano
import numpy as np
from theano.tensor import * from theano.tensor import *
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -12,30 +13,30 @@ class TestRealImag(unittest.TestCase): ...@@ -12,30 +13,30 @@ class TestRealImag(unittest.TestCase):
def test0(self): def test0(self):
x = zvector() x = zvector()
rng = numpy.random.RandomState(23) rng = np.random.RandomState(23)
xval = numpy.asarray(list(numpy.complex(rng.randn(), rng.randn()) xval = np.asarray(list(np.complex(rng.randn(), rng.randn())
for i in xrange(10))) for i in xrange(10)))
assert numpy.all(xval.real == theano.function([x], real(x))(xval)) assert np.all(xval.real == theano.function([x], real(x))(xval))
assert numpy.all(xval.imag == theano.function([x], imag(x))(xval)) assert np.all(xval.imag == theano.function([x], imag(x))(xval))
def test_on_real_input(self): def test_on_real_input(self):
x = dvector() x = dvector()
rng = numpy.random.RandomState(23) rng = np.random.RandomState(23)
xval = rng.randn(10) xval = rng.randn(10)
numpy.all(0 == theano.function([x], imag(x))(xval)) np.all(0 == theano.function([x], imag(x))(xval))
numpy.all(xval == theano.function([x], real(x))(xval)) np.all(xval == theano.function([x], real(x))(xval))
x = imatrix() x = imatrix()
xval = numpy.asarray(rng.randn(3, 3) * 100, dtype='int32') xval = np.asarray(rng.randn(3, 3) * 100, dtype='int32')
numpy.all(0 == theano.function([x], imag(x))(xval)) np.all(0 == theano.function([x], imag(x))(xval))
numpy.all(xval == theano.function([x], real(x))(xval)) np.all(xval == theano.function([x], real(x))(xval))
def test_cast(self): def test_cast(self):
x = zvector() x = zvector()
self.assertRaises(TypeError, cast, x, 'int32') self.assertRaises(TypeError, cast, x, 'int32')
def test_complex(self): def test_complex(self):
rng = numpy.random.RandomState(2333) rng = np.random.RandomState(2333)
m = fmatrix() m = fmatrix()
c = complex(m[0], m[1]) c = complex(m[0], m[1])
assert c.type == cvector assert c.type == cvector
...@@ -44,10 +45,10 @@ class TestRealImag(unittest.TestCase): ...@@ -44,10 +45,10 @@ class TestRealImag(unittest.TestCase):
assert i.type == fvector assert i.type == fvector
f = theano.function([m], [r, i]) f = theano.function([m], [r, i])
mval = numpy.asarray(rng.randn(2, 5), dtype='float32') mval = np.asarray(rng.randn(2, 5), dtype='float32')
rval, ival = f(mval) rval, ival = f(mval)
assert numpy.all(rval == mval[0]), (rval, mval[0]) assert np.all(rval == mval[0]), (rval, mval[0])
assert numpy.all(ival == mval[1]), (ival, mval[1]) assert np.all(ival == mval[1]), (ival, mval[1])
@dec.skipif(True, "Complex grads not enabled, see #178") @dec.skipif(True, "Complex grads not enabled, see #178")
def test_complex_grads(self): def test_complex_grads(self):
...@@ -55,8 +56,8 @@ class TestRealImag(unittest.TestCase): ...@@ -55,8 +56,8 @@ class TestRealImag(unittest.TestCase):
c = complex(m[0], m[1]) c = complex(m[0], m[1])
return .5 * real(c) + .9 * imag(c) return .5 * real(c) + .9 * imag(c)
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5)) mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval]) utt.verify_grad(f, [mval])
@dec.skipif(True, "Complex grads not enabled, see #178") @dec.skipif(True, "Complex grads not enabled, see #178")
...@@ -66,8 +67,8 @@ class TestRealImag(unittest.TestCase): ...@@ -66,8 +67,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1]) ac = complex(a[0], a[1])
return abs((ac)**2).sum() return abs((ac)**2).sum()
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5)) aval = np.asarray(rng.randn(2, 5))
try: try:
utt.verify_grad(f, [aval]) utt.verify_grad(f, [aval])
except utt.verify_grad.E_grad as e: except utt.verify_grad.E_grad as e:
...@@ -82,8 +83,8 @@ class TestRealImag(unittest.TestCase): ...@@ -82,8 +83,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1]) ac = complex(a[0], a[1])
return abs(ac).sum() return abs(ac).sum()
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5)) aval = np.asarray(rng.randn(2, 5))
try: try:
utt.verify_grad(f, [aval]) utt.verify_grad(f, [aval])
except utt.verify_grad.E_grad as e: except utt.verify_grad.E_grad as e:
...@@ -98,8 +99,8 @@ class TestRealImag(unittest.TestCase): ...@@ -98,8 +99,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1]) ac = complex(a[0], a[1])
return abs((ac*b)**2).sum() return abs((ac*b)**2).sum()
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5)) aval = np.asarray(rng.randn(2, 5))
bval = rng.randn(5) bval = rng.randn(5)
try: try:
utt.verify_grad(f, [aval, bval]) utt.verify_grad(f, [aval, bval])
...@@ -114,8 +115,8 @@ class TestRealImag(unittest.TestCase): ...@@ -114,8 +115,8 @@ class TestRealImag(unittest.TestCase):
c = complex_from_polar(abs(m[0]), m[1]) c = complex_from_polar(abs(m[0]), m[1])
return .5 * real(c) + .9 * imag(c) return .5 * real(c) + .9 * imag(c)
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5)) mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval]) utt.verify_grad(f, [mval])
@dec.skipif(True, "Complex grads not enabled, see #178") @dec.skipif(True, "Complex grads not enabled, see #178")
...@@ -124,6 +125,6 @@ class TestRealImag(unittest.TestCase): ...@@ -124,6 +125,6 @@ class TestRealImag(unittest.TestCase):
c = complex(m[0], m[1]) c = complex(m[0], m[1])
return .5 * abs(c) return .5 * abs(c)
rng = numpy.random.RandomState(9333) rng = np.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5)) mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval]) utt.verify_grad(f, [mval])
...@@ -3,7 +3,7 @@ from copy import copy ...@@ -3,7 +3,7 @@ from copy import copy
import unittest import unittest
import math import math
import numpy import numpy as np
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.tools import raises from nose.tools import raises
from six.moves import xrange from six.moves import xrange
...@@ -47,13 +47,13 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -47,13 +47,13 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
x = self.type(self.dtype, ib)('x') x = self.type(self.dtype, ib)('x')
e = self.op(ib, shuffle)(x) e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x], [e])).make_function() f = copy(linker).accept(FunctionGraph([x], [e])).make_function()
assert f(numpy.ones(xsh, dtype=self.dtype)).shape == zsh assert f(np.ones(xsh, dtype=self.dtype)).shape == zsh
# test that DimShuffle.infer_shape work correctly # test that DimShuffle.infer_shape work correctly
x = self.type(self.dtype, ib)('x') x = self.type(self.dtype, ib)('x')
e = self.op(ib, shuffle)(x) e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x], f = copy(linker).accept(FunctionGraph([x],
[e.shape])).make_function() [e.shape])).make_function()
assert all(f(numpy.ones(xsh, dtype=self.dtype))) == all(zsh) assert all(f(np.ones(xsh, dtype=self.dtype))) == all(zsh)
# Test when we drop a axis that is not broadcastable # Test when we drop a axis that is not broadcastable
ib = [False, True, False] ib = [False, True, False]
...@@ -65,7 +65,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -65,7 +65,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
x = self.type(self.dtype, ib)('x') x = self.type(self.dtype, ib)('x')
e = self.op(ib, (1, 2))(x) e = self.op(ib, (1, 2))(x)
f = copy(linker).accept(FunctionGraph([x], [e.shape])).make_function() f = copy(linker).accept(FunctionGraph([x], [e.shape])).make_function()
self.assertRaises(TypeError, f, numpy.ones((2, 1, 4))) self.assertRaises(TypeError, f, np.ones((2, 1, 4)))
# Test that we can't take a dimensions multiple time # Test that we can't take a dimensions multiple time
xsh, shuffle, zsh = ((1, 1, 4), (0, 1, 2, 0), (1, 4)) xsh, shuffle, zsh = ((1, 1, 4), (0, 1, 2, 0), (1, 4))
...@@ -94,7 +94,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -94,7 +94,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
((1,), ('x', 'x'))]: ((1,), ('x', 'x'))]:
ib = [(entry == 1) for entry in xsh] ib = [(entry == 1) for entry in xsh]
adtens = self.type(self.dtype, ib)('x') adtens = self.type(self.dtype, ib)('x')
adtens_val = numpy.ones(xsh, dtype=self.dtype) adtens_val = np.ones(xsh, dtype=self.dtype)
self._compile_and_check([adtens], self._compile_and_check([adtens],
[self.op(ib, shuffle)(adtens)], [self.op(ib, shuffle)(adtens)],
[adtens_val], self.op, [adtens_val], self.op,
...@@ -102,50 +102,50 @@ class test_DimShuffle(unittest_tools.InferShapeTester): ...@@ -102,50 +102,50 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
def test_too_big_rank(self): def test_too_big_rank(self):
x = self.type(self.dtype, broadcastable=())() x = self.type(self.dtype, broadcastable=())()
y = x.dimshuffle(('x',) * (numpy.MAXDIMS + 1)) y = x.dimshuffle(('x',) * (np.MAXDIMS + 1))
self.assertRaises(ValueError, y.eval, {x: 0}) self.assertRaises(ValueError, y.eval, {x: 0})
class test_reduce_axes(unittest.TestCase): class test_reduce_axes(unittest.TestCase):
def test_sum_axes(self): def test_sum_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.sum(a) x.sum(a)
def test_mean_axes(self): def test_mean_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.mean(a) x.mean(a)
def test_max_axes(self): def test_max_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.max(a) x.max(a)
def test_min_axes(self): def test_min_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.min(a) x.min(a)
def test_argmax_axes(self): def test_argmax_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.argmax(a) x.argmax(a)
def test_var_axes(self): def test_var_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1), axes = [None, 0, 1, [0, 1], np.array(1),
[numpy.array(0), numpy.array(1)]] [np.array(0), np.array(1)]]
for a in axes: for a in axes:
x = tensor.matrix() x = tensor.matrix()
x.var(a) x.var(a)
...@@ -166,12 +166,12 @@ class test_Broadcast(unittest.TestCase): ...@@ -166,12 +166,12 @@ class test_Broadcast(unittest.TestCase):
linkers = [gof.PerformLinker, gof.CLinker] linkers = [gof.PerformLinker, gof.CLinker]
def rand_val(self, shp): def rand_val(self, shp):
return numpy.asarray(numpy.random.rand(*shp), return np.asarray(np.random.rand(*shp),
dtype=theano.config.floatX) dtype=theano.config.floatX)
def rand_cval(self, shp): def rand_cval(self, shp):
return numpy.asarray(numpy.random.rand(*shp), return np.asarray(np.random.rand(*shp),
dtype=theano.config.floatX) dtype=theano.config.floatX)
def setUp(self): def setUp(self):
unittest_tools.seed_rng() unittest_tools.seed_rng()
...@@ -331,19 +331,19 @@ class test_Broadcast(unittest.TestCase): ...@@ -331,19 +331,19 @@ class test_Broadcast(unittest.TestCase):
def reduce_bitwise_and(x, axis=-1, dtype='int8'): def reduce_bitwise_and(x, axis=-1, dtype='int8'):
identity = numpy.array((-1,), dtype=dtype)[0] identity = np.array((-1,), dtype=dtype)[0]
shape_without_axis = tuple([s for i, s in enumerate(x.shape) if i != axis]) shape_without_axis = tuple([s for i, s in enumerate(x.shape) if i != axis])
if 0 in shape_without_axis: if 0 in shape_without_axis:
return numpy.empty(shape=shape_without_axis, dtype=x.dtype) return np.empty(shape=shape_without_axis, dtype=x.dtype)
def custom_reduce(a): def custom_reduce(a):
out = identity out = identity
for i in range(a.size): for i in range(a.size):
out = numpy.bitwise_and(a[i], out) out = np.bitwise_and(a[i], out)
return out return out
return numpy.apply_along_axis(custom_reduce, axis, x) return np.apply_along_axis(custom_reduce, axis, x)
class test_CAReduce(unittest_tools.InferShapeTester): class test_CAReduce(unittest_tools.InferShapeTester):
...@@ -384,20 +384,20 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -384,20 +384,20 @@ class test_CAReduce(unittest_tools.InferShapeTester):
tosum = list(range(len(xsh))) tosum = list(range(len(xsh)))
f = copy(linker).accept(FunctionGraph([x], [e])).make_function() f = copy(linker).accept(FunctionGraph([x], [e])).make_function()
xv = numpy.asarray(numpy.random.rand(*xsh)) xv = np.asarray(np.random.rand(*xsh))
if dtype not in tensor.discrete_dtypes: if dtype not in tensor.discrete_dtypes:
xv = numpy.asarray(xv, dtype=dtype) xv = np.asarray(xv, dtype=dtype)
else: else:
xv = numpy.asarray(xv < 0.5, dtype=dtype) xv = np.asarray(xv < 0.5, dtype=dtype)
if test_nan and xv.size > 0: if test_nan and xv.size > 0:
if len(xsh) > 0: if len(xsh) > 0:
xv = xv.flatten() xv = xv.flatten()
xv[0] = numpy.nan xv[0] = np.nan
xv = xv.reshape(*xsh) xv = xv.reshape(*xsh)
else: else:
xv = numpy.asarray(numpy.nan, dtype=dtype) xv = np.asarray(np.nan, dtype=dtype)
zv = xv zv = xv
if pre_scalar_op is not None: if pre_scalar_op is not None:
zv = Elemwise(scalar_op=pre_scalar_op)(x).eval({x: xv}) zv = Elemwise(scalar_op=pre_scalar_op)(x).eval({x: xv})
...@@ -415,48 +415,48 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -415,48 +415,48 @@ class test_CAReduce(unittest_tools.InferShapeTester):
tosum = tuple(axis2) tosum = tuple(axis2)
if tensor_op == tensor.all: if tensor_op == tensor.all:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.all(zv, axis) zv = np.all(zv, axis)
if len(tosum) == 0: if len(tosum) == 0:
zv = zv != 0 zv = zv != 0
elif tensor_op == tensor.any: elif tensor_op == tensor.any:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.any(zv, axis) zv = np.any(zv, axis)
if len(tosum) == 0: if len(tosum) == 0:
zv = zv != 0 zv = zv != 0
elif scalar_op == scalar.add: elif scalar_op == scalar.add:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.add.reduce(zv, axis) zv = np.add.reduce(zv, axis)
if dtype == 'bool': if dtype == 'bool':
# numpy.add of a bool upcast, while CAReduce don't # np.add of a bool upcast, while CAReduce don't
zv = zv.astype(dtype) zv = zv.astype(dtype)
elif scalar_op == scalar.mul: elif scalar_op == scalar.mul:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.multiply.reduce(zv, axis) zv = np.multiply.reduce(zv, axis)
elif scalar_op == scalar.maximum: elif scalar_op == scalar.maximum:
try: try:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.maximum.reduce(zv, axis) zv = np.maximum.reduce(zv, axis)
except ValueError: except ValueError:
numpy_raised = True numpy_raised = True
elif scalar_op == scalar.minimum: elif scalar_op == scalar.minimum:
try: try:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.minimum.reduce(zv, axis) zv = np.minimum.reduce(zv, axis)
except ValueError: except ValueError:
numpy_raised = True numpy_raised = True
elif scalar_op == scalar.or_: elif scalar_op == scalar.or_:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.bitwise_or.reduce(zv, axis) zv = np.bitwise_or.reduce(zv, axis)
elif scalar_op == scalar.and_: elif scalar_op == scalar.and_:
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = reduce_bitwise_and(zv, axis, dtype=dtype) zv = reduce_bitwise_and(zv, axis, dtype=dtype)
elif scalar_op == scalar.xor: elif scalar_op == scalar.xor:
# There is no identity value for the xor function # There is no identity value for the xor function
# So we can't support shape of dimensions 0. # So we can't support shape of dimensions 0.
if numpy.prod(zv.shape) == 0: if np.prod(zv.shape) == 0:
continue continue
for axis in reversed(sorted(tosum)): for axis in reversed(sorted(tosum)):
zv = numpy.bitwise_xor.reduce(zv, axis) zv = np.bitwise_xor.reduce(zv, axis)
else: else:
raise Exception( raise Exception(
"Test for CAReduce with scalar_op %s not implemented" % "Test for CAReduce with scalar_op %s not implemented" %
...@@ -482,7 +482,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -482,7 +482,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
try: try:
f_xv = f(xv) f_xv = f(xv)
self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv)) self.assertTrue((f_xv.shape == zv.shape), (f_xv, zv))
self.assertTrue(numpy.allclose(f_xv, zv), self.assertTrue(np.allclose(f_xv, zv),
(f_xv, zv, xsh, tosum)) (f_xv, zv, xsh, tosum))
except NotImplementedError: except NotImplementedError:
# GpuCAReduce don't implement all cases when size is 0 # GpuCAReduce don't implement all cases when size is 0
...@@ -498,7 +498,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -498,7 +498,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
f = copy(linker).accept(FunctionGraph([x], f = copy(linker).accept(FunctionGraph([x],
[e.shape])).make_function() [e.shape])).make_function()
if not(scalar_op in [scalar.maximum, scalar.minimum] and if not(scalar_op in [scalar.maximum, scalar.minimum] and
((xsh == () or numpy.prod(xsh) == 0))): ((xsh == () or np.prod(xsh) == 0))):
try: try:
assert all(f(xv) == zv.shape) assert all(f(xv) == zv.shape)
except NotImplementedError: except NotImplementedError:
...@@ -579,7 +579,7 @@ class test_CAReduce(unittest_tools.InferShapeTester): ...@@ -579,7 +579,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
x = pre_scalar_op(x) x = pre_scalar_op(x)
if tosum is None: if tosum is None:
tosum = list(range(len(xsh))) tosum = list(range(len(xsh)))
xv = numpy.asarray(numpy.random.rand(*xsh), dtype=dtype) xv = np.asarray(np.random.rand(*xsh), dtype=dtype)
d = {} d = {}
if pre_scalar_op is not None: if pre_scalar_op is not None:
xv = x.eval({x.owner.inputs[0]: xv}) xv = x.eval({x.owner.inputs[0]: xv})
...@@ -608,8 +608,8 @@ class test_Prod(unittest.TestCase): ...@@ -608,8 +608,8 @@ class test_Prod(unittest.TestCase):
# including zeros, as the case with zeros is important # including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row) # (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val = numpy.asarray([[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]], x_val = np.asarray([[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]],
dtype='float32') dtype='float32')
# now with verify_grad # now with verify_grad
unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode) unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode)
...@@ -623,8 +623,8 @@ class test_Prod(unittest.TestCase): ...@@ -623,8 +623,8 @@ class test_Prod(unittest.TestCase):
def test_verify_grad_with_zeros(self): def test_verify_grad_with_zeros(self):
# including zeros, as the case with zeros is important # including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row) # (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val = numpy.asarray([[1., 2., 3.], [0., 5., 6.], [0., 0., 9.]], x_val = np.asarray([[1., 2., 3.], [0., 5., 6.], [0., 0., 9.]],
dtype='float32') dtype='float32')
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
# sanity check # sanity check
...@@ -635,7 +635,7 @@ class test_Prod(unittest.TestCase): ...@@ -635,7 +635,7 @@ class test_Prod(unittest.TestCase):
# p2 = Prod(axis=1)(x2) # p2 = Prod(axis=1)(x2)
# fn = theano.function([x, x2], [p - p2], mode=self.mode) # fn = theano.function([x, x2], [p - p2], mode=self.mode)
# print("hand computed diff for each row") # print("hand computed diff for each row")
# x2_val = numpy.asarray([[1., 2., 3.003], [0.003, 5., 6], [ # x2_val = np.asarray([[1., 2., 3.003], [0.003, 5., 6], [
# 0., 0., 9.01]]) # 0., 0., 9.01]])
# print(fn(x_val, x2_val)) # print(fn(x_val, x2_val))
# fn2 = theano.function([x], [theano.tensor.grad(p.sum(), x)], # fn2 = theano.function([x], [theano.tensor.grad(p.sum(), x)],
...@@ -643,7 +643,7 @@ class test_Prod(unittest.TestCase): ...@@ -643,7 +643,7 @@ class test_Prod(unittest.TestCase):
# print("real grad") # print("real grad")
# print(fn2(x_val)) # print(fn2(x_val))
fn3 = theano.function([x], [p], mode=self.mode) fn3 = theano.function([x], [p], mode=self.mode)
assert numpy.allclose(fn3(x_val), [6., 0., 0.]) assert np.allclose(fn3(x_val), [6., 0., 0.])
# now with verify_grad # now with verify_grad
unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode) unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode)
...@@ -665,25 +665,25 @@ class test_Prod(unittest.TestCase): ...@@ -665,25 +665,25 @@ class test_Prod(unittest.TestCase):
@attr('slow') @attr('slow')
def test_prod_no_zeros_in_input(self): def test_prod_no_zeros_in_input(self):
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
x_val = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32') x_val = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32')
pwz = Prod(axis=1, no_zeros_in_input=True)(x) pwz = Prod(axis=1, no_zeros_in_input=True)(x)
fn = theano.function([x], pwz, mode=self.mode) fn = theano.function([x], pwz, mode=self.mode)
assert numpy.allclose(fn(x_val), [6, 120, 504]) assert np.allclose(fn(x_val), [6, 120, 504])
pwz = Prod(no_zeros_in_input=True)(x) pwz = Prod(no_zeros_in_input=True)(x)
g = theano.grad(pwz, x) g = theano.grad(pwz, x)
gg = theano.grad(g.sum(), x) gg = theano.grad(g.sum(), x)
fn = theano.function([x], g, mode=self.mode) fn = theano.function([x], g, mode=self.mode)
assert numpy.allclose(fn(x_val), assert np.allclose(fn(x_val),
[[362880., 181440., 120960.], [[362880., 181440., 120960.],
[90720., 72576., 60480.], [90720., 72576., 60480.],
[51840., 45360., 40320.]]) [51840., 45360., 40320.]])
fn = theano.function([x], gg, mode=self.mode) fn = theano.function([x], gg, mode=self.mode)
assert numpy.allclose(fn(x_val), assert np.allclose(fn(x_val),
[[663696., 422568., 301872.], [[663696., 422568., 301872.],
[233964., 190800., 161016.], [233964., 190800., 161016.],
[139248., 122652., 109584.]]) [139248., 122652., 109584.]])
unittest_tools.verify_grad(Prod(axis=1, no_zeros_in_input=True), unittest_tools.verify_grad(Prod(axis=1, no_zeros_in_input=True),
[x_val], [x_val],
mode=self.mode) mode=self.mode)
...@@ -697,14 +697,14 @@ class test_Prod(unittest.TestCase): ...@@ -697,14 +697,14 @@ class test_Prod(unittest.TestCase):
def test_prod_without_zeros(self): def test_prod_without_zeros(self):
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
x_val = numpy.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]], dtype='float32') x_val = np.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]], dtype='float32')
pwz = ProdWithoutZeros(axis=1)(x) pwz = ProdWithoutZeros(axis=1)(x)
fn = theano.function([x], pwz, mode=self.mode) fn = theano.function([x], pwz, mode=self.mode)
assert numpy.allclose(fn(x_val), [6, 30, 9]) assert np.allclose(fn(x_val), [6, 30, 9])
pwz_a0 = ProdWithoutZeros(axis=0)(x) pwz_a0 = ProdWithoutZeros(axis=0)(x)
fn_a0 = theano.function([x], pwz_a0, mode=self.mode) fn_a0 = theano.function([x], pwz_a0, mode=self.mode)
assert numpy.allclose(fn_a0(x_val), [1, 10, 162]) assert np.allclose(fn_a0(x_val), [1, 10, 162])
@raises(theano.gradient.NullTypeGradError) @raises(theano.gradient.NullTypeGradError)
def test_prod_without_zeros_grad(self): def test_prod_without_zeros_grad(self):
...@@ -716,33 +716,33 @@ class test_Prod(unittest.TestCase): ...@@ -716,33 +716,33 @@ class test_Prod(unittest.TestCase):
@attr('slow') @attr('slow')
def test_other_grad_tests(self): def test_other_grad_tests(self):
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
x_val1 = numpy.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]], x_val1 = np.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]],
dtype='float32') dtype='float32')
x_val2 = numpy.array([[1, 2, 0], [0, 5, 6], [7, 8, 9], [9, 10, 0]], x_val2 = np.array([[1, 2, 0], [0, 5, 6], [7, 8, 9], [9, 10, 0]],
dtype='float32') dtype='float32')
rng = rng = numpy.random.RandomState(43) rng = rng = np.random.RandomState(43)
p = Prod(axis=1) p = Prod(axis=1)
grad_p = theano.tensor.grad(p(x).sum(), x) grad_p = theano.tensor.grad(p(x).sum(), x)
grad_fn = theano.function([x], grad_p, mode=self.mode) grad_fn = theano.function([x], grad_p, mode=self.mode)
assert numpy.allclose( assert np.allclose(
grad_fn(x_val1), grad_fn(x_val1),
[[6., 3., 2.], [30., 0., 0.], [0., 0., 0.]]) [[6., 3., 2.], [30., 0., 0.], [0., 0., 0.]])
assert numpy.allclose( assert np.allclose(
grad_fn(x_val2), grad_fn(x_val2),
[[0., 0., 2.], [30., 0., 0.], [72., 63., 56.], [0., 0., 90.]]) [[0., 0., 2.], [30., 0., 0.], [72., 63., 56.], [0., 0., 90.]])
p_axis0 = Prod(axis=0) p_axis0 = Prod(axis=0)
grad_p_axis0 = theano.tensor.grad(p_axis0(x).sum(), x) grad_p_axis0 = theano.tensor.grad(p_axis0(x).sum(), x)
grad_fn_axis0 = theano.function([x], grad_p_axis0, mode=self.mode) grad_fn_axis0 = theano.function([x], grad_p_axis0, mode=self.mode)
assert numpy.allclose( assert np.allclose(
grad_fn_axis0(x_val2), grad_fn_axis0(x_val2),
[[0., 400., 0.], [63., 160., 0.], [0., 100., 0.], [0., 80., 0.]]) [[0., 400., 0.], [63., 160., 0.], [0., 100., 0.], [0., 80., 0.]])
tensor.verify_grad(p, [x_val1], rng=rng, mode=self.mode) tensor.verify_grad(p, [x_val1], rng=rng, mode=self.mode)
def test_mul_without_zeros_zeros(self): def test_mul_without_zeros_zeros(self):
a = numpy.zeros((3, 3)) a = np.zeros((3, 3))
x = theano.tensor.dmatrix() x = theano.tensor.dmatrix()
...@@ -763,13 +763,13 @@ class test_Prod(unittest.TestCase): ...@@ -763,13 +763,13 @@ class test_Prod(unittest.TestCase):
class test_IsInf_IsNan(unittest.TestCase): class test_IsInf_IsNan(unittest.TestCase):
def setUp(self): def setUp(self):
self.test_vals = [numpy.array(x, dtype=config.floatX) for x in [ self.test_vals = [np.array(x, dtype=config.floatX) for x in [
0, 0,
1, 1,
numpy.nan, np.nan,
numpy.inf, np.inf,
-numpy.inf, -np.inf,
[numpy.nan, numpy.inf, -numpy.inf, 0, 1, -1], [np.nan, np.inf, -np.inf, 0, 1, -1],
]] ]]
self.scalar = tensor.scalar() self.scalar = tensor.scalar()
self.vector = tensor.vector() self.vector = tensor.vector()
...@@ -784,7 +784,7 @@ class test_IsInf_IsNan(unittest.TestCase): ...@@ -784,7 +784,7 @@ class test_IsInf_IsNan(unittest.TestCase):
theano_isfunc = theano.function([input], theano_isfunc = theano.function([input],
getattr(tensor, isfunc)(input), getattr(tensor, isfunc)(input),
mode=self.mode) mode=self.mode)
numpy_isfunc = getattr(numpy, isfunc) numpy_isfunc = getattr(np, isfunc)
for x in self.test_vals: for x in self.test_vals:
if ((x.ndim == 0 and input is not self.scalar) or if ((x.ndim == 0 and input is not self.scalar) or
(x.ndim == 1 and input is not self.vector)): (x.ndim == 1 and input is not self.vector)):
...@@ -830,7 +830,7 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -830,7 +830,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype) dtype)
data = numpy.random.rand(3, 4) * 10 data = np.random.rand(3, 4) * 10
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -859,7 +859,7 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -859,7 +859,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo, assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype) dtype)
data = numpy.random.rand(3, 4) * 10 data = np.random.rand(3, 4) * 10
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -887,7 +887,7 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -887,7 +887,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], \ assert [n for n in topo if isinstance(n.op, self.op)], \
(topo, output_dtype) (topo, output_dtype)
data = numpy.random.rand(3, 4) * 10 data = np.random.rand(3, 4) * 10
data = data.astype(input_dtype) data = data.astype(input_dtype)
if output_dtype == 'float16' and method == 'prod': if output_dtype == 'float16' and method == 'prod':
# We will likely get something infinite, # We will likely get something infinite,
...@@ -943,17 +943,17 @@ class T_reduce_dtype(unittest.TestCase): ...@@ -943,17 +943,17 @@ class T_reduce_dtype(unittest.TestCase):
def test_reduce_precision(self): def test_reduce_precision(self):
# Check that the default accumulator precision is sufficient # Check that the default accumulator precision is sufficient
for method in self.methods: for method in self.methods:
x = theano.shared(numpy.asarray([1e8, 1, -1e8], x = theano.shared(np.asarray([1e8, 1, -1e8],
dtype='float32')) dtype='float32'))
s = getattr(x, method)() s = getattr(x, method)()
f = theano.function([], s, mode=self.mode) f = theano.function([], s, mode=self.mode)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], topo assert [n for n in topo if isinstance(n.op, self.op)], topo
s_val = f() s_val = f()
# Use extra precision in NumPy to compute the good answer. # Use extra precision in NumPy to compute the good answer.
ret = getattr(numpy.asarray([1e8, 1, -1e8], dtype='float64'), ret = getattr(np.asarray([1e8, 1, -1e8], dtype='float64'),
method)() method)()
assert numpy.allclose(s_val, ret), (s_val, ret) assert np.allclose(s_val, ret), (s_val, ret)
class T_mean_dtype(unittest.TestCase): class T_mean_dtype(unittest.TestCase):
...@@ -971,7 +971,7 @@ class T_mean_dtype(unittest.TestCase): ...@@ -971,7 +971,7 @@ class T_mean_dtype(unittest.TestCase):
else: else:
assert m.dtype == dtype, (m, m.dtype, dtype) assert m.dtype == dtype, (m, m.dtype, dtype)
f = theano.function([x], m) f = theano.function([x], m)
data = numpy.random.rand(3, 4) * 10 data = np.random.rand(3, 4) * 10
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -1005,7 +1005,7 @@ class T_mean_dtype(unittest.TestCase): ...@@ -1005,7 +1005,7 @@ class T_mean_dtype(unittest.TestCase):
input_dtype != sum_dtype): input_dtype != sum_dtype):
continue continue
f = theano.function([x], mean_var) f = theano.function([x], mean_var)
data = numpy.random.rand(3, 4) * 10 data = np.random.rand(3, 4) * 10
data = data.astype(input_dtype) data = data.astype(input_dtype)
f(data) f(data)
# Check that we can take the gradient, when implemented # Check that we can take the gradient, when implemented
...@@ -1026,11 +1026,11 @@ class T_mean_dtype(unittest.TestCase): ...@@ -1026,11 +1026,11 @@ class T_mean_dtype(unittest.TestCase):
def test_mean_precision(self): def test_mean_precision(self):
# Check that the default accumulator precision is sufficient # Check that the default accumulator precision is sufficient
x = theano.shared(numpy.asarray([1e8, 1, -1e8], dtype='float32')) x = theano.shared(np.asarray([1e8, 1, -1e8], dtype='float32'))
m = x.mean() m = x.mean()
f = theano.function([], m) f = theano.function([], m)
m_val = f() m_val = f()
assert numpy.allclose(m_val, 1. / 3) assert np.allclose(m_val, 1. / 3)
class T_prod_without_zeros_dtype(unittest.TestCase): class T_prod_without_zeros_dtype(unittest.TestCase):
...@@ -1077,7 +1077,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase): ...@@ -1077,7 +1077,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
if 'complex' in dtype: if 'complex' in dtype:
continue continue
f = theano.function([x], p) f = theano.function([x], p)
data = numpy.random.rand(2, 3) * 3 data = np.random.rand(2, 3) * 3
data = data.astype(dtype) data = data.astype(dtype)
f(data) f(data)
...@@ -1100,7 +1100,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase): ...@@ -1100,7 +1100,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
'complex' in input_dtype): 'complex' in input_dtype):
continue continue
f = theano.function([x], prod_woz_var) f = theano.function([x], prod_woz_var)
data = numpy.random.rand(2, 3) * 3 data = np.random.rand(2, 3) * 3
data = data.astype(input_dtype) data = data.astype(input_dtype)
f(data) f(data)
...@@ -1129,7 +1129,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase): ...@@ -1129,7 +1129,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
input_dtype != acc_dtype): input_dtype != acc_dtype):
continue continue
f = theano.function([x], prod_woz_var) f = theano.function([x], prod_woz_var)
data = numpy.random.rand(2, 3) * 3 data = np.random.rand(2, 3) * 3
data = data.astype(input_dtype) data = data.astype(input_dtype)
f(data) f(data)
else: else:
...@@ -1143,7 +1143,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase): ...@@ -1143,7 +1143,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
class TestBitOpReduceGrad(unittest.TestCase): class TestBitOpReduceGrad(unittest.TestCase):
def setUp(self): def setUp(self):
self.rng = numpy.random.RandomState(unittest_tools.fetch_seed()) self.rng = np.random.RandomState(unittest_tools.fetch_seed())
def test_all_grad(self): def test_all_grad(self):
x = tensor.bmatrix('x') x = tensor.bmatrix('x')
...@@ -1152,11 +1152,11 @@ class TestBitOpReduceGrad(unittest.TestCase): ...@@ -1152,11 +1152,11 @@ class TestBitOpReduceGrad(unittest.TestCase):
f = theano.function([x], gx) f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8') x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random, for x_val in (x_random,
numpy.zeros_like(x_random), np.zeros_like(x_random),
numpy.ones_like(x_random)): np.ones_like(x_random)):
gx_val = f(x_val) gx_val = f(x_val)
assert gx_val.shape == x_val.shape assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0) assert np.all(gx_val == 0)
def test_any_grad(self): def test_any_grad(self):
x = tensor.bmatrix('x') x = tensor.bmatrix('x')
...@@ -1165,11 +1165,11 @@ class TestBitOpReduceGrad(unittest.TestCase): ...@@ -1165,11 +1165,11 @@ class TestBitOpReduceGrad(unittest.TestCase):
f = theano.function([x], gx) f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8') x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random, for x_val in (x_random,
numpy.zeros_like(x_random), np.zeros_like(x_random),
numpy.ones_like(x_random)): np.ones_like(x_random)):
gx_val = f(x_val) gx_val = f(x_val)
assert gx_val.shape == x_val.shape assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0) assert np.all(gx_val == 0)
class TestElemwise(unittest_tools.InferShapeTester): class TestElemwise(unittest_tools.InferShapeTester):
...@@ -1195,8 +1195,8 @@ class TestElemwise(unittest_tools.InferShapeTester): ...@@ -1195,8 +1195,8 @@ class TestElemwise(unittest_tools.InferShapeTester):
dtype = theano.config.floatX dtype = theano.config.floatX
t_left = TensorType(dtype, [(entry == 1) for entry in s_left])() t_left = TensorType(dtype, [(entry == 1) for entry in s_left])()
t_right = TensorType(dtype, [(entry == 1) for entry in s_right])() t_right = TensorType(dtype, [(entry == 1) for entry in s_right])()
t_left_val = numpy.zeros(s_left, dtype=dtype) t_left_val = np.zeros(s_left, dtype=dtype)
t_right_val = numpy.zeros(s_right, dtype=dtype) t_right_val = np.zeros(s_right, dtype=dtype)
self._compile_and_check( self._compile_and_check(
[t_left, t_right], [t_left, t_right],
[Elemwise(scalar.add)(t_left, t_right)], [Elemwise(scalar.add)(t_left, t_right)],
...@@ -1210,7 +1210,7 @@ class TestElemwise(unittest_tools.InferShapeTester): ...@@ -1210,7 +1210,7 @@ class TestElemwise(unittest_tools.InferShapeTester):
s = a + b + c + d + e + f s = a + b + c + d + e + f
g = theano.function([a, b, c, d, e, f], s, g = theano.function([a, b, c, d, e, f], s,
mode=theano.compile.Mode(linker='py')) mode=theano.compile.Mode(linker='py'))
g(*[numpy.zeros(2 ** 11, config.floatX) for i in xrange(6)]) g(*[np.zeros(2 ** 11, config.floatX) for i in xrange(6)])
def test_gt_grad(): def test_gt_grad():
...@@ -1226,9 +1226,9 @@ def test_gt_grad(): ...@@ -1226,9 +1226,9 @@ def test_gt_grad():
T = theano.tensor T = theano.tensor
input_ = T.vector(dtype=floatX) input_ = T.vector(dtype=floatX)
random_values = numpy.random.RandomState(1234).uniform( random_values = np.random.RandomState(1234).uniform(
low=-1, high=1, size=(2, 2)) low=-1, high=1, size=(2, 2))
W_values = numpy.asarray(random_values, dtype=floatX) W_values = np.asarray(random_values, dtype=floatX)
W = theano.shared(value=W_values, name='weights') W = theano.shared(value=W_values, name='weights')
correct_score = T.dot(input_, W) correct_score = T.dot(input_, W)
wrong_input = T.vector(dtype=floatX) wrong_input = T.vector(dtype=floatX)
...@@ -1258,7 +1258,7 @@ def test_clip_grad(): ...@@ -1258,7 +1258,7 @@ def test_clip_grad():
# use an x value less than y, an x value between y and z, and an x value # use an x value less than y, an x value between y and z, and an x value
# greater than z # greater than z
unittest_tools.verify_grad(func, unittest_tools.verify_grad(func,
[numpy.asarray([-1., 0.5, 2.]), 0., 1.]) [np.asarray([-1., 0.5, 2.]), 0., 1.])
def test_grad_useless_sum(): def test_grad_useless_sum():
...@@ -1287,16 +1287,16 @@ def test_grad_useless_sum(): ...@@ -1287,16 +1287,16 @@ def test_grad_useless_sum():
tensor.type.values_eq_approx_remove_nan) tensor.type.values_eq_approx_remove_nan)
try: try:
for test_value in test_values: for test_value in test_values:
outputs.append(f(numpy.array([test_value]).astype('float32'))) outputs.append(f(np.array([test_value]).astype('float32')))
finally: finally:
TensorType.values_eq_approx = old_values_eq_approx TensorType.values_eq_approx = old_values_eq_approx
assert not any([isinstance(node.op, theano.tensor.elemwise.Sum) for node in nodes]) assert not any([isinstance(node.op, theano.tensor.elemwise.Sum) for node in nodes])
assert numpy.allclose(outputs, [[-3.72007598e-44], assert np.allclose(outputs, [[-3.72007598e-44],
[-0.26894142], [-0.26894142],
[-0.5], [-0.5],
[-0.73105858], [-0.73105858],
[-1.]]) [-1.]])
def test_elemwise_grad_broadcast(): def test_elemwise_grad_broadcast():
......
...@@ -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.],
......
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)
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
from numpy.testing import dec from numpy.testing import dec
import theano import theano
...@@ -10,7 +10,7 @@ from theano.tensor.fourier import Fourier, fft ...@@ -10,7 +10,7 @@ from theano.tensor.fourier import Fourier, fft
class TestFourier(utt.InferShapeTester): class TestFourier(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestFourier, self).setUp() super(TestFourier, self).setUp()
...@@ -20,24 +20,24 @@ class TestFourier(utt.InferShapeTester): ...@@ -20,24 +20,24 @@ class TestFourier(utt.InferShapeTester):
def test_perform(self): def test_perform(self):
a = tensor.dmatrix() a = tensor.dmatrix()
f = theano.function([a], self.op(a, n=10, axis=0)) f = theano.function([a], self.op(a, n=10, axis=0))
a = numpy.random.rand(8, 6) a = np.random.rand(8, 6)
assert numpy.allclose(f(a), numpy.fft.fft(a, 10, 0)) assert np.allclose(f(a), np.fft.fft(a, 10, 0))
def test_infer_shape(self): def test_infer_shape(self):
a = tensor.dvector() a = tensor.dvector()
self._compile_and_check([a], [self.op(a, 16, 0)], self._compile_and_check([a], [self.op(a, 16, 0)],
[numpy.random.rand(12)], [np.random.rand(12)],
self.op_class) self.op_class)
a = tensor.dmatrix() a = tensor.dmatrix()
for var in [self.op(a, 16, 1), self.op(a, None, 1), for var in [self.op(a, 16, 1), self.op(a, None, 1),
self.op(a, 16, None), self.op(a, None, None)]: self.op(a, 16, None), self.op(a, None, None)]:
self._compile_and_check([a], [var], self._compile_and_check([a], [var],
[numpy.random.rand(12, 4)], [np.random.rand(12, 4)],
self.op_class) self.op_class)
b = tensor.iscalar() b = tensor.iscalar()
for var in [self.op(a, 16, b), self.op(a, None, b)]: for var in [self.op(a, 16, b), self.op(a, None, b)]:
self._compile_and_check([a, b], [var], self._compile_and_check([a, b], [var],
[numpy.random.rand(12, 4), 0], [np.random.rand(12, 4), 0],
self.op_class) self.op_class)
@dec.skipif(True, "Complex grads not enabled, see #178") @dec.skipif(True, "Complex grads not enabled, see #178")
...@@ -54,10 +54,10 @@ class TestFourier(utt.InferShapeTester): ...@@ -54,10 +54,10 @@ class TestFourier(utt.InferShapeTester):
def fft_test4(a): def fft_test4(a):
return self.op(a, 4, 0) return self.op(a, 4, 0)
pts = [numpy.random.rand(5, 2, 4, 3), pts = [np.random.rand(5, 2, 4, 3),
numpy.random.rand(2, 3, 4), np.random.rand(2, 3, 4),
numpy.random.rand(2, 5), np.random.rand(2, 5),
numpy.random.rand(5)] np.random.rand(5)]
for fft_test in [fft_test1, fft_test2, fft_test3, fft_test4]: for fft_test in [fft_test1, fft_test2, fft_test3, fft_test4]:
for pt in pts: for pt in pts:
theano.gradient.verify_grad(fft_test, [pt], theano.gradient.verify_grad(fft_test, [pt],
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import six.moves.cPickle as pickle import six.moves.cPickle as pickle
from six.moves import xrange from six.moves import xrange
import theano import theano
...@@ -13,10 +13,10 @@ def test_no_reuse(): ...@@ -13,10 +13,10 @@ def test_no_reuse():
f = theano.function([x, y], x + y) f = theano.function([x, y], x + y)
# provide both inputs in the first call # provide both inputs in the first call
f(numpy.ones(10, dtype='int64'), numpy.ones(10, dtype='int64')) f(np.ones(10, dtype='int64'), np.ones(10, dtype='int64'))
try: try:
f(numpy.ones(10)) f(np.ones(10))
except TypeError: except TypeError:
return return
assert not 'should not get here' assert not 'should not get here'
...@@ -78,8 +78,8 @@ def test_gc_never_pickles_temporaries(): ...@@ -78,8 +78,8 @@ def test_gc_never_pickles_temporaries():
# now run the function once to create temporaries within the no-gc # now run the function once to create temporaries within the no-gc
# linker # linker
f(numpy.ones(100, dtype='float64')) f(np.ones(100, dtype='float64'))
g(numpy.ones(100, dtype='float64')) g(np.ones(100, dtype='float64'))
# serialize the functions again # serialize the functions again
post_f = pickle.dumps(f) post_f = pickle.dumps(f)
......
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
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
import theano import theano
...@@ -43,13 +43,13 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -43,13 +43,13 @@ class Test_inc_subtensor(unittest.TestCase):
f = theano.function([a, increment, sl2_end], resut) f = theano.function([a, increment, sl2_end], resut)
val_a = numpy.ones((5, 5)) val_a = np.ones((5, 5))
val_inc = 2.3 val_inc = 2.3
val_sl2_end = 2 val_sl2_end = 2
result = f(val_a, val_inc, val_sl2_end) result = f(val_a, val_inc, val_sl2_end)
expected_result = numpy.copy(val_a) expected_result = np.copy(val_a)
if do_set: if do_set:
expected_result[:, :val_sl2_end] = val_inc expected_result[:, :val_sl2_end] = val_inc
else: else:
...@@ -71,7 +71,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -71,7 +71,7 @@ class Test_inc_subtensor(unittest.TestCase):
# These symbolic graphs legitimate, as long as increment has exactly # These symbolic graphs legitimate, as long as increment has exactly
# one element. So it should fail at runtime, not at compile time. # one element. So it should fail at runtime, not at compile time.
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
def rng_randX(*shape): def rng_randX(*shape):
return rng.rand(*shape).astype(theano.config.floatX) return rng.rand(*shape).astype(theano.config.floatX)
...@@ -101,7 +101,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -101,7 +101,7 @@ class Test_inc_subtensor(unittest.TestCase):
sl2 = slice(sl2_end) sl2 = slice(sl2_end)
sl3 = 2 sl3 = 2
val_a = numpy.ones((5, 3, 4)) val_a = np.ones((5, 3, 4))
val_inc = 2.3 val_inc = 2.3
val_sl2_end = 2 val_sl2_end = 2
...@@ -112,7 +112,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -112,7 +112,7 @@ class Test_inc_subtensor(unittest.TestCase):
f = theano.function([a, increment, sl2_end], resut) f = theano.function([a, increment, sl2_end], resut)
expected_result = numpy.copy(val_a) expected_result = np.copy(val_a)
result = f(val_a, val_inc, val_sl2_end) result = f(val_a, val_inc, val_sl2_end)
if method is tt.set_subtensor: if method is tt.set_subtensor:
...@@ -127,7 +127,7 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -127,7 +127,7 @@ class Test_inc_subtensor(unittest.TestCase):
f = theano.function([a, increment, sl2_end], resut) f = theano.function([a, increment, sl2_end], resut)
expected_result = numpy.copy(val_a) expected_result = np.copy(val_a)
result = f(val_a, val_inc, val_sl2_end) result = f(val_a, val_inc, val_sl2_end)
if method is tt.set_subtensor: if method is tt.set_subtensor:
...@@ -152,23 +152,23 @@ class Test_inc_subtensor(unittest.TestCase): ...@@ -152,23 +152,23 @@ class Test_inc_subtensor(unittest.TestCase):
# vector # vector
utt.verify_grad( utt.verify_grad(
f_slice(slice(2, 4, None)), f_slice(slice(2, 4, None)),
(numpy.asarray([0, 1, 2, 3, 4, 5.]), (np.asarray([0, 1, 2, 3, 4, 5.]),
numpy.asarray([9, 9.]), )) np.asarray([9, 9.]), ))
# matrix # matrix
utt.verify_grad( utt.verify_grad(
f_slice(slice(1, 2, None), slice(None, None, None)), f_slice(slice(1, 2, None), slice(None, None, None)),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]), (np.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray([[9, 9.]]), )) np.asarray([[9, 9.]]), ))
# single element # single element
utt.verify_grad( utt.verify_grad(
f_slice(2, 1), f_slice(2, 1),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]), (np.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray(9.),)) np.asarray(9.),))
# broadcast # broadcast
utt.verify_grad( utt.verify_grad(
f_slice(2), f_slice(2),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]), (np.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray(9.),)) np.asarray(9.),))
...@@ -2,17 +2,17 @@ from __future__ import absolute_import, print_function, division ...@@ -2,17 +2,17 @@ from __future__ import absolute_import, print_function, division
import unittest import unittest
import theano import theano
from theano import tensor, function, Variable, Generic from theano import tensor, function, Variable, Generic
import numpy import numpy as np
import os import os
class T_load_tensor(unittest.TestCase): class T_load_tensor(unittest.TestCase):
def setUp(self): def setUp(self):
self.data = numpy.arange(5, dtype=numpy.int32) self.data = np.arange(5, dtype=np.int32)
self.filename = os.path.join( self.filename = os.path.join(
theano.config.compiledir, theano.config.compiledir,
"_test.npy") "_test.npy")
numpy.save(self.filename, self.data) np.save(self.filename, self.data)
def test0(self): def test0(self):
path = Variable(Generic()) path = Variable(Generic())
...@@ -49,7 +49,7 @@ class T_load_tensor(unittest.TestCase): ...@@ -49,7 +49,7 @@ class T_load_tensor(unittest.TestCase):
path = Variable(Generic()) path = Variable(Generic())
x = tensor.load(path, 'int32', (False,), mmap_mode='c') x = tensor.load(path, 'int32', (False,), mmap_mode='c')
fn = function([path], x) fn = function([path], x)
assert type(fn(self.filename)) == numpy.core.memmap assert type(fn(self.filename)) == np.core.memmap
def tearDown(self): def tearDown(self):
os.remove(os.path.join( os.remove(os.path.join(
......
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 six import integer_types from six import integer_types
import theano import theano
...@@ -42,7 +42,7 @@ class TestKeepDims(unittest.TestCase): ...@@ -42,7 +42,7 @@ class TestKeepDims(unittest.TestCase):
def test_keepdims(self): def test_keepdims(self):
x = tensor.dtensor3() x = tensor.dtensor3()
a = numpy.random.rand(3, 2, 4) a = np.random.rand(3, 2, 4)
# We don't need to test all opt and C code, as this is tested # We don't need to test all opt and C code, as this is tested
# by the ops tests. # by the ops tests.
mode = theano.compile.Mode(optimizer="fast_compile", linker="py") mode = theano.compile.Mode(optimizer="fast_compile", linker="py")
...@@ -60,7 +60,7 @@ class TestKeepDims(unittest.TestCase): ...@@ -60,7 +60,7 @@ class TestKeepDims(unittest.TestCase):
axis)], axis)],
mode=mode) mode=mode)
ans1, ans2 = f(a) ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
f = function([x], [op(x, axis=axis, keepdims=True)[1], f = function([x], [op(x, axis=axis, keepdims=True)[1],
...@@ -69,7 +69,7 @@ class TestKeepDims(unittest.TestCase): ...@@ -69,7 +69,7 @@ class TestKeepDims(unittest.TestCase):
axis)], axis)],
mode=mode) mode=mode)
ans1, ans2 = f(a) ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
# the following ops can be specified with either a single axis or every # the following ops can be specified with either a single axis or every
...@@ -84,7 +84,7 @@ class TestKeepDims(unittest.TestCase): ...@@ -84,7 +84,7 @@ class TestKeepDims(unittest.TestCase):
axis)], axis)],
mode=mode) mode=mode)
ans1, ans2 = f(a) ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
# the following ops can be specified with a freely specified axis # the following ops can be specified with a freely specified axis
...@@ -103,13 +103,13 @@ class TestKeepDims(unittest.TestCase): ...@@ -103,13 +103,13 @@ class TestKeepDims(unittest.TestCase):
mode=mode) mode=mode)
ans1, ans2 = f(a) ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
def test_norm(self): def test_norm(self):
x = tensor.dtensor3() x = tensor.dtensor3()
a = numpy.random.rand(3, 2, 4).astype(theano.config.floatX) a = np.random.rand(3, 2, 4).astype(theano.config.floatX)
mode = theano.compile.Mode(optimizer="fast_compile", linker="py") mode = theano.compile.Mode(optimizer="fast_compile", linker="py")
for axis in [0, 1, 2, [0], [1], [2], None, for axis in [0, 1, 2, [0], [1], [2], None,
...@@ -121,7 +121,7 @@ class TestKeepDims(unittest.TestCase): ...@@ -121,7 +121,7 @@ class TestKeepDims(unittest.TestCase):
], mode=mode) ], mode=mode)
ans1, ans2 = f(a) ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
g = function([x], [x.norm(L=2, axis=axis, keepdims=True), g = function([x], [x.norm(L=2, axis=axis, keepdims=True),
...@@ -129,5 +129,5 @@ class TestKeepDims(unittest.TestCase): ...@@ -129,5 +129,5 @@ class TestKeepDims(unittest.TestCase):
], mode=mode) ], mode=mode)
ans1, ans2 = g(a) ans1, ans2 = g(a)
assert numpy.allclose(ans1, ans2) assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape assert ans1.shape == ans2.shape
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
from theano.gof.type import Type from theano.gof.type import Type
from theano.gof.graph import Variable, Apply, Constant from theano.gof.graph import Variable, Apply, Constant
from theano.gof.op import Op from theano.gof.op import Op
...@@ -62,8 +62,8 @@ def test_merge_with_weird_eq(): ...@@ -62,8 +62,8 @@ def test_merge_with_weird_eq():
"""numpy arrays don't compare equal like other python objects""" """numpy arrays don't compare equal like other python objects"""
# SCALAR CASE # SCALAR CASE
x = T.constant(numpy.asarray(1), name='x') x = T.constant(np.asarray(1), name='x')
y = T.constant(numpy.asarray(1), name='y') y = T.constant(np.asarray(1), name='y')
g = Env([x, y], [x+y]) g = Env([x, y], [x+y])
MergeOptimizer().optimize(g) MergeOptimizer().optimize(g)
...@@ -74,8 +74,8 @@ def test_merge_with_weird_eq(): ...@@ -74,8 +74,8 @@ def test_merge_with_weird_eq():
# NONSCALAR CASE # NONSCALAR CASE
# This was created to test TensorConstantSignature # This was created to test TensorConstantSignature
x = T.constant(numpy.ones(5), name='x') x = T.constant(np.ones(5), name='x')
y = T.constant(numpy.ones(5), name='y') y = T.constant(np.ones(5), name='y')
g = Env([x, y], [x+y]) g = Env([x, y], [x+y])
MergeOptimizer().optimize(g) MergeOptimizer().optimize(g)
......
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import copy import copy
import sys import sys
import numpy import numpy as np
import theano import theano
from theano import tensor from theano import tensor
from theano.tensor.nnet import crossentropy_softmax_argmax_1hot_with_bias from theano.tensor.nnet import crossentropy_softmax_argmax_1hot_with_bias
...@@ -12,7 +12,7 @@ def test_bug_2009_06_02_trac_387(): ...@@ -12,7 +12,7 @@ def test_bug_2009_06_02_trac_387():
f = theano.function([y], f = theano.function([y],
tensor.int_div( tensor.int_div(
tensor.DimShuffle(y[0].broadcastable, ['x'])(y[0]), 2)) tensor.DimShuffle(y[0].broadcastable, ['x'])(y[0]), 2))
print(f(numpy.ones(1, dtype='int64') * 3)) print(f(np.ones(1, dtype='int64') * 3))
# XXX: there is no assert, nor comment that DEBUGMODE is to do the # XXX: there is no assert, nor comment that DEBUGMODE is to do the
# checking. What was the bug, and how is it being tested? # checking. What was the bug, and how is it being tested?
...@@ -25,15 +25,15 @@ def test_bug_2009_07_17_borrowed_output(): ...@@ -25,15 +25,15 @@ def test_bug_2009_07_17_borrowed_output():
g = theano.function([a, b], g = theano.function([a, b],
theano.Out(theano.tensor.dot(a, b), borrow=False)) theano.Out(theano.tensor.dot(a, b), borrow=False))
x = numpy.zeros((1, 2)) x = np.zeros((1, 2))
y = numpy.ones((2, 5)) y = np.ones((2, 5))
z = g(x, y) z = g(x, y)
print(z) # Should be zero. print(z) # Should be zero.
x.fill(1) x.fill(1)
print(g(x, y)) # Should be non-zero. print(g(x, y)) # Should be non-zero.
print(z) # Should still be zero. print(z) # Should still be zero.
assert numpy.linalg.norm(z) == 0 assert np.linalg.norm(z) == 0
# The code above was supposed to fail when it was written (or, more # The code above was supposed to fail when it was written (or, more
# accurately, on the next revision, i.e. when it was merged with the # accurately, on the next revision, i.e. when it was merged with the
...@@ -55,9 +55,9 @@ def test_bug_2009_07_17_borrowed_output(): ...@@ -55,9 +55,9 @@ def test_bug_2009_07_17_borrowed_output():
g = theano.function([test_output_activation_no_bias, test_b2, test_target], g = theano.function([test_output_activation_no_bias, test_b2, test_target],
theano.Out(output, borrow=False)) theano.Out(output, borrow=False))
a = numpy.zeros((1, 5)) a = np.zeros((1, 5))
b = numpy.ones(5) b = np.ones(5)
c = numpy.zeros(1, dtype=numpy.int32) c = np.zeros(1, dtype=np.int32)
z = g(a, b, c) z = g(a, b, c)
z_backup = copy.copy(z) z_backup = copy.copy(z)
...@@ -80,5 +80,5 @@ def test_deepcopied_type_filter(): ...@@ -80,5 +80,5 @@ def test_deepcopied_type_filter():
# As of commit 731e2d2fa68487733320d341d08b454a50c90d12 # As of commit 731e2d2fa68487733320d341d08b454a50c90d12
# it was failing. # it was failing.
a.type.filter( a.type.filter(
numpy.ones((2, 2), dtype=a.dtype), np.ones((2, 2), dtype=a.dtype),
strict=True) strict=True)
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.linalg import numpy.linalg
from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose from numpy.testing import dec, assert_array_equal, assert_allclose
...@@ -28,7 +28,7 @@ from nose.tools import assert_raises ...@@ -28,7 +28,7 @@ from nose.tools import assert_raises
def test_pseudoinverse_correctness(): def test_pseudoinverse_correctness():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
d1 = rng.randint(4) + 2 d1 = rng.randint(4) + 2
d2 = rng.randint(4) + 2 d2 = rng.randint(4) + 2
r = rng.randn(d1, d2).astype(theano.config.floatX) r = rng.randn(d1, d2).astype(theano.config.floatX)
...@@ -41,8 +41,8 @@ def test_pseudoinverse_correctness(): ...@@ -41,8 +41,8 @@ def test_pseudoinverse_correctness():
assert ri.shape[1] == r.shape[0] assert ri.shape[1] == r.shape[0]
assert ri.dtype == r.dtype assert ri.dtype == r.dtype
# Note that pseudoinverse can be quite unprecise so I prefer to compare # Note that pseudoinverse can be quite unprecise so I prefer to compare
# the result with what numpy.linalg returns # the result with what np.linalg returns
assert _allclose(ri, numpy.linalg.pinv(r)) assert _allclose(ri, np.linalg.pinv(r))
class test_MatrixInverse(utt.InferShapeTester): class test_MatrixInverse(utt.InferShapeTester):
...@@ -50,7 +50,7 @@ class test_MatrixInverse(utt.InferShapeTester): ...@@ -50,7 +50,7 @@ class test_MatrixInverse(utt.InferShapeTester):
super(test_MatrixInverse, self).setUp() super(test_MatrixInverse, self).setUp()
self.op_class = MatrixInverse self.op_class = MatrixInverse
self.op = matrix_inverse self.op = matrix_inverse
self.rng = numpy.random.RandomState(utt.fetch_seed()) self.rng = np.random.RandomState(utt.fetch_seed())
def test_inverse_correctness(self): def test_inverse_correctness(self):
...@@ -63,11 +63,11 @@ class test_MatrixInverse(utt.InferShapeTester): ...@@ -63,11 +63,11 @@ class test_MatrixInverse(utt.InferShapeTester):
assert ri.shape == r.shape assert ri.shape == r.shape
assert ri.dtype == r.dtype assert ri.dtype == r.dtype
rir = numpy.dot(ri, r) rir = np.dot(ri, r)
rri = numpy.dot(r, ri) rri = np.dot(r, ri)
assert _allclose(numpy.identity(4), rir), rir assert _allclose(np.identity(4), rir), rir
assert _allclose(numpy.identity(4), rri), rri assert _allclose(np.identity(4), rri), rri
def test_infer_shape(self): def test_infer_shape(self):
...@@ -81,7 +81,7 @@ class test_MatrixInverse(utt.InferShapeTester): ...@@ -81,7 +81,7 @@ class test_MatrixInverse(utt.InferShapeTester):
def test_matrix_dot(): def test_matrix_dot():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
n = rng.randint(4) + 2 n = rng.randint(4) + 2
rs = [] rs = []
xs = [] xs = []
...@@ -93,26 +93,26 @@ def test_matrix_dot(): ...@@ -93,26 +93,26 @@ def test_matrix_dot():
theano_sol = function(xs, sol)(*rs) theano_sol = function(xs, sol)(*rs)
numpy_sol = rs[0] numpy_sol = rs[0]
for r in rs[1:]: for r in rs[1:]:
numpy_sol = numpy.dot(numpy_sol, r) numpy_sol = np.dot(numpy_sol, r)
assert _allclose(numpy_sol, theano_sol) assert _allclose(numpy_sol, theano_sol)
def test_qr_modes(): def test_qr_modes():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX) A = tensor.matrix("A", dtype=theano.config.floatX)
a = rng.rand(4, 4).astype(theano.config.floatX) a = rng.rand(4, 4).astype(theano.config.floatX)
f = function([A], qr(A)) f = function([A], qr(A))
t_qr = f(a) t_qr = f(a)
n_qr = numpy.linalg.qr(a) n_qr = np.linalg.qr(a)
assert _allclose(n_qr, t_qr) assert _allclose(n_qr, t_qr)
for mode in ["reduced", "r", "raw"]: for mode in ["reduced", "r", "raw"]:
f = function([A], qr(A, mode)) f = function([A], qr(A, mode))
t_qr = f(a) t_qr = f(a)
n_qr = numpy.linalg.qr(a, mode) n_qr = np.linalg.qr(a, mode)
if isinstance(n_qr, (list, tuple)): if isinstance(n_qr, (list, tuple)):
assert _allclose(n_qr[0], t_qr[0]) assert _allclose(n_qr[0], t_qr[0])
assert _allclose(n_qr[1], t_qr[1]) assert _allclose(n_qr[1], t_qr[1])
...@@ -120,7 +120,7 @@ def test_qr_modes(): ...@@ -120,7 +120,7 @@ def test_qr_modes():
assert _allclose(n_qr, t_qr) assert _allclose(n_qr, t_qr)
try: try:
n_qr = numpy.linalg.qr(a, "complete") n_qr = np.linalg.qr(a, "complete")
f = function([A], qr(A, "complete")) f = function([A], qr(A, "complete"))
t_qr = f(a) t_qr = f(a)
assert _allclose(n_qr, t_qr) assert _allclose(n_qr, t_qr)
...@@ -129,12 +129,12 @@ def test_qr_modes(): ...@@ -129,12 +129,12 @@ def test_qr_modes():
def test_svd(): def test_svd():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX) A = tensor.matrix("A", dtype=theano.config.floatX)
U, V, T = svd(A) U, V, T = svd(A)
fn = function([A], [U, V, T]) fn = function([A], [U, V, T])
a = rng.rand(4, 4).astype(theano.config.floatX) a = rng.rand(4, 4).astype(theano.config.floatX)
n_u, n_v, n_t = numpy.linalg.svd(a) n_u, n_v, n_t = np.linalg.svd(a)
t_u, t_v, t_t = fn(a) t_u, t_v, t_t = fn(a)
assert _allclose(n_u, t_u) assert _allclose(n_u, t_u)
...@@ -143,19 +143,19 @@ def test_svd(): ...@@ -143,19 +143,19 @@ def test_svd():
def test_tensorsolve(): def test_tensorsolve():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = tensor.tensor4("A", dtype=theano.config.floatX) A = tensor.tensor4("A", dtype=theano.config.floatX)
B = tensor.matrix("B", dtype=theano.config.floatX) B = tensor.matrix("B", dtype=theano.config.floatX)
X = tensorsolve(A, B) X = tensorsolve(A, B)
fn = function([A, B], [X]) fn = function([A, B], [X])
# slightly modified example from numpy.linalg.tensorsolve docstring # slightly modified example from np.linalg.tensorsolve docstring
a = numpy.eye(2 * 3 * 4).astype(theano.config.floatX) a = np.eye(2 * 3 * 4).astype(theano.config.floatX)
a.shape = (2 * 3, 4, 2, 3 * 4) a.shape = (2 * 3, 4, 2, 3 * 4)
b = rng.rand(2 * 3, 4).astype(theano.config.floatX) b = rng.rand(2 * 3, 4).astype(theano.config.floatX)
n_x = numpy.linalg.tensorsolve(a, b) n_x = np.linalg.tensorsolve(a, b)
t_x = fn(a, b) t_x = fn(a, b)
assert _allclose(n_x, t_x) assert _allclose(n_x, t_x)
...@@ -165,10 +165,10 @@ def test_tensorsolve(): ...@@ -165,10 +165,10 @@ def test_tensorsolve():
Y = tensorsolve(C, D) Y = tensorsolve(C, D)
fn = function([C, D], [Y]) fn = function([C, D], [Y])
c = numpy.eye(2 * 3 * 4, dtype='float32') c = np.eye(2 * 3 * 4, dtype='float32')
c.shape = (2 * 3, 4, 2, 3 * 4) c.shape = (2 * 3, 4, 2, 3 * 4)
d = rng.rand(2 * 3, 4).astype('float64') d = rng.rand(2 * 3, 4).astype('float64')
n_y = numpy.linalg.tensorsolve(c, d) n_y = np.linalg.tensorsolve(c, d)
t_y = fn(c, d) t_y = fn(c, d)
assert _allclose(n_y, t_y) assert _allclose(n_y, t_y)
assert n_y.dtype == Y.dtype assert n_y.dtype == Y.dtype
...@@ -179,68 +179,68 @@ def test_tensorsolve(): ...@@ -179,68 +179,68 @@ def test_tensorsolve():
Z = tensorsolve(E, F) Z = tensorsolve(E, F)
fn = function([E, F], [Z]) fn = function([E, F], [Z])
e = numpy.eye(2 * 3 * 4, dtype='int32') e = np.eye(2 * 3 * 4, dtype='int32')
e.shape = (2 * 3, 4, 2, 3 * 4) e.shape = (2 * 3, 4, 2, 3 * 4)
f = rng.rand(2 * 3, 4).astype('float64') f = rng.rand(2 * 3, 4).astype('float64')
n_z = numpy.linalg.tensorsolve(e, f) n_z = np.linalg.tensorsolve(e, f)
t_z = fn(e, f) t_z = fn(e, f)
assert _allclose(n_z, t_z) assert _allclose(n_z, t_z)
assert n_z.dtype == Z.dtype assert n_z.dtype == Z.dtype
def test_inverse_singular(): def test_inverse_singular():
singular = numpy.array([[1, 0, 0]] + [[0, 1, 0]] * 2, singular = np.array([[1, 0, 0]] + [[0, 1, 0]] * 2,
dtype=theano.config.floatX) dtype=theano.config.floatX)
a = tensor.matrix() a = tensor.matrix()
f = function([a], matrix_inverse(a)) f = function([a], matrix_inverse(a))
try: try:
f(singular) f(singular)
except numpy.linalg.LinAlgError: except np.linalg.LinAlgError:
return return
assert False assert False
def test_inverse_grad(): def test_inverse_grad():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(4, 4) r = rng.randn(4, 4)
tensor.verify_grad(matrix_inverse, [r], rng=numpy.random) tensor.verify_grad(matrix_inverse, [r], rng=np.random)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(4, 4) r = rng.randn(4, 4)
tensor.verify_grad(matrix_inverse, [r], rng=numpy.random) tensor.verify_grad(matrix_inverse, [r], rng=np.random)
def test_det(): def test_det():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
x = tensor.matrix() x = tensor.matrix()
f = theano.function([x], det(x)) f = theano.function([x], det(x))
assert numpy.allclose(numpy.linalg.det(r), f(r)) assert np.allclose(np.linalg.det(r), f(r))
def test_det_grad(): def test_det_grad():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
tensor.verify_grad(det, [r], rng=numpy.random) tensor.verify_grad(det, [r], rng=np.random)
def test_det_shape(): def test_det_shape():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
x = tensor.matrix() x = tensor.matrix()
f = theano.function([x], det(x)) f = theano.function([x], det(x))
f_shape = theano.function([x], det(x).shape) f_shape = theano.function([x], det(x).shape)
assert numpy.all(f(r).shape == f_shape(r)) assert np.all(f(r).shape == f_shape(r))
class test_diag(unittest.TestCase): class test_diag(unittest.TestCase):
""" """
Test that linalg.diag has the same behavior as numpy.diag. Test that linalg.diag has the same behavior as np.diag.
numpy.diag has two behaviors: np.diag has two behaviors:
(1) when given a vector, it returns a matrix with that vector as the (1) when given a vector, it returns a matrix with that vector as the
diagonal. diagonal.
(2) when given a matrix, returns a vector which is the diagonal of the (2) when given a matrix, returns a vector which is the diagonal of the
...@@ -263,7 +263,7 @@ class test_diag(unittest.TestCase): ...@@ -263,7 +263,7 @@ class test_diag(unittest.TestCase):
super(test_diag, self).__init__(name) super(test_diag, self).__init__(name)
def test_alloc_diag(self): def test_alloc_diag(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = theano.tensor.vector() x = theano.tensor.vector()
g = alloc_diag(x) g = alloc_diag(x)
f = theano.function([x], g) f = theano.function([x], g)
...@@ -271,7 +271,7 @@ class test_diag(unittest.TestCase): ...@@ -271,7 +271,7 @@ class test_diag(unittest.TestCase):
# test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1 # test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
for shp in [5, 0, 1]: for shp in [5, 0, 1]:
m = rng.rand(shp).astype(self.floatX) m = rng.rand(shp).astype(self.floatX)
v = numpy.diag(m) v = np.diag(m)
r = f(m) r = f(m)
# The right matrix is created # The right matrix is created
assert (r == v).all() assert (r == v).all()
...@@ -295,7 +295,7 @@ class test_diag(unittest.TestCase): ...@@ -295,7 +295,7 @@ class test_diag(unittest.TestCase):
assert (f(m) == m.shape).all() assert (f(m) == m.shape).all()
def test_alloc_diag_grad(self): def test_alloc_diag_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = rng.rand(5) x = rng.rand(5)
tensor.verify_grad(alloc_diag, [x], rng=rng) tensor.verify_grad(alloc_diag, [x], rng=rng)
...@@ -322,7 +322,7 @@ class test_diag(unittest.TestCase): ...@@ -322,7 +322,7 @@ class test_diag(unittest.TestCase):
# not testing the view=True case since it is not used anywhere. # not testing the view=True case since it is not used anywhere.
def test_extract_diag(self): def test_extract_diag(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
m = rng.rand(2, 3).astype(self.floatX) m = rng.rand(2, 3).astype(self.floatX)
x = self.shared(m) x = self.shared(m)
g = extract_diag(x) g = extract_diag(x)
...@@ -334,7 +334,7 @@ class test_diag(unittest.TestCase): ...@@ -334,7 +334,7 @@ class test_diag(unittest.TestCase):
for shp in [(2, 3), (3, 2), (3, 3), (1, 1), (0, 0)]: for shp in [(2, 3), (3, 2), (3, 3), (1, 1), (0, 0)]:
m = rng.rand(*shp).astype(self.floatX) m = rng.rand(*shp).astype(self.floatX)
x.set_value(m) x.set_value(m)
v = numpy.diag(m) v = np.diag(m)
r = f() r = f()
# The right diagonal is extracted # The right diagonal is extracted
assert (r == v).all() assert (r == v).all()
...@@ -360,13 +360,13 @@ class test_diag(unittest.TestCase): ...@@ -360,13 +360,13 @@ class test_diag(unittest.TestCase):
assert f() == min(shp) assert f() == min(shp)
def test_extract_diag_grad(self): def test_extract_diag_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = rng.rand(5, 4).astype(self.floatX) x = rng.rand(5, 4).astype(self.floatX)
tensor.verify_grad(extract_diag, [x], rng=rng) tensor.verify_grad(extract_diag, [x], rng=rng)
@attr('slow') @attr('slow')
def test_extract_diag_empty(self): def test_extract_diag_empty(self):
c = self.shared(numpy.array([[], []], self.floatX)) c = self.shared(np.array([[], []], self.floatX))
f = theano.function([], extract_diag(c), mode=self.mode) f = theano.function([], extract_diag(c), mode=self.mode)
assert [isinstance(node.inputs[0].type, self.type) assert [isinstance(node.inputs[0].type, self.type)
...@@ -375,14 +375,14 @@ class test_diag(unittest.TestCase): ...@@ -375,14 +375,14 @@ class test_diag(unittest.TestCase):
def test_trace(): def test_trace():
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = theano.tensor.matrix() x = theano.tensor.matrix()
g = trace(x) g = trace(x)
f = theano.function([x], g) f = theano.function([x], g)
for shp in [(2, 3), (3, 2), (3, 3)]: for shp in [(2, 3), (3, 2), (3, 3)]:
m = rng.rand(*shp).astype(config.floatX) m = rng.rand(*shp).astype(config.floatX)
v = numpy.trace(m) v = np.trace(m)
assert v == f(m) assert v == f(m)
xx = theano.tensor.vector() xx = theano.tensor.vector()
...@@ -401,9 +401,9 @@ class test_Eig(utt.InferShapeTester): ...@@ -401,9 +401,9 @@ class test_Eig(utt.InferShapeTester):
def setUp(self): def setUp(self):
super(test_Eig, self).setUp() super(test_Eig, self).setUp()
self.rng = numpy.random.RandomState(utt.fetch_seed()) self.rng = np.random.RandomState(utt.fetch_seed())
self.A = theano.tensor.matrix(dtype=self.dtype) self.A = theano.tensor.matrix(dtype=self.dtype)
self.X = numpy.asarray(self.rng.rand(5, 5), self.X = np.asarray(self.rng.rand(5, 5),
dtype=self.dtype) dtype=self.dtype)
self.S = self.X.dot(self.X.T) self.S = self.X.dot(self.X.T)
...@@ -423,7 +423,7 @@ class test_Eig(utt.InferShapeTester): ...@@ -423,7 +423,7 @@ class test_Eig(utt.InferShapeTester):
[[1.0], [[1.0]]]) [[1.0], [[1.0]]])
x = [[0, 1], [1, 0]] x = [[0, 1], [1, 0]]
w, v = [e.eval({A: x}) for e in self.op(A)] w, v = [e.eval({A: x}) for e in self.op(A)]
assert_array_almost_equal(numpy.dot(x, v), w * v) assert_array_almost_equal(np.dot(x, v), w * v)
class test_Eigh(test_Eig): class test_Eigh(test_Eig):
...@@ -435,8 +435,8 @@ class test_Eigh(test_Eig): ...@@ -435,8 +435,8 @@ class test_Eigh(test_Eig):
wu, vu = [out.eval({a: S}) for out in self.op(a, 'U')] wu, vu = [out.eval({a: S}) for out in self.op(a, 'U')]
wl, vl = [out.eval({a: S}) for out in self.op(a, 'L')] wl, vl = [out.eval({a: S}) for out in self.op(a, 'L')]
assert_array_almost_equal(wu, wl) assert_array_almost_equal(wu, wl)
assert_array_almost_equal(vu * numpy.sign(vu[0, :]), assert_array_almost_equal(vu * np.sign(vu[0, :]),
vl * numpy.sign(vl[0, :])) vl * np.sign(vl[0, :]))
def test_grad(self): def test_grad(self):
X = self.X X = self.X
...@@ -466,12 +466,12 @@ class T_lstsq(unittest.TestCase): ...@@ -466,12 +466,12 @@ class T_lstsq(unittest.TestCase):
z = tensor.lscalar() z = tensor.lscalar()
b = theano.tensor.nlinalg.lstsq()(x, y, z) b = theano.tensor.nlinalg.lstsq()(x, y, z)
f = function([x, y, z], b) f = function([x, y, z], b)
TestMatrix1 = numpy.asarray([[2, 1], [3, 4]]) TestMatrix1 = np.asarray([[2, 1], [3, 4]])
TestMatrix2 = numpy.asarray([[17, 20], [43, 50]]) TestMatrix2 = np.asarray([[17, 20], [43, 50]])
TestScalar = numpy.asarray(1) TestScalar = np.asarray(1)
f = function([x, y, z], b) f = function([x, y, z], b)
m = f(TestMatrix1, TestMatrix2, TestScalar) m = f(TestMatrix1, TestMatrix2, TestScalar)
self.assertTrue(numpy.allclose(TestMatrix2, numpy.dot(TestMatrix1, m[0]))) self.assertTrue(np.allclose(TestMatrix2, np.dot(TestMatrix1, m[0])))
def test_wrong_coefficient_matrix(self): def test_wrong_coefficient_matrix(self):
x = tensor.vector() x = tensor.vector()
...@@ -479,7 +479,7 @@ class T_lstsq(unittest.TestCase): ...@@ -479,7 +479,7 @@ class T_lstsq(unittest.TestCase):
z = tensor.scalar() z = tensor.scalar()
b = theano.tensor.nlinalg.lstsq()(x, y, z) b = theano.tensor.nlinalg.lstsq()(x, y, z)
f = function([x, y, z], b) f = function([x, y, z], b)
self.assertRaises(numpy.linalg.linalg.LinAlgError, f, [2, 1], [2, 1], 1) self.assertRaises(np.linalg.linalg.LinAlgError, f, [2, 1], [2, 1], 1)
def test_wrong_rcond_dimension(self): def test_wrong_rcond_dimension(self):
x = tensor.vector() x = tensor.vector()
...@@ -487,24 +487,24 @@ class T_lstsq(unittest.TestCase): ...@@ -487,24 +487,24 @@ class T_lstsq(unittest.TestCase):
z = tensor.vector() z = tensor.vector()
b = theano.tensor.nlinalg.lstsq()(x, y, z) b = theano.tensor.nlinalg.lstsq()(x, y, z)
f = function([x, y, z], b) f = function([x, y, z], b)
self.assertRaises(numpy.linalg.LinAlgError, f, [2, 1], [2, 1], [2, 1]) self.assertRaises(np.linalg.LinAlgError, f, [2, 1], [2, 1], [2, 1])
class Matrix_power(unittest.TestCase): class Matrix_power(unittest.TestCase):
def test_numpy_compare(self): def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX) A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3) Q = matrix_power(A, 3)
fn = function([A], [Q]) fn = function([A], [Q])
a = rng.rand(4, 4).astype(theano.config.floatX) a = rng.rand(4, 4).astype(theano.config.floatX)
n_p = numpy.linalg.matrix_power(a, 3) n_p = np.linalg.matrix_power(a, 3)
t_p = fn(a) t_p = fn(a)
assert numpy.allclose(n_p, t_p) assert np.allclose(n_p, t_p)
def test_non_square_matrix(self): def test_non_square_matrix(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = tensor.matrix("A", dtype=theano.config.floatX) A = tensor.matrix("A", dtype=theano.config.floatX)
Q = matrix_power(A, 3) Q = matrix_power(A, 3)
f = function([A], [Q]) f = function([A], [Q])
...@@ -524,10 +524,10 @@ class T_NormTests(unittest.TestCase): ...@@ -524,10 +524,10 @@ class T_NormTests(unittest.TestCase):
self.assertRaises(ValueError, norm, 3, None) self.assertRaises(ValueError, norm, 3, None)
def test_tensor_input(self): def test_tensor_input(self):
self.assertRaises(NotImplementedError, norm, numpy.random.rand(3, 4, 5), None) self.assertRaises(NotImplementedError, norm, np.random.rand(3, 4, 5), None)
def test_numpy_compare(self): def test_numpy_compare(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
M = tensor.matrix("A", dtype=theano.config.floatX) M = tensor.matrix("A", dtype=theano.config.floatX)
V = tensor.vector("V", dtype=theano.config.floatX) V = tensor.vector("V", dtype=theano.config.floatX)
...@@ -543,7 +543,7 @@ class T_NormTests(unittest.TestCase): ...@@ -543,7 +543,7 @@ class T_NormTests(unittest.TestCase):
for i in range(0, 14): for i in range(0, 14):
f = function([A[1][i]], norm(A[1][i], A[0][i])) f = function([A[1][i]], norm(A[1][i], A[0][i]))
t_n = f(A[2][i]) t_n = f(A[2][i])
n_n = numpy.linalg.norm(A[2][i], A[3][i]) n_n = np.linalg.norm(A[2][i], A[3][i])
assert _allclose(n_n, t_n) assert _allclose(n_n, t_n)
...@@ -552,9 +552,9 @@ class test_TensorInv(utt.InferShapeTester): ...@@ -552,9 +552,9 @@ class test_TensorInv(utt.InferShapeTester):
super(test_TensorInv, self).setUp() super(test_TensorInv, self).setUp()
self.A = tensor.tensor4("A", dtype=theano.config.floatX) self.A = tensor.tensor4("A", dtype=theano.config.floatX)
self.B = tensor.tensor3("B", dtype=theano.config.floatX) self.B = tensor.tensor3("B", dtype=theano.config.floatX)
self.a = numpy.random.rand(4, 6, 8, 3).astype(theano.config.floatX) self.a = np.random.rand(4, 6, 8, 3).astype(theano.config.floatX)
self.b = numpy.random.rand(2, 15, 30).astype(theano.config.floatX) self.b = np.random.rand(2, 15, 30).astype(theano.config.floatX)
self.b1 = numpy.random.rand(30, 2, 15).astype(theano.config.floatX) # for ind=1 since we need prod(b1.shape[:ind]) == prod(b1.shape[ind:]) self.b1 = np.random.rand(30, 2, 15).astype(theano.config.floatX) # for ind=1 since we need prod(b1.shape[:ind]) == prod(b1.shape[ind:])
def test_infer_shape(self): def test_infer_shape(self):
A = self.A A = self.A
...@@ -567,7 +567,7 @@ class test_TensorInv(utt.InferShapeTester): ...@@ -567,7 +567,7 @@ class test_TensorInv(utt.InferShapeTester):
def test_eval(self): def test_eval(self):
A = self.A A = self.A
Ai = tensorinv(A) Ai = tensorinv(A)
n_ainv = numpy.linalg.tensorinv(self.a) n_ainv = np.linalg.tensorinv(self.a)
tf_a = function([A], [Ai]) tf_a = function([A], [Ai])
t_ainv = tf_a(self.a) t_ainv = tf_a(self.a)
assert _allclose(n_ainv, t_ainv) assert _allclose(n_ainv, t_ainv)
...@@ -575,8 +575,8 @@ class test_TensorInv(utt.InferShapeTester): ...@@ -575,8 +575,8 @@ class test_TensorInv(utt.InferShapeTester):
B = self.B B = self.B
Bi = tensorinv(B) Bi = tensorinv(B)
Bi1 = tensorinv(B, ind=1) Bi1 = tensorinv(B, ind=1)
n_binv = numpy.linalg.tensorinv(self.b) n_binv = np.linalg.tensorinv(self.b)
n_binv1 = numpy.linalg.tensorinv(self.b1, ind=1) n_binv1 = np.linalg.tensorinv(self.b1, ind=1)
tf_b = function([B], [Bi]) tf_b = function([B], [Bi])
tf_b1 = function([B], [Bi1]) tf_b1 = function([B], [Bi1])
t_binv = tf_b(self.b) t_binv = tf_b(self.b)
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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 theano import theano
from theano import function, config from theano import function, config
...@@ -28,7 +28,7 @@ class T_max_and_argmax(unittest.TestCase): ...@@ -28,7 +28,7 @@ class T_max_and_argmax(unittest.TestCase):
'canonicalize', 'fast_run') 'canonicalize', 'fast_run')
for axis in [0, 1, -1]: for axis in [0, 1, -1]:
data = numpy.asarray(numpy.random.rand(2, 3), dtype=config.floatX) data = np.asarray(np.random.rand(2, 3), dtype=config.floatX)
n = tensor.matrix() n = tensor.matrix()
f = function([n], tensor.max_and_argmax(n, axis)[0], mode=mode) f = function([n], tensor.max_and_argmax(n, axis)[0], mode=mode)
...@@ -49,7 +49,7 @@ class T_min_max(unittest.TestCase): ...@@ -49,7 +49,7 @@ class T_min_max(unittest.TestCase):
'canonicalize', 'fast_run') 'canonicalize', 'fast_run')
def test_optimization_max(self): def test_optimization_max(self):
data = numpy.asarray(numpy.random.rand(2, 3), dtype=config.floatX) data = np.asarray(np.random.rand(2, 3), dtype=config.floatX)
n = tensor.matrix() n = tensor.matrix()
for axis in [0, 1, -1]: for axis in [0, 1, -1]:
...@@ -82,7 +82,7 @@ class T_min_max(unittest.TestCase): ...@@ -82,7 +82,7 @@ class T_min_max(unittest.TestCase):
f(data) f(data)
def test_optimization_min(self): def test_optimization_min(self):
data = numpy.asarray(numpy.random.rand(2, 3), dtype=config.floatX) data = np.asarray(np.random.rand(2, 3), dtype=config.floatX)
n = tensor.matrix() n = tensor.matrix()
for axis in [0, 1, -1]: for axis in [0, 1, -1]:
...@@ -206,4 +206,4 @@ def test_local_dimshuffle_subtensor(): ...@@ -206,4 +206,4 @@ def test_local_dimshuffle_subtensor():
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert any([not isinstance(x, DimShuffle) for x in topo]) assert any([not isinstance(x, DimShuffle) for x in topo])
assert f(numpy.random.rand(5, 1, 4, 1), 2).shape == (4,) assert f(np.random.rand(5, 1, 4, 1), 2).shape == (4,)
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import pickle import pickle
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
...@@ -19,7 +19,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -19,7 +19,7 @@ class T_random_function(utt.InferShapeTester):
utt.seed_rng() utt.seed_rng()
def test_basic_usage(self): def test_basic_usage(self):
rf = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector) rf = RandomFunction(np.random.RandomState.uniform, tensor.dvector)
assert not rf.inplace assert not rf.inplace
assert getattr(rf, 'destroy_map', {}) == {} assert getattr(rf, 'destroy_map', {}) == {}
...@@ -33,23 +33,23 @@ class T_random_function(utt.InferShapeTester): ...@@ -33,23 +33,23 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R], out) f = compile.function([rng_R], out)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
f_0 = f(rng_state0) f_0 = f(rng_state0)
f_1 = f(rng_state0) f_1 = f(rng_state0)
assert numpy.all(f_0 == f_1) assert np.all(f_0 == f_1)
def test_inplace_norun(self): def test_inplace_norun(self):
rf = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector, rf = RandomFunction(np.random.RandomState.uniform, tensor.dvector,
inplace=True) inplace=True)
assert rf.inplace assert rf.inplace
assert getattr(rf, 'destroy_map', {}) != {} assert getattr(rf, 'destroy_map', {}) != {}
def test_args(self): def test_args(self):
"""Test that arguments to RandomFunction are honored""" """Test that arguments to RandomFunction are honored"""
rf2 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector) rf2 = RandomFunction(np.random.RandomState.uniform, tensor.dvector)
rf4 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector, rf4 = RandomFunction(np.random.RandomState.uniform, tensor.dvector,
inplace=True) inplace=True)
rng_R = random_state_type() rng_R = random_state_type()
...@@ -64,7 +64,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -64,7 +64,7 @@ class T_random_function(utt.InferShapeTester):
# be maintained by post_r4 # be maintained by post_r4
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r4, update=post_r4,
mutable=True)], mutable=True)],
[out2, out4, out2_4, out2_4_4], [out2, out4, out2_4, out2_4_4],
...@@ -84,19 +84,19 @@ class T_random_function(utt.InferShapeTester): ...@@ -84,19 +84,19 @@ class T_random_function(utt.InferShapeTester):
# print f2_4_4b # print f2_4_4b
# setting bounds is same as multiplying by 2 # setting bounds is same as multiplying by 2
assert numpy.allclose(f2 * 2, f4), (f2, f4) assert np.allclose(f2 * 2, f4), (f2, f4)
# retrieving from non-inplace generator # retrieving from non-inplace generator
# is same as inplace one for first call # is same as inplace one for first call
assert numpy.allclose(f2_4_4, f4), (f2_4_4, f4) assert np.allclose(f2_4_4, f4), (f2_4_4, f4)
# f4 changes from call to call, that the update has worked # f4 changes from call to call, that the update has worked
assert not numpy.allclose(f4, f4b), (f4, f4b) assert not np.allclose(f4, f4b), (f4, f4b)
def test_inplace_optimization(self): def test_inplace_optimization(self):
"""Test that FAST_RUN includes the random_make_inplace optimization""" """Test that FAST_RUN includes the random_make_inplace optimization"""
#inplace = False #inplace = False
rf2 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector) rf2 = RandomFunction(np.random.RandomState.uniform, tensor.dvector)
rng_R = random_state_type() rng_R = random_state_type()
# If calling RandomFunction directly, all args have to be specified, # If calling RandomFunction directly, all args have to be specified,
...@@ -105,7 +105,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -105,7 +105,7 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r2, update=post_r2,
mutable=True)], mutable=True)],
out2, out2,
...@@ -121,7 +121,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -121,7 +121,7 @@ class T_random_function(utt.InferShapeTester):
val1 = f() val1 = f()
assert id0 == id(f[rng_R]) assert id0 == id(f[rng_R])
assert not numpy.allclose(val0, val1) assert not np.allclose(val0, val1)
def test_no_inplace(self): def test_no_inplace(self):
"""Test that when not running inplace, the RandomState is """Test that when not running inplace, the RandomState is
...@@ -131,10 +131,10 @@ class T_random_function(utt.InferShapeTester): ...@@ -131,10 +131,10 @@ class T_random_function(utt.InferShapeTester):
post_r, out = rf(rng_R, (3,), 0., 1.) post_r, out = rf(rng_R, (3,), 0., 1.)
f = compile.function([rng_R], [post_r, out]) f = compile.function([rng_R], [post_r, out])
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
rng0, val0 = f(rng) rng0, val0 = f(rng)
rng_ = numpy.random.RandomState(utt.fetch_seed()) rng_ = np.random.RandomState(utt.fetch_seed())
# rng should still be in a fresh state # rng should still be in a fresh state
self.assertTrue(rng_R.type.values_eq(rng, rng_)) self.assertTrue(rng_R.type.values_eq(rng, rng_))
# rng0 should be in an updated state # rng0 should be in an updated state
...@@ -178,7 +178,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -178,7 +178,7 @@ class T_random_function(utt.InferShapeTester):
f_ok = compile.function( f_ok = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_out2_4_4, update=post_out2_4_4,
mutable=True)], mutable=True)],
[out4, out1_4, out2_4_4], [out4, out1_4, out2_4_4],
...@@ -188,8 +188,8 @@ class T_random_function(utt.InferShapeTester): ...@@ -188,8 +188,8 @@ class T_random_function(utt.InferShapeTester):
o4, o1_4, o2_4_4 = f_ok() o4, o1_4, o2_4_4 = f_ok()
# Check the sanity of the answers # Check the sanity of the answers
self.assertTrue(numpy.allclose(o4, o1_4)) self.assertTrue(np.allclose(o4, o1_4))
self.assertTrue(numpy.allclose(o4, o2_4_4[0])) self.assertTrue(np.allclose(o4, o2_4_4[0]))
def test_random_function_noshape_args(self): def test_random_function_noshape_args(self):
'''Test if random_function helper works with args but without shape''' '''Test if random_function helper works with args but without shape'''
...@@ -199,7 +199,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -199,7 +199,7 @@ class T_random_function(utt.InferShapeTester):
post_out, out = uniform(rng_R, size=None, ndim=2) post_out, out = uniform(rng_R, size=None, ndim=2)
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_out, update=post_out,
mutable=True)], mutable=True)],
[out], [out],
...@@ -219,7 +219,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -219,7 +219,7 @@ class T_random_function(utt.InferShapeTester):
[low, [low,
high, high,
compile.In(rng_R, compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_out2, update=post_out2,
mutable=True)], mutable=True)],
[out2], [out2],
...@@ -242,7 +242,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -242,7 +242,7 @@ class T_random_function(utt.InferShapeTester):
# If using numpy's uniform distribution, ndim_added should be 0, # If using numpy's uniform distribution, ndim_added should be 0,
# because the shape provided as argument is the output shape. # because the shape provided as argument is the output shape.
# Specifying a different ndim_added will change the Op's output ndim, # Specifying a different ndim_added will change the Op's output ndim,
# so numpy.uniform will produce a result of incorrect shape, # so np.uniform will produce a result of incorrect shape,
# and a ValueError should be raised. # and a ValueError should be raised.
def ndim_added_deco(ndim_added): def ndim_added_deco(ndim_added):
def randomfunction(random_state, size=(), low=0.0, high=0.0, def randomfunction(random_state, size=(), low=0.0, high=0.0,
...@@ -282,27 +282,27 @@ class T_random_function(utt.InferShapeTester): ...@@ -282,27 +282,27 @@ class T_random_function(utt.InferShapeTester):
f11 = compile.function( f11 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=p_uni11, mutable=True)], update=p_uni11, mutable=True)],
[uni11], accept_inplace=True) [uni11], accept_inplace=True)
f12 = compile.function( f12 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=p_uni12, mutable=True)], update=p_uni12, mutable=True)],
[uni12], accept_inplace=True) [uni12], accept_inplace=True)
fm11 = compile.function( fm11 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=p_unim11, mutable=True)], update=p_unim11, mutable=True)],
[unim11], accept_inplace=True) [unim11], accept_inplace=True)
fm12 = compile.function( fm12 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=p_unim12, mutable=True)], update=p_unim12, mutable=True)],
[unim12], accept_inplace=True) [unim12], accept_inplace=True)
f0 = compile.function( f0 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=p_uni02, mutable=True)], update=p_uni02, mutable=True)],
[uni01, uni02], accept_inplace=True) [uni01, uni02], accept_inplace=True)
self.assertRaises(ValueError, f11) self.assertRaises(ValueError, f11)
...@@ -310,7 +310,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -310,7 +310,7 @@ class T_random_function(utt.InferShapeTester):
self.assertRaises(ValueError, fm11) self.assertRaises(ValueError, fm11)
self.assertRaises(ValueError, fm12) self.assertRaises(ValueError, fm12)
u01, u02 = f0() u01, u02 = f0()
self.assertTrue(numpy.allclose(u01, u02[0])) self.assertTrue(np.allclose(u01, u02[0]))
def test_uniform(self): def test_uniform(self):
"""Test that raw_random.uniform generates the same results as numpy.""" """Test that raw_random.uniform generates the same results as numpy."""
...@@ -321,17 +321,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -321,17 +321,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.uniform(-2.0, 2.0, size=(4,)) numpy_val0 = numpy_rng.uniform(-2.0, 2.0, size=(4,))
numpy_val1 = numpy_rng.uniform(-2.0, 2.0, size=(4,)) numpy_val1 = numpy_rng.uniform(-2.0, 2.0, size=(4,))
self.assertTrue(numpy.allclose(val0, numpy_val0)) self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(numpy.allclose(val1, numpy_val1)) self.assertTrue(np.allclose(val1, numpy_val1))
def test_binomial(self): def test_binomial(self):
"""Test that raw_random.binomial generates the same results """Test that raw_random.binomial generates the same results
...@@ -344,17 +344,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -344,17 +344,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[bin], accept_inplace=True) [bin], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.binomial(5, 0.8, size=(7, 12)) numpy_val0 = numpy_rng.binomial(5, 0.8, size=(7, 12))
numpy_val1 = numpy_rng.binomial(5, 0.8, size=(7, 12)) numpy_val1 = numpy_rng.binomial(5, 0.8, size=(7, 12))
self.assertTrue(numpy.all(val0 == numpy_val0)) self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1)) self.assertTrue(np.all(val1 == numpy_val1))
def test_normal(self): def test_normal(self):
"""Test that raw_random.normal generates the same results as numpy.""" """Test that raw_random.normal generates the same results as numpy."""
...@@ -365,17 +365,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -365,17 +365,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.normal(4.0, 2.0, size=(2, 3)) numpy_val0 = numpy_rng.normal(4.0, 2.0, size=(2, 3))
numpy_val1 = numpy_rng.normal(4.0, 2.0, size=(2, 3)) numpy_val1 = numpy_rng.normal(4.0, 2.0, size=(2, 3))
self.assertTrue(numpy.allclose(val0, numpy_val0)) self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(numpy.allclose(val1, numpy_val1)) self.assertTrue(np.allclose(val1, numpy_val1))
def test_random_integers(self): def test_random_integers(self):
# Test that raw_random.random_integers generates the same # Test that raw_random.random_integers generates the same
...@@ -390,17 +390,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -390,17 +390,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.randint(-3, 17, size=(11, 8)) numpy_val0 = numpy_rng.randint(-3, 17, size=(11, 8))
numpy_val1 = numpy_rng.randint(-3, 17, size=(11, 8)) numpy_val1 = numpy_rng.randint(-3, 17, size=(11, 8))
self.assertTrue(numpy.allclose(val0, numpy_val0)) self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(numpy.allclose(val1, numpy_val1)) self.assertTrue(np.allclose(val1, numpy_val1))
def test_permutation_helper(self): def test_permutation_helper(self):
"""Test that raw_random.permutation_helper generates the same """Test that raw_random.permutation_helper generates the same
...@@ -418,21 +418,21 @@ class T_random_function(utt.InferShapeTester): ...@@ -418,21 +418,21 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
# numpy_rng.permutation outputs one vector at a time, # numpy_rng.permutation outputs one vector at a time,
# so we call it iteratively to generate all the samples. # so we call it iteratively to generate all the samples.
numpy_val0 = numpy.asarray([numpy_rng.permutation(8) numpy_val0 = np.asarray([numpy_rng.permutation(8)
for i in range(7)]) for i in range(7)])
numpy_val1 = numpy.asarray([numpy_rng.permutation(8) numpy_val1 = np.asarray([numpy_rng.permutation(8)
for i in range(7)]) for i in range(7)])
self.assertTrue(numpy.all(val0 == numpy_val0)) self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1)) self.assertTrue(np.all(val1 == numpy_val1))
# This call lacks "ndim_added=1", so ndim_added defaults to 0. # This call lacks "ndim_added=1", so ndim_added defaults to 0.
# A ValueError should be raised. # A ValueError should be raised.
...@@ -440,7 +440,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -440,7 +440,7 @@ class T_random_function(utt.InferShapeTester):
post_r0, out0 = rf0(rng_R, (7,), 8) post_r0, out0 = rf0(rng_R, (7,), 8)
f0 = compile.function( f0 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r0, mutable=True)], update=post_r0, mutable=True)],
[out0], accept_inplace=True) [out0], accept_inplace=True)
self.assertRaises(ValueError, f0) self.assertRaises(ValueError, f0)
...@@ -451,11 +451,11 @@ class T_random_function(utt.InferShapeTester): ...@@ -451,11 +451,11 @@ class T_random_function(utt.InferShapeTester):
post_r2, out2 = rf2(rng_R, (7,), 8) post_r2, out2 = rf2(rng_R, (7,), 8)
f2 = compile.function( f2 = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r2, mutable=True)], update=post_r2, mutable=True)],
[out2], accept_inplace=True) [out2], accept_inplace=True)
self.assertRaises(ValueError, f2) self.assertRaises(ValueError, f2)
def test_choice(self): def test_choice(self):
"""Test that raw_random.choice generates the same """Test that raw_random.choice generates the same
results as numpy.""" results as numpy."""
...@@ -467,17 +467,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -467,17 +467,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.choice(10, (11, 8), True, None) numpy_val0 = numpy_rng.choice(10, (11, 8), True, None)
numpy_val1 = numpy_rng.choice(10, (11, 8), True, None) numpy_val1 = numpy_rng.choice(10, (11, 8), True, None)
self.assertTrue(numpy.allclose(val0, numpy_val0)) self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(numpy.allclose(val1, numpy_val1)) self.assertTrue(np.allclose(val1, numpy_val1))
def test_poisson(self): def test_poisson(self):
"""Test that raw_random.poisson generates the same """Test that raw_random.poisson generates the same
...@@ -490,17 +490,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -490,17 +490,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy_rng.poisson(5, size=(11, 8)) numpy_val0 = numpy_rng.poisson(5, size=(11, 8))
numpy_val1 = numpy_rng.poisson(5, size=(11, 8)) numpy_val1 = numpy_rng.poisson(5, size=(11, 8))
self.assertTrue(numpy.allclose(val0, numpy_val0)) self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(numpy.allclose(val1, numpy_val1)) self.assertTrue(np.allclose(val1, numpy_val1))
def test_permutation(self): def test_permutation(self):
"""Test that raw_random.permutation generates the same """Test that raw_random.permutation generates the same
...@@ -509,33 +509,33 @@ class T_random_function(utt.InferShapeTester): ...@@ -509,33 +509,33 @@ class T_random_function(utt.InferShapeTester):
post_r, out = permutation(rng_R, size=(9,), n=6) post_r, out = permutation(rng_R, size=(9,), n=6)
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
# numpy_rng.permutation outputs one vector at a time, # numpy_rng.permutation outputs one vector at a time,
# so we call it iteratively to generate all the samples. # so we call it iteratively to generate all the samples.
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = numpy.asarray([numpy_rng.permutation(6) numpy_val0 = np.asarray([numpy_rng.permutation(6)
for i in range(9)]) for i in range(9)])
numpy_val1 = numpy.asarray([numpy_rng.permutation(6) numpy_val1 = np.asarray([numpy_rng.permutation(6)
for i in range(9)]) for i in range(9)])
self.assertTrue(numpy.all(val0 == numpy_val0)) self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1)) self.assertTrue(np.all(val1 == numpy_val1))
# Test that we can generate a list: have size=None or (). # Test that we can generate a list: have size=None or ().
for ndim in [1, None]: for ndim in [1, None]:
post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim) post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim)
inp = compile.In(rng_R, inp = compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True) update=post_r, mutable=True)
f = theano.function([inp], out) f = theano.function([inp], out)
o = f() o = f()
assert o.shape == (10,) assert o.shape == (10,)
assert (numpy.sort(o) == numpy.arange(10)).all() assert (np.sort(o) == np.arange(10)).all()
# Wrong number of dimensions asked # Wrong number of dimensions asked
self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2) self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2)
...@@ -548,17 +548,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -548,17 +548,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function( f = compile.function(
[compile.In(rng_R, [compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()), value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)], update=post_r, mutable=True)],
[out], accept_inplace=True) [out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
val0, = f() val0, = f()
val1, = f() val1, = f()
numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3)) numpy_val0 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3)) numpy_val1 = numpy_rng.multinomial(6, [0.2] * 5, (7, 3))
self.assertTrue(numpy.all(val0 == numpy_val0)) self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1)) self.assertTrue(np.all(val1 == numpy_val1))
self.assertTrue(val0.shape == (7, 3, 5)) self.assertTrue(val0.shape == (7, 3, 5))
self.assertTrue(val1.shape == (7, 3, 5)) self.assertTrue(val1.shape == (7, 3, 5))
...@@ -568,7 +568,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -568,7 +568,7 @@ class T_random_function(utt.InferShapeTester):
shape = tensor.lvector() shape = tensor.lvector()
post_r, out = uniform(rng_R, shape, ndim=2) post_r, out = uniform(rng_R, shape, ndim=2)
f = compile.function([rng_R, shape], out) f = compile.function([rng_R, shape], out)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
assert f(rng_state0, [2, 3]).shape == (2, 3) assert f(rng_state0, [2, 3]).shape == (2, 3)
assert f(rng_state0, [4, 8]).shape == (4, 8) assert f(rng_state0, [4, 8]).shape == (4, 8)
...@@ -583,7 +583,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -583,7 +583,7 @@ class T_random_function(utt.InferShapeTester):
shape = (shape0, 3) shape = (shape0, 3)
post_r, u = uniform(rng_R, size=shape, ndim=2) post_r, u = uniform(rng_R, size=shape, ndim=2)
f = compile.function([rng_R, shape0], u) f = compile.function([rng_R, shape0], u)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
assert f(rng_state0, 2).shape == (2, 3) assert f(rng_state0, 2).shape == (2, 3)
assert f(rng_state0, 8).shape == (8, 3) assert f(rng_state0, 8).shape == (8, 3)
...@@ -601,7 +601,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -601,7 +601,7 @@ class T_random_function(utt.InferShapeTester):
post_r, u = uniform(rng_R, size=shape, ndim=2) post_r, u = uniform(rng_R, size=shape, ndim=2)
assert u.broadcastable == (False, True) assert u.broadcastable == (False, True)
f = compile.function([rng_R, shape0], u) f = compile.function([rng_R, shape0], u)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
assert f(rng_state0, 2).shape == (2, 1) assert f(rng_state0, 2).shape == (2, 1)
assert f(rng_state0, 8).shape == (8, 1) assert f(rng_state0, 8).shape == (8, 1)
...@@ -617,25 +617,25 @@ class T_random_function(utt.InferShapeTester): ...@@ -617,25 +617,25 @@ class T_random_function(utt.InferShapeTester):
post_r, out = uniform(rng_R) post_r, out = uniform(rng_R)
f = compile.function([rng_R], [post_r, out], accept_inplace=True) f = compile.function([rng_R], [post_r, out], accept_inplace=True)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0) post0, val0 = f(rng_state0)
post1, val1 = f(post0) post1, val1 = f(post0)
numpy_val0 = numpy.asarray(numpy_rng.uniform(), numpy_val0 = np.asarray(numpy_rng.uniform(),
dtype=theano.config.floatX) dtype=theano.config.floatX)
numpy_val1 = numpy.asarray(numpy_rng.uniform(), numpy_val1 = np.asarray(numpy_rng.uniform(),
dtype=theano.config.floatX) dtype=theano.config.floatX)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
post_r, out = multinomial(rng_R) post_r, out = multinomial(rng_R)
g = compile.function([rng_R], [post_r, out], accept_inplace=True) g = compile.function([rng_R], [post_r, out], accept_inplace=True)
post2, val2 = g(post1) post2, val2 = g(post1)
numpy_val2 = numpy.asarray(numpy_rng.multinomial(n=1, pvals=[.5, .5]), numpy_val2 = np.asarray(numpy_rng.multinomial(n=1, pvals=[.5, .5]),
dtype=theano.config.floatX) dtype=theano.config.floatX)
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_vector_arguments(self): def test_vector_arguments(self):
rng_R = random_state_type() rng_R = random_state_type()
...@@ -645,17 +645,17 @@ class T_random_function(utt.InferShapeTester): ...@@ -645,17 +645,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, low], [post_r, out], accept_inplace=True) f = compile.function([rng_R, low], [post_r, out], accept_inplace=True)
def as_floatX(thing): def as_floatX(thing):
return numpy.asarray(thing, dtype=theano.config.floatX) return np.asarray(thing, dtype=theano.config.floatX)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0, [-5, .5, 0, 1]) post0, val0 = f(rng_state0, [-5, .5, 0, 1])
post1, val1 = f(post0, as_floatX([.9])) post1, val1 = f(post0, as_floatX([.9]))
numpy_val0 = as_floatX(numpy_rng.uniform(low=[-5, .5, 0, 1], high=1)) numpy_val0 = as_floatX(numpy_rng.uniform(low=[-5, .5, 0, 1], high=1))
numpy_val1 = as_floatX(numpy_rng.uniform(low=as_floatX([.9]), high=1)) numpy_val1 = as_floatX(numpy_rng.uniform(low=as_floatX([.9]), high=1))
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
high = tensor.vector() high = tensor.vector()
post_rb, outb = uniform(rng_R, low=low, high=high) post_rb, outb = uniform(rng_R, low=low, high=high)
...@@ -667,8 +667,8 @@ class T_random_function(utt.InferShapeTester): ...@@ -667,8 +667,8 @@ class T_random_function(utt.InferShapeTester):
post1b, val1b = fb(post0b, [-4.], [-1]) post1b, val1b = fb(post0b, [-4.], [-1])
numpy_val0b = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0])) numpy_val0b = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
numpy_val1b = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1])) numpy_val1b = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
assert numpy.all(val0b == numpy_val0b) assert np.all(val0b == numpy_val0b)
assert numpy.all(val1b == numpy_val1b) assert np.all(val1b == numpy_val1b)
self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1, 0, 1]) self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1, 0, 1])
# TODO: do we want that? # TODO: do we want that?
#self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1]) #self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1])
...@@ -681,8 +681,8 @@ class T_random_function(utt.InferShapeTester): ...@@ -681,8 +681,8 @@ class T_random_function(utt.InferShapeTester):
post1c, val1c = fc(post0c, [-4.], [-1], [1]) post1c, val1c = fc(post0c, [-4.], [-1], [1])
numpy_val0c = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0])) numpy_val0c = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
numpy_val1c = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1])) numpy_val1c = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
assert numpy.all(val0c == numpy_val0c) assert np.all(val0c == numpy_val0c)
assert numpy.all(val1c == numpy_val1c) assert np.all(val1c == numpy_val1c)
self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1]) self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1])
self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1, 2]) self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [1, 2])
self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [2, 1]) self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [2, 1])
...@@ -699,8 +699,8 @@ class T_random_function(utt.InferShapeTester): ...@@ -699,8 +699,8 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, low, high], [post_r, out], f = compile.function([rng_R, low, high], [post_r, out],
accept_inplace=True) accept_inplace=True)
rng_state0 = numpy.random.RandomState(utt.fetch_seed()) rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0, [-5, .5, 0, 1], [[1.]]) post0, val0 = f(rng_state0, [-5, .5, 0, 1], [[1.]])
post1, val1 = f(post0, [.9], [[1.], [1.1], [1.5]]) post1, val1 = f(post0, [.9], [[1.], [1.1], [1.5]])
post2, val2 = f(post1, [-5, .5, 0, 1], [[1.], [1.1], [1.5]]) post2, val2 = f(post1, [-5, .5, 0, 1], [[1.], [1.1], [1.5]])
...@@ -710,9 +710,9 @@ class T_random_function(utt.InferShapeTester): ...@@ -710,9 +710,9 @@ class T_random_function(utt.InferShapeTester):
numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1], numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1],
high=[[1.], [1.1], [1.5]]) high=[[1.], [1.1], [1.5]])
assert numpy.all(val0 == numpy_val0), (val0, numpy_val0) assert np.all(val0 == numpy_val0), (val0, numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_uniform_vector(self): def test_uniform_vector(self):
rng_R = random_state_type() rng_R = random_state_type()
...@@ -724,22 +724,22 @@ class T_random_function(utt.InferShapeTester): ...@@ -724,22 +724,22 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True) accept_inplace=True)
def as_floatX(thing): def as_floatX(thing):
return numpy.asarray(thing, dtype=theano.config.floatX) return np.asarray(thing, dtype=theano.config.floatX)
low_val = as_floatX([.1, .2, .3]) low_val = as_floatX([.1, .2, .3])
high_val = as_floatX([1.1, 2.2, 3.3]) high_val = as_floatX([1.1, 2.2, 3.3])
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,) # Arguments of size (3,)
rng0, val0 = f(rng, low_val, high_val) rng0, val0 = f(rng, low_val, high_val)
numpy_val0 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val)) numpy_val0 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val))
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
rng1, val1 = f(rng0, low_val[:-1], high_val[:-1]) rng1, val1 = f(rng0, low_val[:-1], high_val[:-1])
numpy_val1 = as_floatX(numpy_rng.uniform(low=low_val[:-1], numpy_val1 = as_floatX(numpy_rng.uniform(low=low_val[:-1],
high=high_val[:-1])) high=high_val[:-1]))
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = compile.function([rng_R, low, high], g = compile.function([rng_R, low, high],
...@@ -748,7 +748,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -748,7 +748,7 @@ class T_random_function(utt.InferShapeTester):
rng2, val2 = g(rng1, low_val, high_val) rng2, val2 = g(rng1, low_val, high_val)
numpy_val2 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val, numpy_val2 = as_floatX(numpy_rng.uniform(low=low_val, high=high_val,
size=(3,))) size=(3,)))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1])
def test_binomial_vector(self): def test_binomial_vector(self):
...@@ -761,19 +761,19 @@ class T_random_function(utt.InferShapeTester): ...@@ -761,19 +761,19 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True) accept_inplace=True)
n_val = [1, 2, 3] n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX) prob_val = np.asarray([.1, .2, .3], dtype=config.floatX)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,) # Arguments of size (3,)
rng0, val0 = f(rng, n_val, prob_val) rng0, val0 = f(rng, n_val, prob_val)
numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val) numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
rng1, val1 = f(rng0, n_val[:-1], prob_val[:-1]) rng1, val1 = f(rng0, n_val[:-1], prob_val[:-1])
numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1]) numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = compile.function([rng_R, n, prob], g = compile.function([rng_R, n, prob],
...@@ -781,7 +781,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -781,7 +781,7 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True) accept_inplace=True)
rng2, val2 = g(rng1, n_val, prob_val) rng2, val2 = g(rng1, n_val, prob_val)
numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,)) numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, n_val[:-1], prob_val[:-1]) self.assertRaises(ValueError, g, rng2, n_val[:-1], prob_val[:-1])
def test_normal_vector(self): def test_normal_vector(self):
...@@ -794,35 +794,35 @@ class T_random_function(utt.InferShapeTester): ...@@ -794,35 +794,35 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True) accept_inplace=True)
def as_floatX(thing): def as_floatX(thing):
return numpy.asarray(thing, dtype=theano.config.floatX) return np.asarray(thing, dtype=theano.config.floatX)
avg_val = [1, 2, 3] avg_val = [1, 2, 3]
std_val = as_floatX([.1, .2, .3]) std_val = as_floatX([.1, .2, .3])
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,) # Arguments of size (3,)
rng0, val0 = f(rng, avg_val, std_val) rng0, val0 = f(rng, avg_val, std_val)
numpy_val0 = as_floatX(numpy_rng.normal(loc=as_floatX(avg_val), numpy_val0 = as_floatX(numpy_rng.normal(loc=as_floatX(avg_val),
scale=as_floatX(std_val))) scale=as_floatX(std_val)))
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
rng1, val1 = f(rng0, avg_val[:-1], std_val[:-1]) rng1, val1 = f(rng0, avg_val[:-1], std_val[:-1])
numpy_val1 = numpy.asarray(numpy_rng.normal(loc=avg_val[:-1], numpy_val1 = np.asarray(numpy_rng.normal(loc=avg_val[:-1],
scale=std_val[:-1]), scale=std_val[:-1]),
dtype=theano.config.floatX) dtype=theano.config.floatX)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = compile.function([rng_R, avg, std], g = compile.function([rng_R, avg, std],
normal(rng_R, avg=avg, std=std, size=(3,)), normal(rng_R, avg=avg, std=std, size=(3,)),
accept_inplace=True) accept_inplace=True)
rng2, val2 = g(rng1, avg_val, std_val) rng2, val2 = g(rng1, avg_val, std_val)
numpy_val2 = numpy.asarray(numpy_rng.normal(loc=avg_val, scale=std_val, numpy_val2 = np.asarray(numpy_rng.normal(loc=avg_val, scale=std_val,
size=(3,)), size=(3,)),
dtype=theano.config.floatX) dtype=theano.config.floatX)
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, avg_val[:-1], std_val[:-1]) self.assertRaises(ValueError, g, rng2, avg_val[:-1], std_val[:-1])
def test_random_integers_vector(self): def test_random_integers_vector(self):
...@@ -836,29 +836,29 @@ class T_random_function(utt.InferShapeTester): ...@@ -836,29 +836,29 @@ class T_random_function(utt.InferShapeTester):
low_val = [100, 200, 300] low_val = [100, 200, 300]
high_val = [110, 220, 330] high_val = [110, 220, 330]
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,) # Arguments of size (3,)
rng0, val0 = f(rng, low_val, high_val) rng0, val0 = f(rng, low_val, high_val)
numpy_val0 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val0 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
rng1, val1 = f(rng0, low_val[:-1], high_val[:-1]) rng1, val1 = f(rng0, low_val[:-1], high_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val1 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val[:-1], high_val[:-1])]) for lv, hv in zip(low_val[:-1], high_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = compile.function([rng_R, low, high], g = compile.function([rng_R, low, high],
random_integers(rng_R, low=low, high=high, size=(3,)), random_integers(rng_R, low=low, high=high, size=(3,)),
accept_inplace=True) accept_inplace=True)
rng2, val2 = g(rng1, low_val, high_val) rng2, val2 = g(rng1, low_val, high_val)
numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val2 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1])
# Vectorized permutation don't make sense: the only parameter, n, # Vectorized permutation don't make sense: the only parameter, n,
...@@ -875,30 +875,30 @@ class T_random_function(utt.InferShapeTester): ...@@ -875,30 +875,30 @@ class T_random_function(utt.InferShapeTester):
n_val = [1, 2, 3] n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]] pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX) pvals_val = np.asarray(pvals_val, dtype=config.floatX)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,) # Arguments of size (3,)
rng0, val0 = f(rng, n_val, pvals_val) rng0, val0 = f(rng, n_val, pvals_val)
numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val0 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
rng1, val1 = f(rng0, n_val[:-1], pvals_val[:-1]) rng1, val1 = f(rng0, n_val[:-1], pvals_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val1 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val[:-1], pvals_val[:-1])]) for nv, pv in zip(n_val[:-1], pvals_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = compile.function([rng_R, n, pvals], g = compile.function([rng_R, n, pvals],
multinomial(rng_R, n=n, pvals=pvals, size=(3,)), multinomial(rng_R, n=n, pvals=pvals, size=(3,)),
accept_inplace=True) accept_inplace=True)
rng2, val2 = g(rng1, n_val, pvals_val) rng2, val2 = g(rng1, n_val, pvals_val)
numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val2 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, n_val[:-1], pvals_val[:-1]) self.assertRaises(ValueError, g, rng2, n_val[:-1], pvals_val[:-1])
def test_multinomial_tensor3_a(self): def test_multinomial_tensor3_a(self):
...@@ -914,15 +914,15 @@ class T_random_function(utt.InferShapeTester): ...@@ -914,15 +914,15 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, pvals], [post_r, out], f = compile.function([rng_R, pvals], [post_r, out],
accept_inplace=True) accept_inplace=True)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
pvals_val = numpy.asarray([[[.1, .9], [.2, .8], [.3, .7]]]) pvals_val = np.asarray([[[.1, .9], [.2, .8], [.3, .7]]])
assert pvals_val.shape == (1, 3, 2) assert pvals_val.shape == (1, 3, 2)
new_rng, draw = f(rng, pvals_val) new_rng, draw = f(rng, pvals_val)
assert draw.shape == (1, 3, 2) assert draw.shape == (1, 3, 2)
assert numpy.allclose(draw.sum(axis=2), 9) assert np.allclose(draw.sum(axis=2), 9)
def test_multinomial_tensor3_b(self): def test_multinomial_tensor3_b(self):
# Test the examples given in the multinomial documentation regarding # Test the examples given in the multinomial documentation regarding
...@@ -937,15 +937,15 @@ class T_random_function(utt.InferShapeTester): ...@@ -937,15 +937,15 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, pvals], [post_r, out], f = compile.function([rng_R, pvals], [post_r, out],
accept_inplace=True) accept_inplace=True)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed()) numpy_rng = np.random.RandomState(utt.fetch_seed())
pvals_val = numpy.asarray([[[.1, .9], [.2, .8], [.3, .7]]]) pvals_val = np.asarray([[[.1, .9], [.2, .8], [.3, .7]]])
assert pvals_val.shape == (1, 3, 2) assert pvals_val.shape == (1, 3, 2)
out_rng, draw = f(rng, pvals_val) out_rng, draw = f(rng, pvals_val)
assert draw.shape == (10, 1, 3, 2) assert draw.shape == (10, 1, 3, 2)
assert numpy.allclose(draw.sum(axis=3), 9) assert np.allclose(draw.sum(axis=3), 9)
def test_dtype(self): def test_dtype(self):
rng_R = random_state_type() rng_R = random_state_type()
...@@ -956,13 +956,13 @@ class T_random_function(utt.InferShapeTester): ...@@ -956,13 +956,13 @@ class T_random_function(utt.InferShapeTester):
assert out.dtype == 'int8' assert out.dtype == 'int8'
f = compile.function([rng_R, low, high], [post_r, out]) f = compile.function([rng_R, low, high], [post_r, out])
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
rng0, val0 = f(rng, 0, 9) rng0, val0 = f(rng, 0, 9)
assert val0.dtype == 'int8' assert val0.dtype == 'int8'
rng1, val1 = f(rng0, 255, 257) rng1, val1 = f(rng0, 255, 257)
assert val1.dtype == 'int8' assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1) assert np.all(abs(val1) <= 1)
def test_dtype_normal_uniform_687(self): def test_dtype_normal_uniform_687(self):
# Regression test for #687. # Regression test for #687.
...@@ -978,7 +978,7 @@ class T_random_function(utt.InferShapeTester): ...@@ -978,7 +978,7 @@ class T_random_function(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
rng_R = random_state_type() rng_R = random_state_type()
rng_R_val = numpy.random.RandomState(utt.fetch_seed()) rng_R_val = np.random.RandomState(utt.fetch_seed())
# no shape specified, default args # no shape specified, default args
post_r, out = uniform(rng_R) post_r, out = uniform(rng_R)
......
...@@ -3,7 +3,7 @@ __docformat__ = "restructuredtext en" ...@@ -3,7 +3,7 @@ __docformat__ = "restructuredtext en"
import sys import sys
import unittest import unittest
import numpy import numpy as np
from theano.tensor import raw_random from theano.tensor import raw_random
from theano.tensor.shared_randomstreams import RandomStreams from theano.tensor.shared_randomstreams import RandomStreams
...@@ -27,12 +27,12 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -27,12 +27,12 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u) nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
assert numpy.all(f() != f()) assert np.all(f() != f())
assert numpy.all(g() == g()) assert np.all(g() == g())
assert numpy.all(abs(nearly_zeros()) < 1e-5) assert np.all(abs(nearly_zeros()) < 1e-5)
assert isinstance(rv_u.rng.get_value(borrow=True), assert isinstance(rv_u.rng.get_value(borrow=True),
numpy.random.RandomState) np.random.RandomState)
def test_basics(self): def test_basics(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -44,20 +44,20 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -44,20 +44,20 @@ class T_SharedRandomStreams(unittest.TestCase):
gn_val0 = gn() gn_val0 = gn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
# print fn_val0 # print fn_val0
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0 # print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
print(fn_val0) print(fn_val0)
print(numpy_val0) print(numpy_val0)
print(fn_val1) print(fn_val1)
print(numpy_val1) print(numpy_val1)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_seed_fn(self): def test_seed_fn(self):
random = RandomStreams(234) random = RandomStreams(234)
...@@ -68,16 +68,16 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -68,16 +68,16 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit rng = np.random.RandomState(int(rng_seed)) #int() is for 32bit
# print fn_val0 # print fn_val0
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0 # print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_getitem(self): def test_getitem(self):
...@@ -87,15 +87,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -87,15 +87,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(utt.fetch_seed()) random.seed(utt.fetch_seed())
rng = numpy.random.RandomState() rng = np.random.RandomState()
rng.set_state(random[out.rng].get_state()) # tests getitem rng.set_state(random[out.rng].get_state()) # tests getitem
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_setitem(self): def test_setitem(self):
...@@ -105,15 +105,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -105,15 +105,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(888) random.seed(888)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
random[out.rng] = numpy.random.RandomState(utt.fetch_seed()) random[out.rng] = np.random.RandomState(utt.fetch_seed())
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2)) numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2)) numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_ndim(self): def test_ndim(self):
"""Test that the behaviour of 'ndim' optional parameter""" """Test that the behaviour of 'ndim' optional parameter"""
...@@ -130,7 +130,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -130,7 +130,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = fn() val1 = fn()
val2 = fn2() val2 = fn2()
assert numpy.all(val1 == val2) assert np.all(val1 == val2)
# ndim specified, inconsistent with shape, should raise ValueError # ndim specified, inconsistent with shape, should raise ValueError
random3 = RandomStreams(utt.fetch_seed()) random3 = RandomStreams(utt.fetch_seed())
...@@ -144,13 +144,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -144,13 +144,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.uniform(-1, 1, size=(2, 2)) numpy_val0 = rng.uniform(-1, 1, size=(2, 2))
numpy_val1 = rng.uniform(-1, 1, size=(2, 2)) numpy_val1 = rng.uniform(-1, 1, size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_normal(self): def test_normal(self):
"""Test that RandomStreams.normal generates the same results as numpy""" """Test that RandomStreams.normal generates the same results as numpy"""
...@@ -161,13 +161,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -161,13 +161,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.normal(-1, 2, size=(2, 2)) numpy_val0 = rng.normal(-1, 2, size=(2, 2))
numpy_val1 = rng.normal(-1, 2, size=(2, 2)) numpy_val1 = rng.normal(-1, 2, size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0) assert np.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1) assert np.allclose(fn_val1, numpy_val1)
def test_random_integers(self): def test_random_integers(self):
# Test that RandomStreams.random_integers generates the same # Test that RandomStreams.random_integers generates the same
...@@ -180,14 +180,14 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -180,14 +180,14 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.randint(-5, 6, size=(20, 20)) numpy_val0 = rng.randint(-5, 6, size=(20, 20))
numpy_val1 = rng.randint(-5, 6, size=(20, 20)) numpy_val1 = rng.randint(-5, 6, size=(20, 20))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_choice(self): def test_choice(self):
"""Test that RandomStreams.choice generates the same results as numpy""" """Test that RandomStreams.choice generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
...@@ -196,30 +196,30 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -196,30 +196,30 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.choice(10, (11, 8), True, None) numpy_val0 = rng.choice(10, (11, 8), True, None)
numpy_val1 = rng.choice(10, (11, 8), True, None) numpy_val1 = rng.choice(10, (11, 8), True, None)
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_poisson(self): def test_poisson(self):
"""Test that RandomStreams.poisson generates the same results as numpy""" """Test that RandomStreams.poisson generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
fn = function([], random.poisson(lam=5, size=(11, 8))) fn = function([], random.poisson(lam=5, size=(11, 8)))
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.poisson(lam=5, size=(11, 8)) numpy_val0 = rng.poisson(lam=5, size=(11, 8))
numpy_val1 = rng.poisson(lam=5, size=(11, 8)) numpy_val1 = rng.poisson(lam=5, size=(11, 8))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_permutation(self): def test_permutation(self):
"""Test that RandomStreams.permutation generates the same results as numpy""" """Test that RandomStreams.permutation generates the same results as numpy"""
...@@ -230,15 +230,15 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -230,15 +230,15 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
# rng.permutation outputs one vector at a time, so we iterate. # rng.permutation outputs one vector at a time, so we iterate.
numpy_val0 = numpy.asarray([rng.permutation(10) for i in range(20)]) numpy_val0 = np.asarray([rng.permutation(10) for i in range(20)])
numpy_val1 = numpy.asarray([rng.permutation(10) for i in range(20)]) numpy_val1 = np.asarray([rng.permutation(10) for i in range(20)])
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_multinomial(self): def test_multinomial(self):
"""Test that RandomStreams.multinomial generates the same results as numpy""" """Test that RandomStreams.multinomial generates the same results as numpy"""
...@@ -249,39 +249,39 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -249,39 +249,39 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn() fn_val0 = fn()
fn_val1 = fn() fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.multinomial(1, [0.1]*10, size=(4, 4)) numpy_val0 = rng.multinomial(1, [0.1]*10, size=(4, 4))
numpy_val1 = rng.multinomial(1, [0.1]*10, size=(4, 4)) numpy_val1 = rng.multinomial(1, [0.1]*10, size=(4, 4))
assert numpy.all(fn_val0 == numpy_val0) assert np.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1) assert np.all(fn_val1 == numpy_val1)
def test_shuffle_row_elements(self): def test_shuffle_row_elements(self):
"""Test that RandomStreams.shuffle_row_elements generates the right results""" """Test that RandomStreams.shuffle_row_elements generates the right results"""
# Check over two calls to see if the random state is correctly updated. # Check over two calls to see if the random state is correctly updated.
# On matrices, for each row, the elements of that row should be shuffled. # On matrices, for each row, the elements of that row should be shuffled.
# Note that this differs from numpy.random.shuffle, where all the elements # Note that this differs from np.random.shuffle, where all the elements
# of the matrix are shuffled. # of the matrix are shuffled.
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
m_input = tensor.dmatrix() m_input = tensor.dmatrix()
f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates()) f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates())
# Generate the elements to be shuffled # Generate the elements to be shuffled
val_rng = numpy.random.RandomState(utt.fetch_seed()+42) val_rng = np.random.RandomState(utt.fetch_seed()+42)
in_mval = val_rng.uniform(-2, 2, size=(20, 5)) in_mval = val_rng.uniform(-2, 2, size=(20, 5))
fn_mval0 = f(in_mval) fn_mval0 = f(in_mval)
fn_mval1 = f(in_mval) fn_mval1 = f(in_mval)
print(in_mval[0]) print(in_mval[0])
print(fn_mval0[0]) print(fn_mval0[0])
print(fn_mval1[0]) print(fn_mval1[0])
assert not numpy.all(in_mval == fn_mval0) assert not np.all(in_mval == fn_mval0)
assert not numpy.all(in_mval == fn_mval1) assert not np.all(in_mval == fn_mval1)
assert not numpy.all(fn_mval0 == fn_mval1) assert not np.all(fn_mval0 == fn_mval1)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) rng = np.random.RandomState(int(rng_seed))
numpy_mval0 = in_mval.copy() numpy_mval0 = in_mval.copy()
numpy_mval1 = in_mval.copy() numpy_mval1 = in_mval.copy()
for row in numpy_mval0: for row in numpy_mval0:
...@@ -289,10 +289,10 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -289,10 +289,10 @@ class T_SharedRandomStreams(unittest.TestCase):
for row in numpy_mval1: for row in numpy_mval1:
rng.shuffle(row) rng.shuffle(row)
assert numpy.all(numpy_mval0 == fn_mval0) assert np.all(numpy_mval0 == fn_mval0)
assert numpy.all(numpy_mval1 == fn_mval1) assert np.all(numpy_mval1 == fn_mval1)
# On vectors, the behaviour is the same as numpy.random.shuffle, # On vectors, the behaviour is the same as np.random.shuffle,
# except that it does not work in place, but returns a shuffled vector. # except that it does not work in place, but returns a shuffled vector.
random1 = RandomStreams(utt.fetch_seed()) random1 = RandomStreams(utt.fetch_seed())
v_input = tensor.dvector() v_input = tensor.dvector()
...@@ -301,12 +301,12 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -301,12 +301,12 @@ class T_SharedRandomStreams(unittest.TestCase):
in_vval = val_rng.uniform(-3, 3, size=(12,)) in_vval = val_rng.uniform(-3, 3, size=(12,))
fn_vval = f1(in_vval) fn_vval = f1(in_vval)
numpy_vval = in_vval.copy() numpy_vval = in_vval.copy()
vrng = numpy.random.RandomState(int(rng_seed)) vrng = np.random.RandomState(int(rng_seed))
vrng.shuffle(numpy_vval) vrng.shuffle(numpy_vval)
print(in_vval) print(in_vval)
print(fn_vval) print(fn_vval)
print(numpy_vval) print(numpy_vval)
assert numpy.all(numpy_vval == fn_vval) assert np.all(numpy_vval == fn_vval)
# Trying to shuffle a vector with function that should shuffle # Trying to shuffle a vector with function that should shuffle
# matrices, or vice versa, raises a TypeError # matrices, or vice versa, raises a TypeError
...@@ -320,10 +320,10 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -320,10 +320,10 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_a = function([], out_a) fn_a = function([], out_a)
fn_a_val0 = fn_a() fn_a_val0 = fn_a()
fn_a_val1 = fn_a() fn_a_val1 = fn_a()
assert not numpy.all(fn_a_val0 == fn_a_val1) assert not np.all(fn_a_val0 == fn_a_val1)
nearly_zeros = function([], out_a + out_a - 2 * out_a) nearly_zeros = function([], out_a + out_a - 2 * out_a)
assert numpy.all(abs(nearly_zeros()) < 1e-5) assert np.all(abs(nearly_zeros()) < 1e-5)
# Explicit updates #1 # Explicit updates #1
random_b = RandomStreams(utt.fetch_seed()) random_b = RandomStreams(utt.fetch_seed())
...@@ -331,8 +331,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -331,8 +331,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_b = function([], out_b, updates=random_b.updates()) fn_b = function([], out_b, updates=random_b.updates())
fn_b_val0 = fn_b() fn_b_val0 = fn_b()
fn_b_val1 = fn_b() fn_b_val1 = fn_b()
assert numpy.all(fn_b_val0 == fn_a_val0) assert np.all(fn_b_val0 == fn_a_val0)
assert numpy.all(fn_b_val1 == fn_a_val1) assert np.all(fn_b_val1 == fn_a_val1)
# Explicit updates #2 # Explicit updates #2
random_c = RandomStreams(utt.fetch_seed()) random_c = RandomStreams(utt.fetch_seed())
...@@ -340,8 +340,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -340,8 +340,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_c = function([], out_c, updates=[out_c.update]) fn_c = function([], out_c, updates=[out_c.update])
fn_c_val0 = fn_c() fn_c_val0 = fn_c()
fn_c_val1 = fn_c() fn_c_val1 = fn_c()
assert numpy.all(fn_c_val0 == fn_a_val0) assert np.all(fn_c_val0 == fn_a_val0)
assert numpy.all(fn_c_val1 == fn_a_val1) assert np.all(fn_c_val1 == fn_a_val1)
# No updates at all # No updates at all
random_d = RandomStreams(utt.fetch_seed()) random_d = RandomStreams(utt.fetch_seed())
...@@ -349,8 +349,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -349,8 +349,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_d = function([], out_d, no_default_updates=True) fn_d = function([], out_d, no_default_updates=True)
fn_d_val0 = fn_d() fn_d_val0 = fn_d()
fn_d_val1 = fn_d() fn_d_val1 = fn_d()
assert numpy.all(fn_d_val0 == fn_a_val0) assert np.all(fn_d_val0 == fn_a_val0)
assert numpy.all(fn_d_val1 == fn_d_val0) assert np.all(fn_d_val1 == fn_d_val0)
# No updates for out # No updates for out
random_e = RandomStreams(utt.fetch_seed()) random_e = RandomStreams(utt.fetch_seed())
...@@ -358,8 +358,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -358,8 +358,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_e = function([], out_e, no_default_updates=[out_e.rng]) fn_e = function([], out_e, no_default_updates=[out_e.rng])
fn_e_val0 = fn_e() fn_e_val0 = fn_e()
fn_e_val1 = fn_e() fn_e_val1 = fn_e()
assert numpy.all(fn_e_val0 == fn_a_val0) assert np.all(fn_e_val0 == fn_a_val0)
assert numpy.all(fn_e_val1 == fn_e_val0) assert np.all(fn_e_val1 == fn_e_val0)
def test_symbolic_shape(self): def test_symbolic_shape(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -407,21 +407,21 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -407,21 +407,21 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], random.multinomial()) g = function([], random.multinomial())
# seed_rng is generator for generating *seeds* for RandomStates # seed_rng is generator for generating *seeds* for RandomStates
seed_rng = numpy.random.RandomState(utt.fetch_seed()) seed_rng = np.random.RandomState(utt.fetch_seed())
uniform_rng = numpy.random.RandomState(int(seed_rng.randint(2**30))) uniform_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = numpy.random.RandomState(int(seed_rng.randint(2**30))) multinomial_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
val0 = f() val0 = f()
val1 = f() val1 = f()
numpy_val0 = uniform_rng.uniform() numpy_val0 = uniform_rng.uniform()
numpy_val1 = uniform_rng.uniform() numpy_val1 = uniform_rng.uniform()
assert numpy.allclose(val0, numpy_val0) assert np.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1) assert np.allclose(val1, numpy_val1)
for i in range(10): # every test has 50% chance of passing even with non-matching random states for i in range(10): # every test has 50% chance of passing even with non-matching random states
val2 = g() val2 = g()
numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5]) numpy_val2 = multinomial_rng.multinomial(n=1, pvals=[.5, .5])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_vector_arguments(self): def test_vector_arguments(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -430,27 +430,27 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -430,27 +430,27 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 1 assert out.ndim == 1
f = function([low], out) f = function([low], out)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0 = f([-5, .5, 0, 1]) val0 = f([-5, .5, 0, 1])
val1 = f([.9]) val1 = f([.9])
numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1) numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1)
numpy_val1 = numpy_rng.uniform(low=[.9], high=1) numpy_val1 = numpy_rng.uniform(low=[.9], high=1)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
high = tensor.vector() high = tensor.vector()
outb = random.uniform(low=low, high=high) outb = random.uniform(low=low, high=high)
assert outb.ndim == 1 assert outb.ndim == 1
fb = function([low, high], outb) fb = function([low, high], outb)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0b = fb([-4., -2], [-1, 0]) val0b = fb([-4., -2], [-1, 0])
val1b = fb([-4.], [-1]) val1b = fb([-4.], [-1])
numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0]) numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1]) numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0b == numpy_val0b) assert np.all(val0b == numpy_val0b)
assert numpy.all(val1b == numpy_val1b) assert np.all(val1b == numpy_val1b)
self.assertRaises(ValueError, fb, [-4., -2], [-1, 0, 1]) self.assertRaises(ValueError, fb, [-4., -2], [-1, 0, 1])
# TODO: do we want that? # TODO: do we want that?
#self.assertRaises(ValueError, fb, [-4., -2], [-1]) #self.assertRaises(ValueError, fb, [-4., -2], [-1])
...@@ -459,13 +459,13 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -459,13 +459,13 @@ class T_SharedRandomStreams(unittest.TestCase):
outc = random.uniform(low=low, high=high, size=size, ndim=1) outc = random.uniform(low=low, high=high, size=size, ndim=1)
fc = function([low, high, size], outc) fc = function([low, high, size], outc)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0c = fc([-4., -2], [-1, 0], [2]) val0c = fc([-4., -2], [-1, 0], [2])
val1c = fc([-4.], [-1], [1]) val1c = fc([-4.], [-1], [1])
numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0]) numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1]) numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0c == numpy_val0c) assert np.all(val0c == numpy_val0c)
assert numpy.all(val1c == numpy_val1c) assert np.all(val1c == numpy_val1c)
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1])
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1, 2]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [1, 2])
self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [2, 1]) self.assertRaises(ValueError, fc, [-4., -2], [-1, 0], [2, 1])
...@@ -481,8 +481,8 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -481,8 +481,8 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 2 assert out.ndim == 2
f = function([low, high], out) f = function([low, high], out)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30) rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = numpy.random.RandomState(int(rng_seed)) numpy_rng = np.random.RandomState(int(rng_seed))
val0 = f([-5, .5, 0, 1], [[1.]]) val0 = f([-5, .5, 0, 1], [[1.]])
val1 = f([.9], [[1.], [1.1], [1.5]]) val1 = f([.9], [[1.], [1.1], [1.5]])
val2 = f([-5, .5, 0, 1], [[1.], [1.1], [1.5]]) val2 = f([-5, .5, 0, 1], [[1.], [1.1], [1.5]])
...@@ -491,9 +491,9 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -491,9 +491,9 @@ class T_SharedRandomStreams(unittest.TestCase):
numpy_val1 = numpy_rng.uniform(low=[.9], high=[[1.], [1.1], [1.5]]) numpy_val1 = numpy_rng.uniform(low=[.9], high=[[1.], [1.1], [1.5]])
numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=[[1.], [1.1], [1.5]]) numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=[[1.], [1.1], [1.5]])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
def test_uniform_vector(self): def test_uniform_vector(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -505,29 +505,29 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -505,29 +505,29 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [.1, .2, .3] low_val = [.1, .2, .3]
high_val = [1.1, 2.2, 3.3] high_val = [1.1, 2.2, 3.3]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(low_val, high_val) val0 = f(low_val, high_val)
numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val) numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
print('THEANO', val0) print('THEANO', val0)
print('NUMPY', numpy_val0) print('NUMPY', numpy_val0)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1]) val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1]) numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
print('THEANO', val1) print('THEANO', val1)
print('NUMPY', numpy_val1) print('NUMPY', numpy_val1)
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([low, high], random.uniform(low=low, high=high, size=(3,))) g = function([low, high], random.uniform(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val) val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,)) numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
def test_binomial_vector(self): def test_binomial_vector(self):
...@@ -539,26 +539,26 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -539,26 +539,26 @@ class T_SharedRandomStreams(unittest.TestCase):
f = function([n, prob], out) f = function([n, prob], out)
n_val = [1, 2, 3] n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX) prob_val = np.asarray([.1, .2, .3], dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(n_val, prob_val) val0 = f(n_val, prob_val)
numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val) numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(n_val[:-1], prob_val[:-1]) val1 = f(n_val[:-1], prob_val[:-1])
numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1]) numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([n, prob], random.binomial(n=n, p=prob, size=(3,))) g = function([n, prob], random.binomial(n=n, p=prob, size=(3,)))
val2 = g(n_val, prob_val) val2 = g(n_val, prob_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,)) numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1]) self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1])
def test_normal_vector(self): def test_normal_vector(self):
...@@ -571,25 +571,25 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -571,25 +571,25 @@ class T_SharedRandomStreams(unittest.TestCase):
avg_val = [1, 2, 3] avg_val = [1, 2, 3]
std_val = [.1, .2, .3] std_val = [.1, .2, .3]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(avg_val, std_val) val0 = f(avg_val, std_val)
numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val) numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val)
assert numpy.allclose(val0, numpy_val0) assert np.allclose(val0, numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(avg_val[:-1], std_val[:-1]) val1 = f(avg_val[:-1], std_val[:-1])
numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1]) numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1])
assert numpy.allclose(val1, numpy_val1) assert np.allclose(val1, numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([avg, std], random.normal(avg=avg, std=std, size=(3,))) g = function([avg, std], random.normal(avg=avg, std=std, size=(3,)))
val2 = g(avg_val, std_val) val2 = g(avg_val, std_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,)) numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,))
assert numpy.allclose(val2, numpy_val2) assert np.allclose(val2, numpy_val2)
self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1]) self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1])
def test_random_integers_vector(self): def test_random_integers_vector(self):
...@@ -602,28 +602,28 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -602,28 +602,28 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [100, 200, 300] low_val = [100, 200, 300]
high_val = [110, 220, 330] high_val = [110, 220, 330]
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(low_val, high_val) val0 = f(low_val, high_val)
numpy_val0 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val0 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1]) val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val1 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val[:-1], high_val[:-1])]) for lv, hv in zip(low_val[:-1], high_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([low, high], random.random_integers(low=low, high=high, size=(3,))) g = function([low, high], random.random_integers(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val) val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1) numpy_val2 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)]) for lv, hv in zip(low_val, high_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1]) self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
# Vectorized permutation don't make sense: the only parameter, n, # Vectorized permutation don't make sense: the only parameter, n,
...@@ -639,29 +639,29 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -639,29 +639,29 @@ class T_SharedRandomStreams(unittest.TestCase):
n_val = [1, 2, 3] n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]] pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX) pvals_val = np.asarray(pvals_val, dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed()) seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,) # Arguments of size (3,)
val0 = f(n_val, pvals_val) val0 = f(n_val, pvals_val)
numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val0 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val0 == numpy_val0) assert np.all(val0 == numpy_val0)
# arguments of size (2,) # arguments of size (2,)
val1 = f(n_val[:-1], pvals_val[:-1]) val1 = f(n_val[:-1], pvals_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val1 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val[:-1], pvals_val[:-1])]) for nv, pv in zip(n_val[:-1], pvals_val[:-1])])
assert numpy.all(val1 == numpy_val1) assert np.all(val1 == numpy_val1)
# Specifying the size explicitly # Specifying the size explicitly
g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,))) g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,)))
val2 = g(n_val, pvals_val) val2 = g(n_val, pvals_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30))) numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv) numpy_val2 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)]) for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val2 == numpy_val2) assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1]) self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1])
def test_dtype(self): def test_dtype(self):
...@@ -677,7 +677,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -677,7 +677,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = f(255, 257) val1 = f(255, 257)
assert val1.dtype == 'int8' assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1) assert np.all(abs(val1) <= 1)
def test_default_dtype(self): def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed()) random = RandomStreams(utt.fetch_seed())
...@@ -704,11 +704,11 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -704,11 +704,11 @@ class T_SharedRandomStreams(unittest.TestCase):
outf = random.uniform(low=lowf, high=highf, size=(42,)) outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX assert outf.dtype == config.floatX
ff = function([lowf, highf], outf) ff = function([lowf, highf], outf)
valf = ff(numpy.float32(-0.1), numpy.float32(0.3)) valf = ff(np.float32(-0.1), np.float32(0.3))
assert valf.dtype == config.floatX assert valf.dtype == config.floatX
def test_shared_constructor_borrow(self): def test_shared_constructor_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng_default = shared(rng) s_rng_default = shared(rng)
s_rng_True = shared(rng, borrow=True) s_rng_True = shared(rng, borrow=True)
s_rng_False = shared(rng, borrow=False) s_rng_False = shared(rng, borrow=False)
...@@ -728,7 +728,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -728,7 +728,7 @@ class T_SharedRandomStreams(unittest.TestCase):
def test_get_value_borrow(self): def test_get_value_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
r_ = s_rng.container.storage[0] r_ = s_rng.container.storage[0]
...@@ -745,7 +745,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -745,7 +745,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand() assert r_.rand() == r_F.rand()
def test_get_value_internal_type(self): def test_get_value_internal_type(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
# there is no special behaviour required of return_internal_type # there is no special behaviour required of return_internal_type
...@@ -765,11 +765,11 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -765,11 +765,11 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand() assert r_.rand() == r_F.rand()
def test_set_value_borrow(self): def test_set_value_borrow(self):
rng = numpy.random.RandomState(123) rng = np.random.RandomState(123)
s_rng = shared(rng) s_rng = shared(rng)
new_rng = numpy.random.RandomState(234234) new_rng = np.random.RandomState(234234)
# Test the borrow contract is respected: # Test the borrow contract is respected:
# assigning with borrow=False makes a copy # assigning with borrow=False makes a copy
...@@ -778,7 +778,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -778,7 +778,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert new_rng.randn() == s_rng.container.storage[0].randn() assert new_rng.randn() == s_rng.container.storage[0].randn()
# Test that the current implementation is actually borrowing when it can. # Test that the current implementation is actually borrowing when it can.
rr = numpy.random.RandomState(33) rr = np.random.RandomState(33)
s_rng.set_value(rr, borrow=True) s_rng.set_value(rr, borrow=True)
assert rr is s_rng.container.storage[0] assert rr is s_rng.container.storage[0]
...@@ -811,7 +811,7 @@ class T_SharedRandomStreams(unittest.TestCase): ...@@ -811,7 +811,7 @@ class T_SharedRandomStreams(unittest.TestCase):
for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates): for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
su2[0].set_value(su1[0].get_value()) su2[0].set_value(su1[0].get_value())
numpy.testing.assert_array_almost_equal(f1(), f2(), decimal=6) np.testing.assert_array_almost_equal(f1(), f2(), decimal=6)
if __name__ == '__main__': if __name__ == '__main__':
......
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 warnings import warnings
...@@ -24,7 +24,7 @@ def makeSharedTester(shared_constructor_, ...@@ -24,7 +24,7 @@ def makeSharedTester(shared_constructor_,
test_internal_type_, test_internal_type_,
theano_fct_, theano_fct_,
ref_fct_, ref_fct_,
cast_value_=numpy.asarray, cast_value_=np.asarray,
op_by_matrix_=False, op_by_matrix_=False,
name=None, name=None,
): ):
...@@ -82,8 +82,8 @@ def makeSharedTester(shared_constructor_, ...@@ -82,8 +82,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -94,17 +94,17 @@ def makeSharedTester(shared_constructor_, ...@@ -94,17 +94,17 @@ def makeSharedTester(shared_constructor_,
total_val = total_func() total_val = total_func()
assert numpy.allclose(self.ref_fct(x), total_val) assert np.allclose(self.ref_fct(x), total_val)
values_to_div = .5 values_to_div = .5
if self.op_by_matrix: if self.op_by_matrix:
values_to_div = self.internal_type(numpy.ones(x.shape, dtype=dtype)/2) # supported for cudandarray, but not ndarray. values_to_div = self.internal_type(np.ones(x.shape, dtype=dtype)/2) # supported for cudandarray, but not ndarray.
assert self.test_internal_type(values_to_div) assert self.test_internal_type(values_to_div)
x /= values_to_div x /= values_to_div
total_val_2 = total_func() total_val_2 = total_func()
# value used to construct should not alias with internal # value used to construct should not alias with internal
assert numpy.allclose(total_val, total_val_2) assert np.allclose(total_val, total_val_2)
x = x_shared.get_value(borrow=False) x = x_shared.get_value(borrow=False)
...@@ -113,7 +113,7 @@ def makeSharedTester(shared_constructor_, ...@@ -113,7 +113,7 @@ def makeSharedTester(shared_constructor_,
total_val_3 = total_func() total_val_3 = total_func()
# value returned by access should not alias with internal # value returned by access should not alias with internal
assert numpy.allclose(total_val, total_val_3) assert np.allclose(total_val, total_val_3)
# in this case we can alias # in this case we can alias
x = x_shared.get_value(borrow=True) x = x_shared.get_value(borrow=True)
...@@ -122,17 +122,17 @@ def makeSharedTester(shared_constructor_, ...@@ -122,17 +122,17 @@ def makeSharedTester(shared_constructor_,
# this is not required by the contract but it is a feature we've # this is not required by the contract but it is a feature we've
# implemented for some type of SharedVariable. # implemented for some type of SharedVariable.
if self.get_value_borrow_true_alias: if self.get_value_borrow_true_alias:
assert numpy.allclose(self.ref_fct(x), total_func()) assert np.allclose(self.ref_fct(x), total_func())
else: else:
assert numpy.allclose(x_ref, total_func()) assert np.allclose(x_ref, total_func())
def test_shape(self): def test_shape(self):
dtype = self.dtype dtype = self.dtype
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -142,7 +142,7 @@ def makeSharedTester(shared_constructor_, ...@@ -142,7 +142,7 @@ def makeSharedTester(shared_constructor_,
f = theano.function([], x_shared.shape) f = theano.function([], x_shared.shape)
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert numpy.all(f() == (2, 4)) assert np.all(f() == (2, 4))
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert len(topo) == 3 assert len(topo) == 3
assert isinstance(topo[0].op, tensor.opt.Shape_i) assert isinstance(topo[0].op, tensor.opt.Shape_i)
...@@ -154,8 +154,8 @@ def makeSharedTester(shared_constructor_, ...@@ -154,8 +154,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -165,7 +165,7 @@ def makeSharedTester(shared_constructor_, ...@@ -165,7 +165,7 @@ def makeSharedTester(shared_constructor_,
f = theano.function([], x_shared.shape[1]) f = theano.function([], x_shared.shape[1])
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
assert numpy.all(f() == (4)) assert np.all(f() == (4))
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert len(topo) == 1 assert len(topo) == 1
assert isinstance(topo[0].op, tensor.opt.Shape_i) assert isinstance(topo[0].op, tensor.opt.Shape_i)
...@@ -175,8 +175,8 @@ def makeSharedTester(shared_constructor_, ...@@ -175,8 +175,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -193,12 +193,12 @@ def makeSharedTester(shared_constructor_, ...@@ -193,12 +193,12 @@ def makeSharedTester(shared_constructor_,
if self.op_by_matrix: if self.op_by_matrix:
# supported for cudandarray, but not ndarray. # supported for cudandarray, but not ndarray.
values_to_div = self.internal_type( values_to_div = self.internal_type(
numpy.ones(x.shape, dtype=dtype)/2) np.ones(x.shape, dtype=dtype)/2)
x /= values_to_div # supported by ndarray and CudaNdarray x /= values_to_div # supported by ndarray and CudaNdarray
# this is not required by the contract but it is a feature we can # this is not required by the contract but it is a feature we can
# implement for some type of SharedVariable. # implement for some type of SharedVariable.
assert numpy.allclose(self.ref_fct(x), total_func()) assert np.allclose(self.ref_fct(x), total_func())
x = x_shared.get_value(borrow=False, return_internal_type=True) x = x_shared.get_value(borrow=False, return_internal_type=True)
assert self.test_internal_type(x) assert self.test_internal_type(x)
...@@ -206,7 +206,7 @@ def makeSharedTester(shared_constructor_, ...@@ -206,7 +206,7 @@ def makeSharedTester(shared_constructor_,
x /= values_to_div # supported by ndarray and CudaNdarray x /= values_to_div # supported by ndarray and CudaNdarray
# this is required by the contract # this is required by the contract
assert not numpy.allclose(self.ref_fct(x), total_func()) assert not np.allclose(self.ref_fct(x), total_func())
def test_get_value(self): def test_get_value(self):
""" """
...@@ -216,8 +216,8 @@ def makeSharedTester(shared_constructor_, ...@@ -216,8 +216,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x_orig = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x_orig = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x_cast = self.cast_value(x_orig) x_cast = self.cast_value(x_orig)
if self.shared_constructor_accept_ndarray: if self.shared_constructor_accept_ndarray:
x_shared = self.shared_constructor(x_orig, borrow=False) x_shared = self.shared_constructor(x_orig, borrow=False)
...@@ -231,8 +231,8 @@ def makeSharedTester(shared_constructor_, ...@@ -231,8 +231,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype) x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_orig = x x_orig = x
...@@ -247,7 +247,7 @@ def makeSharedTester(shared_constructor_, ...@@ -247,7 +247,7 @@ def makeSharedTester(shared_constructor_,
values_to_div = .5 values_to_div = .5
if self.op_by_matrix: if self.op_by_matrix:
# supported for cudandarray, but not ndarray. # supported for cudandarray, but not ndarray.
values_to_div = self.internal_type(numpy.ones(x.shape, dtype=dtype)/2) values_to_div = self.internal_type(np.ones(x.shape, dtype=dtype)/2)
assert self.test_internal_type(values_to_div) assert self.test_internal_type(values_to_div)
# test if that theano shared variable optimize set_value(borrow=True) # test if that theano shared variable optimize set_value(borrow=True)
...@@ -260,7 +260,7 @@ def makeSharedTester(shared_constructor_, ...@@ -260,7 +260,7 @@ def makeSharedTester(shared_constructor_,
assert x is get_x assert x is get_x
else: else:
assert x is not get_x assert x is not get_x
assert numpy.allclose(self.ref_fct(numpy.asarray(x_orig)/.5), self.ref_fct(x)) assert np.allclose(self.ref_fct(np.asarray(x_orig)/.5), self.ref_fct(x))
# test optimized get set value on the gpu(don't pass data to the cpu) # test optimized get set value on the gpu(don't pass data to the cpu)
get_x = x_shared.get_value(borrow=True, return_internal_type=True) get_x = x_shared.get_value(borrow=True, return_internal_type=True)
...@@ -280,8 +280,8 @@ def makeSharedTester(shared_constructor_, ...@@ -280,8 +280,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype) x = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_ref = self.ref_fct(x) x_ref = self.ref_fct(x)
...@@ -293,20 +293,20 @@ def makeSharedTester(shared_constructor_, ...@@ -293,20 +293,20 @@ def makeSharedTester(shared_constructor_,
total_val = total_func() total_val = total_func()
assert numpy.allclose(self.ref_fct(x), total_val) assert np.allclose(self.ref_fct(x), total_val)
values_to_div = .5 values_to_div = .5
if self.op_by_matrix: if self.op_by_matrix:
# supported for cudandarray, but not ndarray. # supported for cudandarray, but not ndarray.
values_to_div = self.internal_type(numpy.ones(x.shape, dtype=dtype)/2) values_to_div = self.internal_type(np.ones(x.shape, dtype=dtype)/2)
assert self.test_internal_type(values_to_div) assert self.test_internal_type(values_to_div)
x /= values_to_div x /= values_to_div
# not required by the contract but it is a feature we've implemented # not required by the contract but it is a feature we've implemented
if self.shared_borrow_true_alias: if self.shared_borrow_true_alias:
assert numpy.allclose(self.ref_fct(x), total_func()) assert np.allclose(self.ref_fct(x), total_func())
else: else:
assert numpy.allclose(x_ref, total_func()) assert np.allclose(x_ref, total_func())
def test_inplace_set_value(self): def test_inplace_set_value(self):
""" """
...@@ -319,25 +319,25 @@ def makeSharedTester(shared_constructor_, ...@@ -319,25 +319,25 @@ def makeSharedTester(shared_constructor_,
shp = (100//4, 1024) # 100KB shp = (100//4, 1024) # 100KB
x = numpy.zeros(shp, dtype=dtype) x = np.zeros(shp, dtype=dtype)
x = self.cast_value(x) x = self.cast_value(x)
x_shared = self.shared_constructor(x, borrow=True) x_shared = self.shared_constructor(x, borrow=True)
old_data = x_shared.container.storage[0] old_data = x_shared.container.storage[0]
nd = numpy.ones(shp, dtype=dtype) nd = np.ones(shp, dtype=dtype)
if x.__class__.__name__ != 'csr_matrix': if x.__class__.__name__ != 'csr_matrix':
# sparse matrix don't support inplace affectation # sparse matrix don't support inplace affectation
x_shared.container.value[:] = nd x_shared.container.value[:] = nd
assert (numpy.asarray(x_shared.get_value(borrow=True)) == nd).all() assert (np.asarray(x_shared.get_value(borrow=True)) == nd).all()
# This should always share value! # This should always share value!
assert may_share_memory(old_data, x_shared.container.storage[0]) assert may_share_memory(old_data, x_shared.container.storage[0])
assert may_share_memory(old_data, x_shared.get_value(borrow=True, return_internal_type=True)) assert may_share_memory(old_data, x_shared.get_value(borrow=True, return_internal_type=True))
nd[0] += 1 nd[0] += 1
x_shared.container.value[0] = nd[0] x_shared.container.value[0] = nd[0]
assert (numpy.asarray(x_shared.get_value(borrow=True)[0]) == nd[0]).all() assert (np.asarray(x_shared.get_value(borrow=True)[0]) == nd[0]).all()
assert (numpy.asarray(x_shared.get_value(borrow=True)[1:]) == nd[1:]).all() assert (np.asarray(x_shared.get_value(borrow=True)[1:]) == nd[1:]).all()
# This should always share value! # This should always share value!
assert may_share_memory(old_data, x_shared.container.storage[0]) assert may_share_memory(old_data, x_shared.container.storage[0])
assert may_share_memory(old_data, x_shared.get_value(borrow=True, return_internal_type=True)) assert may_share_memory(old_data, x_shared.get_value(borrow=True, return_internal_type=True))
...@@ -347,7 +347,7 @@ def makeSharedTester(shared_constructor_, ...@@ -347,7 +347,7 @@ def makeSharedTester(shared_constructor_,
nd += 1 nd += 1
# THIS DON't DO WHAT WE EXPECT the contain of a is not updated for CudaNdarray, but it is for ndarray # THIS DON't DO WHAT WE EXPECT the contain of a is not updated for CudaNdarray, but it is for ndarray
x_shared.get_value(borrow=True)[:] = nd x_shared.get_value(borrow=True)[:] = nd
#assert (numpy.asarray(x_shared.get_value(borrow=True))!=nd).all() #assert (np.asarray(x_shared.get_value(borrow=True))!=nd).all()
assert may_share_memory(old_data, x_shared.container.storage[0]) assert may_share_memory(old_data, x_shared.container.storage[0])
x_shared.get_value(borrow=True) x_shared.get_value(borrow=True)
...@@ -355,7 +355,7 @@ def makeSharedTester(shared_constructor_, ...@@ -355,7 +355,7 @@ def makeSharedTester(shared_constructor_,
nd += 1 nd += 1
old_data = x_shared.container.storage[0] old_data = x_shared.container.storage[0]
x_shared.set_value(nd, borrow=False) x_shared.set_value(nd, borrow=False)
assert numpy.allclose(self.ref_fct(x_shared.get_value(borrow=True)), assert np.allclose(self.ref_fct(x_shared.get_value(borrow=True)),
self.ref_fct(self.cast_value(nd))) self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace
...@@ -364,7 +364,7 @@ def makeSharedTester(shared_constructor_, ...@@ -364,7 +364,7 @@ def makeSharedTester(shared_constructor_,
nd += 1 nd += 1
old_data = x_shared.container.storage[0] old_data = x_shared.container.storage[0]
x_shared.set_value(self.cast_value(nd), borrow=False) x_shared.set_value(self.cast_value(nd), borrow=False)
assert numpy.allclose(self.ref_fct(x_shared.get_value(borrow=True)), assert np.allclose(self.ref_fct(x_shared.get_value(borrow=True)),
self.ref_fct(self.cast_value(nd))) self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_cast_value_inplace assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_cast_value_inplace
...@@ -372,7 +372,7 @@ def makeSharedTester(shared_constructor_, ...@@ -372,7 +372,7 @@ def makeSharedTester(shared_constructor_,
nd += 1 nd += 1
old_data = x_shared.container.storage[0] old_data = x_shared.container.storage[0]
x_shared.set_value(nd.copy(), borrow=True) x_shared.set_value(nd.copy(), borrow=True)
assert numpy.allclose(self.ref_fct(x_shared.get_value(borrow=True)), assert np.allclose(self.ref_fct(x_shared.get_value(borrow=True)),
self.ref_fct(self.cast_value(nd))) self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace
...@@ -380,7 +380,7 @@ def makeSharedTester(shared_constructor_, ...@@ -380,7 +380,7 @@ def makeSharedTester(shared_constructor_,
nd += 1 nd += 1
old_data = x_shared.container.storage[0] old_data = x_shared.container.storage[0]
x_shared.set_value(self.cast_value(nd.copy()), borrow=True) x_shared.set_value(self.cast_value(nd.copy()), borrow=True)
assert numpy.allclose(self.ref_fct(x_shared.get_value(borrow=True)), self.ref_fct(self.cast_value(nd))) assert np.allclose(self.ref_fct(x_shared.get_value(borrow=True)), self.ref_fct(self.cast_value(nd)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_cast_value_inplace assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_cast_value_inplace
def test_specify_shape(self): def test_specify_shape(self):
...@@ -388,19 +388,19 @@ def makeSharedTester(shared_constructor_, ...@@ -388,19 +388,19 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x1_1 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype) x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x1_1 = self.cast_value(x1_1) x1_1 = self.cast_value(x1_1)
x1_2 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype) x1_2 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x1_2 = self.cast_value(x1_2) x1_2 = self.cast_value(x1_2)
x2 = numpy.asarray(rng.uniform(1, 2, [4, 3]), dtype=dtype) x2 = np.asarray(rng.uniform(1, 2, [4, 3]), dtype=dtype)
x2 = self.cast_value(x2) x2 = self.cast_value(x2)
# Test that we can replace with values of the same shape # Test that we can replace with values of the same shape
x1_shared = self.shared_constructor(x1_1) x1_shared = self.shared_constructor(x1_1)
x1_specify_shape = tensor.specify_shape(x1_shared, x1_1.shape) x1_specify_shape = tensor.specify_shape(x1_shared, x1_1.shape)
x1_shared.set_value(x1_2) x1_shared.set_value(x1_2)
assert numpy.allclose(self.ref_fct(x1_shared.get_value(borrow=True)), assert np.allclose(self.ref_fct(x1_shared.get_value(borrow=True)),
self.ref_fct( x1_2)) self.ref_fct( x1_2))
shape_op_fct = theano.function([], x1_shared.shape) shape_op_fct = theano.function([], x1_shared.shape)
topo = shape_op_fct.maker.fgraph.toposort() topo = shape_op_fct.maker.fgraph.toposort()
...@@ -412,14 +412,14 @@ def makeSharedTester(shared_constructor_, ...@@ -412,14 +412,14 @@ def makeSharedTester(shared_constructor_,
# Test that we forward the input # Test that we forward the input
specify_shape_fct = theano.function([], x1_specify_shape) specify_shape_fct = theano.function([], x1_specify_shape)
assert numpy.all(self.ref_fct(specify_shape_fct()) == assert np.all(self.ref_fct(specify_shape_fct()) ==
self.ref_fct(x1_2)) self.ref_fct(x1_2))
topo_specify = specify_shape_fct.maker.fgraph.toposort() topo_specify = specify_shape_fct.maker.fgraph.toposort()
assert len(topo_specify) == 2 assert len(topo_specify) == 2
# Test that we put the shape info into the graph # Test that we put the shape info into the graph
shape_constant_fct = theano.function([], x1_specify_shape.shape) shape_constant_fct = theano.function([], x1_specify_shape.shape)
assert numpy.all(shape_constant_fct() == shape_op_fct()) assert np.all(shape_constant_fct() == shape_op_fct())
topo_cst = shape_constant_fct.maker.fgraph.toposort() topo_cst = shape_constant_fct.maker.fgraph.toposort()
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert len(topo_cst) == 1 assert len(topo_cst) == 1
...@@ -454,12 +454,12 @@ def makeSharedTester(shared_constructor_, ...@@ -454,12 +454,12 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x1_1 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype) x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x1_1 = self.cast_value(x1_1) x1_1 = self.cast_value(x1_1)
x1_2 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype) x1_2 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x1_2 = self.cast_value(x1_2) x1_2 = self.cast_value(x1_2)
x2 = numpy.asarray(rng.uniform(1, 2, [5, 2]), dtype=dtype) x2 = np.asarray(rng.uniform(1, 2, [5, 2]), dtype=dtype)
x2 = self.cast_value(x2) x2 = self.cast_value(x2)
# Test that we can replace with values of the same shape # Test that we can replace with values of the same shape
...@@ -468,7 +468,7 @@ def makeSharedTester(shared_constructor_, ...@@ -468,7 +468,7 @@ def makeSharedTester(shared_constructor_,
(tensor.as_tensor_variable(x1_1.shape[0]), (tensor.as_tensor_variable(x1_1.shape[0]),
x1_shared.shape[1])) x1_shared.shape[1]))
x1_shared.set_value(x1_2) x1_shared.set_value(x1_2)
assert numpy.allclose( assert np.allclose(
self.ref_fct(x1_shared.get_value(borrow=True)), self.ref_fct(x1_shared.get_value(borrow=True)),
self.ref_fct( x1_2)) self.ref_fct( x1_2))
shape_op_fct = theano.function([], x1_shared.shape) shape_op_fct = theano.function([], x1_shared.shape)
...@@ -484,7 +484,7 @@ def makeSharedTester(shared_constructor_, ...@@ -484,7 +484,7 @@ def makeSharedTester(shared_constructor_,
specify_shape_fct = theano.function([], x1_specify_shape) specify_shape_fct = theano.function([], x1_specify_shape)
specify_shape_fct() specify_shape_fct()
# theano.printing.debugprint(specify_shape_fct) # theano.printing.debugprint(specify_shape_fct)
assert numpy.all(self.ref_fct(specify_shape_fct()) assert np.all(self.ref_fct(specify_shape_fct())
== self.ref_fct(x1_2)) == self.ref_fct(x1_2))
topo_specify = specify_shape_fct.maker.fgraph.toposort() topo_specify = specify_shape_fct.maker.fgraph.toposort()
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
...@@ -493,7 +493,7 @@ def makeSharedTester(shared_constructor_, ...@@ -493,7 +493,7 @@ def makeSharedTester(shared_constructor_,
# Test that we put the shape info into the graph # Test that we put the shape info into the graph
shape_constant_fct = theano.function([], x1_specify_shape.shape) shape_constant_fct = theano.function([], x1_specify_shape.shape)
# theano.printing.debugprint(shape_constant_fct) # theano.printing.debugprint(shape_constant_fct)
assert numpy.all(shape_constant_fct() == shape_op_fct()) assert np.all(shape_constant_fct() == shape_op_fct())
topo_cst = shape_constant_fct.maker.fgraph.toposort() topo_cst = shape_constant_fct.maker.fgraph.toposort()
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert len(topo_cst) == 2 assert len(topo_cst) == 2
...@@ -516,14 +516,14 @@ def makeSharedTester(shared_constructor_, ...@@ -516,14 +516,14 @@ def makeSharedTester(shared_constructor_,
if dtype is None: if dtype is None:
dtype = theano.config.floatX dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
a = numpy.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype) a = np.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
a = self.cast_value(a) a = self.cast_value(a)
a_shared = self.shared_constructor(a) a_shared = self.shared_constructor(a)
b = numpy.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype) b = np.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
b = self.cast_value(b) b = self.cast_value(b)
b_shared = self.shared_constructor(b) b_shared = self.shared_constructor(b)
s = numpy.zeros((40, 40), dtype=dtype) s = np.zeros((40, 40), dtype=dtype)
s = self.cast_value(s) s = self.cast_value(s)
s_shared = self.shared_constructor(s) s_shared = self.shared_constructor(s)
f = theano.function([], f = theano.function([],
...@@ -546,7 +546,7 @@ def makeSharedTester(shared_constructor_, ...@@ -546,7 +546,7 @@ def makeSharedTester(shared_constructor_,
+ s_shared_specify)]) + s_shared_specify)])
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
shp = f() shp = f()
assert numpy.all(shp == (40, 40)) assert np.all(shp == (40, 40))
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert sum([node.op.__class__.__name__ in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo]) == 1 assert sum([node.op.__class__.__name__ in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo]) == 1
assert all(node.op == tensor.blas.gemm_inplace for node in topo if isinstance(node.op, tensor.blas.Gemm)) assert all(node.op == tensor.blas.gemm_inplace for node in topo if isinstance(node.op, tensor.blas.Gemm))
...@@ -562,7 +562,7 @@ def makeSharedTester(shared_constructor_, ...@@ -562,7 +562,7 @@ def makeSharedTester(shared_constructor_,
+ s_shared_specify)]) + s_shared_specify)])
topo = f.maker.fgraph.toposort() topo = f.maker.fgraph.toposort()
shp = f() shp = f()
assert numpy.all(shp == (40, 40)) assert np.all(shp == (40, 40))
if theano.config.mode != 'FAST_COMPILE': if theano.config.mode != 'FAST_COMPILE':
assert sum([node.op.__class__.__name__ in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo]) == 1 assert sum([node.op.__class__.__name__ in ["Gemm", "GpuGemm", "StructuredDot"] for node in topo]) == 1
assert all(node.op == tensor.blas.gemm_inplace for node in topo if isinstance(node.op, tensor.blas.Gemm)) assert all(node.op == tensor.blas.gemm_inplace for node in topo if isinstance(node.op, tensor.blas.Gemm))
...@@ -578,9 +578,9 @@ def makeSharedTester(shared_constructor_, ...@@ -578,9 +578,9 @@ def makeSharedTester(shared_constructor_,
shp = (1024, 1024) shp = (1024, 1024)
# Test the case with all zeros element # Test the case with all zeros element
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
for x in [numpy.asarray(rng.rand(*shp), dtype=dtype), for x in [np.asarray(rng.rand(*shp), dtype=dtype),
numpy.zeros(shp, dtype=dtype)]: np.zeros(shp, dtype=dtype)]:
zeros = (x == 0).all() zeros = (x == 0).all()
x = self.cast_value(x) x = self.cast_value(x)
x_shared = self.shared_constructor(x, borrow=True) x_shared = self.shared_constructor(x, borrow=True)
...@@ -592,7 +592,7 @@ def makeSharedTester(shared_constructor_, ...@@ -592,7 +592,7 @@ def makeSharedTester(shared_constructor_,
assert x_shared.type.values_eq(x, x) assert x_shared.type.values_eq(x, x)
assert x_shared.type.values_eq_approx(x, x) assert x_shared.type.values_eq_approx(x, x)
if not zeros: if not zeros:
assert not numpy.allclose(self.ref_fct(x), self.ref_fct(y)) assert not np.allclose(self.ref_fct(x), self.ref_fct(y))
assert not x_shared.type.values_eq(x, y) assert not x_shared.type.values_eq(x, y)
assert not x_shared.type.values_eq_approx(x, y) assert not x_shared.type.values_eq_approx(x, y)
...@@ -612,11 +612,11 @@ test_shared_options = makeSharedTester( ...@@ -612,11 +612,11 @@ test_shared_options = makeSharedTester(
set_value_inplace_=False, set_value_inplace_=False,
set_cast_value_inplace_=False, set_cast_value_inplace_=False,
shared_constructor_accept_ndarray_=True, shared_constructor_accept_ndarray_=True,
internal_type_=numpy.ndarray, internal_type_=np.ndarray,
test_internal_type_=lambda a: isinstance(a, numpy.ndarray), test_internal_type_=lambda a: isinstance(a, np.ndarray),
theano_fct_=lambda a: a*2, theano_fct_=lambda a: a*2,
ref_fct_=lambda a: numpy.asarray((a*2)), ref_fct_=lambda a: np.asarray((a*2)),
cast_value_=numpy.asarray, cast_value_=np.asarray,
op_by_matrix_=False, op_by_matrix_=False,
name='test_shared_options') name='test_shared_options')
...@@ -624,4 +624,4 @@ test_shared_options = makeSharedTester( ...@@ -624,4 +624,4 @@ test_shared_options = makeSharedTester(
def test_scalar_shared_options(): def test_scalar_shared_options():
# Simple test to make sure we do not loose that fonctionality. # Simple test to make sure we do not loose that fonctionality.
theano.shared(value=0., name='lk', borrow=True) theano.shared(value=0., name='lk', borrow=True)
theano.shared(value=numpy.float32(0.), name='lk', borrow=True) theano.shared(value=np.float32(0.), name='lk', borrow=True)
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.linalg import numpy.linalg
from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose from numpy.testing import dec, assert_array_equal, assert_allclose
...@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f): ...@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f):
ch = ch_f(pd) ch = ch_f(pd)
assert ch[0, pd.shape[1] - 1] == 0 assert ch[0, pd.shape[1] - 1] == 0
assert ch[pd.shape[0] - 1, 0] != 0 assert ch[pd.shape[0] - 1, 0] != 0
assert numpy.allclose(numpy.dot(ch, ch.T), pd) assert np.allclose(np.dot(ch, ch.T), pd)
assert not numpy.allclose(numpy.dot(ch.T, ch), pd) assert not np.allclose(np.dot(ch.T, ch), pd)
def check_upper_triangular(pd, ch_f): def check_upper_triangular(pd, ch_f):
ch = ch_f(pd) ch = ch_f(pd)
assert ch[4, 0] == 0 assert ch[4, 0] == 0
assert ch[0, 4] != 0 assert ch[0, 4] != 0
assert numpy.allclose(numpy.dot(ch.T, ch), pd) assert np.allclose(np.dot(ch.T, ch), pd)
assert not numpy.allclose(numpy.dot(ch, ch.T), pd) assert not np.allclose(np.dot(ch, ch.T), pd)
def test_cholesky(): def test_cholesky():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
pd = numpy.dot(r, r.T) pd = np.dot(r, r.T)
x = tensor.matrix() x = tensor.matrix()
chol = cholesky(x) chol = cholesky(x)
# Check the default. # Check the default.
...@@ -72,7 +72,7 @@ def test_cholesky(): ...@@ -72,7 +72,7 @@ def test_cholesky():
def test_cholesky_grad(): def test_cholesky_grad():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX) r = rng.randn(5, 5).astype(config.floatX)
# The dots are inside the graph since Cholesky needs separable matrices # The dots are inside the graph since Cholesky needs separable matrices
...@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape(): ...@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.") raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
x = tensor.matrix() x = tensor.matrix()
for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)): for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)):
f_chol = theano.function([x], l.shape) f_chol = theano.function([x], l.shape)
...@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape(): ...@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape():
assert sum([node.op.__class__ == CholeskyGrad assert sum([node.op.__class__ == CholeskyGrad
for node in topo_cholgrad]) == 0 for node in topo_cholgrad]) == 0
for shp in [2, 3, 5]: for shp in [2, 3, 5]:
m = numpy.cov(rng.randn(shp, shp + 10)).astype(config.floatX) m = np.cov(rng.randn(shp, shp + 10)).astype(config.floatX)
yield numpy.testing.assert_equal, f_chol(m), (shp, shp) yield np.testing.assert_equal, f_chol(m), (shp, shp)
yield numpy.testing.assert_equal, f_cholgrad(m), (shp, shp) yield np.testing.assert_equal, f_cholgrad(m), (shp, shp)
def test_eigvalsh(): def test_eigvalsh():
...@@ -121,13 +121,13 @@ def test_eigvalsh(): ...@@ -121,13 +121,13 @@ def test_eigvalsh():
B = theano.tensor.dmatrix('b') B = theano.tensor.dmatrix('b')
f = function([A, B], eigvalsh(A, B)) f = function([A, B], eigvalsh(A, B))
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5) a = rng.randn(5, 5)
a = a + a.T a = a + a.T
for b in [10 * numpy.eye(5, 5) + rng.randn(5, 5)]: for b in [10 * np.eye(5, 5) + rng.randn(5, 5)]:
w = f(a, b) w = f(a, b)
refw = scipy.linalg.eigvalsh(a, b) refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw) np.testing.assert_array_almost_equal(w, refw)
# We need to test None separatly, as otherwise DebugMode will # We need to test None separatly, as otherwise DebugMode will
# complain, as this isn't a valid ndarray. # complain, as this isn't a valid ndarray.
...@@ -136,7 +136,7 @@ def test_eigvalsh(): ...@@ -136,7 +136,7 @@ def test_eigvalsh():
f = function([A], eigvalsh(A, B)) f = function([A], eigvalsh(A, B))
w = f(a) w = f(a)
refw = scipy.linalg.eigvalsh(a, b) refw = scipy.linalg.eigvalsh(a, b)
numpy.testing.assert_array_almost_equal(w, refw) np.testing.assert_array_almost_equal(w, refw)
def test_eigvalsh_grad(): def test_eigvalsh_grad():
...@@ -144,12 +144,12 @@ def test_eigvalsh_grad(): ...@@ -144,12 +144,12 @@ def test_eigvalsh_grad():
raise SkipTest("Scipy needed for the geigvalsh op.") raise SkipTest("Scipy needed for the geigvalsh op.")
import scipy.linalg import scipy.linalg
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5) a = rng.randn(5, 5)
a = a + a.T a = a + a.T
b = 10 * numpy.eye(5, 5) + rng.randn(5, 5) b = 10 * np.eye(5, 5) + rng.randn(5, 5)
tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]), tensor.verify_grad(lambda a, b: eigvalsh(a, b).dot([1, 2, 3, 4, 5]),
[a, b], rng=numpy.random) [a, b], rng=np.random)
class test_Solve(utt.InferShapeTester): class test_Solve(utt.InferShapeTester):
...@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester): ...@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Solve op.") raise SkipTest("Scipy needed for the Solve op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.matrix() b = theano.tensor.matrix()
self._compile_and_check([A, b], # theano.function inputs self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs [self.op(A, b)], # theano.function outputs
# A must be square # A must be square
[numpy.asarray(rng.rand(5, 5), [np.asarray(rng.rand(5, 5),
dtype=config.floatX), dtype=config.floatX),
numpy.asarray(rng.rand(5, 1), np.asarray(rng.rand(5, 1),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class, self.op_class,
warn=False) warn=False)
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.vector() b = theano.tensor.vector()
self._compile_and_check([A, b], # theano.function inputs self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs [self.op(A, b)], # theano.function outputs
# A must be square # A must be square
[numpy.asarray(rng.rand(5, 5), [np.asarray(rng.rand(5, 5),
dtype=config.floatX), dtype=config.floatX),
numpy.asarray(rng.rand(5), np.asarray(rng.rand(5),
dtype=config.floatX)], dtype=config.floatX)],
self.op_class, self.op_class,
warn=False) warn=False)
...@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_correctness(self): def test_solve_correctness(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky and Solve ops.") raise SkipTest("Scipy needed for the Cholesky and Solve ops.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix() A = theano.tensor.matrix()
b = theano.tensor.matrix() b = theano.tensor.matrix()
y = self.op(A, b) y = self.op(A, b)
...@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester): ...@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester):
y_upper = self.op(U, b) y_upper = self.op(U, b)
upper_solve_func = theano.function([U, b], y_upper) upper_solve_func = theano.function([U, b], y_upper)
b_val = numpy.asarray(rng.rand(5, 1), dtype=config.floatX) b_val = np.asarray(rng.rand(5, 1), dtype=config.floatX)
# 1-test general case # 1-test general case
A_val = numpy.asarray(rng.rand(5, 5), dtype=config.floatX) A_val = np.asarray(rng.rand(5, 5), dtype=config.floatX)
# positive definite matrix: # positive definite matrix:
A_val = numpy.dot(A_val.transpose(), A_val) A_val = np.dot(A_val.transpose(), A_val)
assert numpy.allclose(scipy.linalg.solve(A_val, b_val), assert np.allclose(scipy.linalg.solve(A_val, b_val),
gen_solve_func(A_val, b_val)) gen_solve_func(A_val, b_val))
# 2-test lower traingular case # 2-test lower traingular case
L_val = scipy.linalg.cholesky(A_val, lower=True) L_val = scipy.linalg.cholesky(A_val, lower=True)
assert numpy.allclose(scipy.linalg.solve_triangular(L_val, b_val, lower=True), assert np.allclose(scipy.linalg.solve_triangular(L_val, b_val, lower=True),
lower_solve_func(L_val, b_val)) lower_solve_func(L_val, b_val))
# 3-test upper traingular case # 3-test upper traingular case
U_val = scipy.linalg.cholesky(A_val, lower=False) U_val = scipy.linalg.cholesky(A_val, lower=False)
assert numpy.allclose(scipy.linalg.solve_triangular(U_val, b_val, lower=False), assert np.allclose(scipy.linalg.solve_triangular(U_val, b_val, lower=False),
upper_solve_func(U_val, b_val)) upper_solve_func(U_val, b_val))
def test_solve_dtype(self): def test_solve_dtype(self):
...@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester): ...@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester):
'int8', 'int16', 'int32', 'int64', 'int8', 'int16', 'int32', 'int64',
'float16', 'float32', 'float64'] 'float16', 'float32', 'float64']
A_val = numpy.eye(2) A_val = np.eye(2)
b_val = numpy.ones((2, 1)) b_val = np.ones((2, 1))
# try all dtype combinations # try all dtype combinations
for A_dtype, b_dtype in itertools.product(dtypes, dtypes): for A_dtype, b_dtype in itertools.product(dtypes, dtypes):
...@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester): ...@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester):
# ensure diagonal elements of A relatively large to avoid numerical # ensure diagonal elements of A relatively large to avoid numerical
# precision issues # precision issues
A_val = (rng.normal(size=(m, m)) * 0.5 + A_val = (rng.normal(size=(m, m)) * 0.5 +
numpy.eye(m)).astype(config.floatX) np.eye(m)).astype(config.floatX)
if A_structure == 'lower_triangular': if A_structure == 'lower_triangular':
A_val = numpy.tril(A_val) A_val = np.tril(A_val)
elif A_structure == 'upper_triangular': elif A_structure == 'upper_triangular':
A_val = numpy.triu(A_val) A_val = np.triu(A_val)
if n is None: if n is None:
b_val = rng.normal(size=m).astype(config.floatX) b_val = rng.normal(size=m).astype(config.floatX)
else: else:
...@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_grad(self): def test_solve_grad(self):
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the Solve op.") raise SkipTest("Scipy needed for the Solve op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
structures = ['general', 'lower_triangular', 'upper_triangular'] structures = ['general', 'lower_triangular', 'upper_triangular']
for A_structure in structures: for A_structure in structures:
lower = (A_structure == 'lower_triangular') lower = (A_structure == 'lower_triangular')
...@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester): ...@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester):
def test_expm(): def test_expm():
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
A = rng.randn(5, 5).astype(config.floatX) A = rng.randn(5, 5).astype(config.floatX)
ref = scipy.linalg.expm(A) ref = scipy.linalg.expm(A)
...@@ -292,14 +292,14 @@ def test_expm(): ...@@ -292,14 +292,14 @@ def test_expm():
expm_f = function([x], m) expm_f = function([x], m)
val = expm_f(A) val = expm_f(A)
numpy.testing.assert_array_almost_equal(val, ref) np.testing.assert_array_almost_equal(val, ref)
def test_expm_grad_1(): def test_expm_grad_1():
# with symmetric matrix (real eigenvectors) # with symmetric matrix (real eigenvectors)
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
A = A + A.T A = A + A.T
...@@ -311,12 +311,12 @@ def test_expm_grad_2(): ...@@ -311,12 +311,12 @@ def test_expm_grad_2():
# with non-symmetric matrix with real eigenspecta # with non-symmetric matrix with real eigenspecta
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
w = rng.randn(5)**2 w = rng.randn(5)**2
A = (numpy.diag(w**0.5)).dot(A + A.T).dot(numpy.diag(w**(-0.5))) A = (np.diag(w**0.5)).dot(A + A.T).dot(np.diag(w**(-0.5)))
assert not numpy.allclose(A, A.T) assert not np.allclose(A, A.T)
tensor.verify_grad(expm, [A], rng=rng) tensor.verify_grad(expm, [A], rng=rng)
...@@ -325,7 +325,7 @@ def test_expm_grad_3(): ...@@ -325,7 +325,7 @@ def test_expm_grad_3():
# with non-symmetric matrix (complex eigenvectors) # with non-symmetric matrix (complex eigenvectors)
if not imported_scipy: if not imported_scipy:
raise SkipTest("Scipy needed for the expm op.") raise SkipTest("Scipy needed for the expm op.")
rng = numpy.random.RandomState(utt.fetch_seed()) rng = np.random.RandomState(utt.fetch_seed())
# Always test in float64 for better numerical stability. # Always test in float64 for better numerical stability.
A = rng.randn(5, 5) A = rng.randn(5, 5)
...@@ -334,7 +334,7 @@ def test_expm_grad_3(): ...@@ -334,7 +334,7 @@ def test_expm_grad_3():
class TestKron(utt.InferShapeTester): class TestKron(utt.InferShapeTester):
rng = numpy.random.RandomState(43) rng = np.random.RandomState(43)
def setUp(self): def setUp(self):
super(TestKron, self).setUp() super(TestKron, self).setUp()
...@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester): ...@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]: for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]:
x = tensor.tensor(dtype='floatX', x = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp0)) broadcastable=(False,) * len(shp0))
a = numpy.asarray(self.rng.rand(*shp0)).astype(config.floatX) a = np.asarray(self.rng.rand(*shp0)).astype(config.floatX)
for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]: for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]:
if len(shp0) + len(shp1) == 2: if len(shp0) + len(shp1) == 2:
continue continue
...@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester): ...@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester):
# so we have to add a dimension to a and flatten the result. # so we have to add a dimension to a and flatten the result.
if len(shp0) + len(shp1) == 3: if len(shp0) + len(shp1) == 3:
scipy_val = scipy.linalg.kron( scipy_val = scipy.linalg.kron(
a[numpy.newaxis, :], b).flatten() a[np.newaxis, :], b).flatten()
else: else:
scipy_val = scipy.linalg.kron(a, b) scipy_val = scipy.linalg.kron(a, b)
utt.assert_allclose(out, scipy_val) utt.assert_allclose(out, scipy_val)
...@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester): ...@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2, 3)]: for shp0 in [(2, 3)]:
x = tensor.tensor(dtype='floatX', x = tensor.tensor(dtype='floatX',
broadcastable=(False,) * len(shp0)) broadcastable=(False,) * len(shp0))
a = numpy.asarray(self.rng.rand(*shp0)).astype(config.floatX) a = np.asarray(self.rng.rand(*shp0)).astype(config.floatX)
for shp1 in [(6, 7)]: for shp1 in [(6, 7)]:
if len(shp0) + len(shp1) == 2: if len(shp0) + len(shp1) == 2:
continue continue
...@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester): ...@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester):
f = function([x, y], kron(x, y)) f = function([x, y], kron(x, y))
b = self.rng.rand(*shp1).astype(config.floatX) b = self.rng.rand(*shp1).astype(config.floatX)
out = f(a, b) out = f(a, b)
assert numpy.allclose(out, numpy.kron(a, b)) assert np.allclose(out, np.kron(a, b))
...@@ -4,7 +4,7 @@ import logging ...@@ -4,7 +4,7 @@ import logging
import sys import sys
import unittest import unittest
import numpy import numpy as np
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose.tools import assert_equal from nose.tools import assert_equal
from numpy.testing import assert_array_equal from numpy.testing import assert_array_equal
...@@ -119,7 +119,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -119,7 +119,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test0_err_invalid(self): def test0_err_invalid(self):
# it is impossible to retrieve a view of a 0-d tensor # it is impossible to retrieve a view of a 0-d tensor
n = self.shared(numpy.ones((), dtype=self.dtype)) n = self.shared(np.ones((), dtype=self.dtype))
try: try:
n[0] n[0]
except ValueError as e: except ValueError as e:
...@@ -128,7 +128,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -128,7 +128,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail() self.fail()
def test1_err_bounds(self): def test1_err_bounds(self):
n = self.shared(numpy.ones(3, dtype=self.dtype)) n = self.shared(np.ones(3, dtype=self.dtype))
ctv_backup = config.compute_test_value ctv_backup = config.compute_test_value
config.compute_test_value = 'off' config.compute_test_value = 'off'
try: try:
...@@ -150,7 +150,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -150,7 +150,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
_logger.setLevel(oldlevel) _logger.setLevel(oldlevel)
def test1_err_subslice(self): def test1_err_subslice(self):
n = self.shared(numpy.ones(3, dtype=self.dtype)) n = self.shared(np.ones(3, dtype=self.dtype))
try: try:
n[slice(0, slice(1, 2, None), None)] n[slice(0, slice(1, 2, None), None)]
except Exception: except Exception:
...@@ -162,7 +162,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -162,7 +162,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail() self.fail()
def test1_ok_range_finite(self): def test1_ok_range_finite(self):
n = self.shared(numpy.arange(3, dtype=self.dtype)) n = self.shared(np.arange(3, dtype=self.dtype))
t = n[0:2] t = n[0:2]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
...@@ -170,24 +170,24 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -170,24 +170,24 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [0, 1]).all()) self.assertTrue((tval == [0, 1]).all())
def test2_ok_range_finite(self): def test2_ok_range_finite(self):
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((3, 4))) n = self.shared(np.arange(12, dtype=self.dtype).reshape((3, 4)))
# Also check negative index # Also check negative index
for idx in [(slice(0, 2), 3), ((slice(0, 2), -1)), (slice(0, 2), -4)]: for idx in [(slice(0, 2), 3), ((slice(0, 2), -1)), (slice(0, 2), -4)]:
t = n[idx] # l]#0:2,3] t = n[idx] # l]#0:2,3]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2,)) self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.allclose(tval, n.get_value()[idx])) self.assertTrue(np.allclose(tval, n.get_value()[idx]))
def test1_0_dims(self): def test1_0_dims(self):
n = self.shared(numpy.ones((), dtype=self.dtype)) n = self.shared(np.ones((), dtype=self.dtype))
t = self.sub([])(n) t = self.sub([])(n)
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
self.eval_output_and_check( self.eval_output_and_check(
t, mode=self.mode.excluding("local_useless_subtensor")) t, mode=self.mode.excluding("local_useless_subtensor"))
def test1_err_invalid(self): def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype)) n = self.shared(np.ones(1, dtype=self.dtype))
try: try:
n[0, 0] n[0, 0]
except ValueError as e: except ValueError as e:
...@@ -196,7 +196,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -196,7 +196,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail() self.fail()
def test1_ok_elem(self): def test1_ok_elem(self):
n = self.shared(numpy.ones(1, dtype=self.dtype) * 5) n = self.shared(np.ones(1, dtype=self.dtype) * 5)
t = n[0] t = n[0]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
...@@ -204,7 +204,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -204,7 +204,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(tval == 5.0) self.assertTrue(tval == 5.0)
def test1_ok_range_infinite(self): def test1_ok_range_infinite(self):
n = self.shared(numpy.arange(3, dtype=self.dtype)) n = self.shared(np.arange(3, dtype=self.dtype))
t = n[1:] t = n[1:]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
...@@ -212,7 +212,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -212,7 +212,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [1.0, 2.0]).all()) self.assertTrue((tval == [1.0, 2.0]).all())
def test1_ok_strided(self): def test1_ok_strided(self):
n = self.shared(numpy.arange(5, dtype=self.dtype)) n = self.shared(np.arange(5, dtype=self.dtype))
t = n[1::2] t = n[1::2]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
...@@ -225,7 +225,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -225,7 +225,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [0.0, 2.0]).all()) self.assertTrue((tval == [0.0, 2.0]).all())
def test2_err_bounds0(self): def test2_err_bounds0(self):
n = self.shared(numpy.ones((2, 3), dtype=self.dtype) * 5) n = self.shared(np.ones((2, 3), dtype=self.dtype) * 5)
ctv_backup = config.compute_test_value ctv_backup = config.compute_test_value
config.compute_test_value = 'off' config.compute_test_value = 'off'
try: try:
...@@ -245,7 +245,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -245,7 +245,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
config.compute_test_value = ctv_backup config.compute_test_value = ctv_backup
def test2_err_bounds1(self): def test2_err_bounds1(self):
n = self.shared((numpy.ones((2, 3), dtype=self.dtype) * 5)) n = self.shared((np.ones((2, 3), dtype=self.dtype) * 5))
t = n[4:5, 3] t = n[4:5, 3]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
old_stderr = sys.stderr old_stderr = sys.stderr
...@@ -257,107 +257,107 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -257,107 +257,107 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
sys.stderr = old_stderr sys.stderr = old_stderr
def test2_ok_elem(self): def test2_ok_elem(self):
n = self.shared(numpy.arange(6, dtype=self.dtype).reshape((2, 3))) n = self.shared(np.arange(6, dtype=self.dtype).reshape((2, 3)))
t = n[0, 2] t = n[0, 2]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == ()) self.assertTrue(tval.shape == ())
self.assertTrue(numpy.all(tval == 2)) self.assertTrue(np.all(tval == 2))
def test2_ok_row(self): def test2_ok_row(self):
n = self.shared(numpy.arange(6, dtype=self.dtype).reshape((2, 3))) n = self.shared(np.arange(6, dtype=self.dtype).reshape((2, 3)))
t = n[1] t = n[1]
self.assertFalse(any(n.type.broadcastable)) self.assertFalse(any(n.type.broadcastable))
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (3,)) self.assertTrue(tval.shape == (3,))
self.assertTrue(numpy.all(tval == [3, 4, 5])) self.assertTrue(np.all(tval == [3, 4, 5]))
def test2_ok_col(self): def test2_ok_col(self):
n = self.shared(numpy.arange(6, dtype=self.dtype).reshape((2, 3))) n = self.shared(np.arange(6, dtype=self.dtype).reshape((2, 3)))
t = n[:, 0] t = n[:, 0]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
self.assertFalse(any(n.type.broadcastable)) self.assertFalse(any(n.type.broadcastable))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2,)) self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.all(tval == [0, 3])) self.assertTrue(np.all(tval == [0, 3]))
def test2_ok_rows_finite(self): def test2_ok_rows_finite(self):
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3))) n = self.shared(np.arange(12, dtype=self.dtype).reshape((4, 3)))
t = n[1:3, 0] t = n[1:3, 0]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2,)) self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.all(tval == [3, 6])) self.assertTrue(np.all(tval == [3, 6]))
def test2_ok_cols_infinite(self): def test2_ok_cols_infinite(self):
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3))) n = self.shared(np.arange(12, dtype=self.dtype).reshape((4, 3)))
t = n[1, 2:] t = n[1, 2:]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (1,)) self.assertTrue(tval.shape == (1,))
self.assertTrue(numpy.all(tval == 5)) self.assertTrue(np.all(tval == 5))
def test2_ok_strided(self): def test2_ok_strided(self):
n = self.shared(numpy.arange(20, dtype=self.dtype).reshape((4, 5))) n = self.shared(np.arange(20, dtype=self.dtype).reshape((4, 5)))
t = n[1:4:2, 1:5:2] t = n[1:4:2, 1:5:2]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2, 2)) self.assertTrue(tval.shape == (2, 2))
self.assertTrue(numpy.all(tval == [[6, 8], [16, 18]])) self.assertTrue(np.all(tval == [[6, 8], [16, 18]]))
def test3_ok_mat(self): def test3_ok_mat(self):
n = self.shared(numpy.arange(24, dtype=self.dtype).reshape((2, 3, 4))) n = self.shared(np.arange(24, dtype=self.dtype).reshape((2, 3, 4)))
t = n[0, 0, 0] t = n[0, 0, 0]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == ()) self.assertTrue(tval.shape == ())
self.assertTrue(numpy.all(tval == 0)) self.assertTrue(np.all(tval == 0))
def test_long(self): def test_long(self):
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3))) n = self.shared(np.arange(12, dtype=self.dtype).reshape((4, 3)))
t = n[L(1):L(4):L(2), L(1)] t = n[L(1):L(4):L(2), L(1)]
self.assertTrue(isinstance(t.owner.op, Subtensor)) self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t) tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (2,)) self.assertTrue(tval.shape == (2,))
self.assertTrue(numpy.all(tval == [4, 10])) self.assertTrue(np.all(tval == [4, 10]))
def test_long_too_big(self): def test_long_too_big(self):
# Currently, we cast Python longs to int64 when used for indexing. # Currently, we cast Python longs to int64 when used for indexing.
# This test checks that using a long that does not fit raises an error. # This test checks that using a long that does not fit raises an error.
n = self.shared(numpy.arange(12, dtype=self.dtype).reshape((4, 3))) n = self.shared(np.arange(12, dtype=self.dtype).reshape((4, 3)))
self.assertRaises(Exception, lambda: n[:L(2 ** 63)]) self.assertRaises(Exception, lambda: n[:L(2 ** 63)])
def test_list_slice(self): def test_list_slice(self):
x = theano.tensor.arange(100).reshape((5, 5, 4)) x = theano.tensor.arange(100).reshape((5, 5, 4))
res = x[[slice(1, -1)] * x.ndim].eval() res = x[[slice(1, -1)] * x.ndim].eval()
x = numpy.arange(100).reshape((5, 5, 4)) x = np.arange(100).reshape((5, 5, 4))
numpy.allclose(res, x[[slice(1, -1)] * x.ndim]) np.allclose(res, x[[slice(1, -1)] * x.ndim])
def test_slice_symbol(self): def test_slice_symbol(self):
x = self.shared(numpy.random.rand(5, 4).astype(self.dtype)) x = self.shared(np.random.rand(5, 4).astype(self.dtype))
y = self.shared(numpy.random.rand(1, 2, 3).astype(self.dtype)) y = self.shared(np.random.rand(1, 2, 3).astype(self.dtype))
o = x[:y.shape[0], None, :] o = x[:y.shape[0], None, :]
f = theano.function([], o, mode=self.mode) f = theano.function([], o, mode=self.mode)
ret = f() ret = f()
assert ret.shape == (1, 1, 4) assert ret.shape == (1, 1, 4)
def test_ellipsis(self): def test_ellipsis(self):
numpy_n = numpy.arange(24, dtype=self.dtype).reshape((2, 3, 4)) numpy_n = np.arange(24, dtype=self.dtype).reshape((2, 3, 4))
n = self.shared(numpy_n) n = self.shared(numpy_n)
test_cases = [ test_cases = [
(0, Subtensor, self.sub, numpy.index_exp[...]), (0, Subtensor, self.sub, np.index_exp[...]),
(1, Subtensor, self.sub, numpy.index_exp[..., 1]), (1, Subtensor, self.sub, np.index_exp[..., 1]),
(1, Subtensor, self.sub, numpy.index_exp[1, ...]), (1, Subtensor, self.sub, np.index_exp[1, ...]),
(1, Subtensor, self.sub, numpy.index_exp[..., 1, 2, 3]), (1, Subtensor, self.sub, np.index_exp[..., 1, 2, 3]),
(1, Subtensor, self.sub, numpy.index_exp[1, ..., 2, 3]), (1, Subtensor, self.sub, np.index_exp[1, ..., 2, 3]),
(1, Subtensor, self.sub, numpy.index_exp[1, 2, 3, ...]), (1, Subtensor, self.sub, np.index_exp[1, 2, 3, ...]),
(3, DimShuffle, self.dimshuffle, (3, DimShuffle, self.dimshuffle,
numpy.index_exp[..., [0, 2, 3]]), np.index_exp[..., [0, 2, 3]]),
(1, DimShuffle, self.dimshuffle, (1, DimShuffle, self.dimshuffle,
numpy.index_exp[numpy.newaxis, ...]), np.index_exp[np.newaxis, ...]),
(1, AdvancedSubtensor, self.adv_sub, (1, AdvancedSubtensor, self.adv_sub,
numpy.index_exp[..., numpy.newaxis, [1, 2]])] np.index_exp[..., np.newaxis, [1, 2]])]
for length, op_type, op_type_opt, slice_ in test_cases: for length, op_type, op_type_opt, slice_ in test_cases:
numpy_tval = numpy_n[slice_] numpy_tval = numpy_n[slice_]
...@@ -379,9 +379,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -379,9 +379,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
interaction with the Subtensor Op (which has no support of its own for interaction with the Subtensor Op (which has no support of its own for
newaxis). newaxis).
""" """
newaxis = numpy.newaxis newaxis = np.newaxis
n = self.shared(numpy.arange(24, dtype=self.dtype).reshape((2, 3, 4))) n = self.shared(np.arange(24, dtype=self.dtype).reshape((2, 3, 4)))
assert n.ndim == 3 assert n.ndim == 3
n4 = n[newaxis, :, :, :] n4 = n[newaxis, :, :, :]
...@@ -405,15 +405,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -405,15 +405,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
vs1, vn3, vn4 = theano.function([s], [s1, n3, n4])(-2.0) vs1, vn3, vn4 = theano.function([s], [s1, n3, n4])(-2.0)
assert numpy.all(vs1 == [-2.0]) assert np.all(vs1 == [-2.0])
assert numpy.all(vn3 == assert np.all(vn3 ==
numpy.arange(24)[newaxis, :, newaxis]) np.arange(24)[newaxis, :, newaxis])
assert numpy.all(vn4 == assert np.all(vn4 ==
numpy.arange(24).reshape((2, 3, 4))[:, :, :, newaxis]) np.arange(24).reshape((2, 3, 4))[:, :, :, newaxis])
def test_grad_1d(self): def test_grad_1d(self):
subi = 0 subi = 0
data = numpy.asarray(rand(2, 3), dtype=self.dtype) data = np.asarray(rand(2, 3), dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
z = scal.constant(subi).astype('int32') z = scal.constant(subi).astype('int32')
t = n[z:, z] t = n[z:, z]
...@@ -425,15 +425,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -425,15 +425,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.ignore_topo)] self.ignore_topo)]
if not self.fast_compile: if not self.fast_compile:
assert len(topo_) == 6 assert len(topo_) == 6
assert numpy.sum([isinstance(node.op, self.inc_sub) assert np.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1 for node in topo_]) == 1
assert numpy.sum([isinstance(node.op, self.sub) assert np.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1 for node in topo_]) == 1
gval = f() gval = f()
good = numpy.zeros_like(data) good = np.zeros_like(data)
good[subi:, subi] = numpy.exp(data[subi:, subi]) good[subi:, subi] = np.exp(data[subi:, subi])
self.assertTrue(numpy.allclose(gval, good), (gval, good)) self.assertTrue(np.allclose(gval, good), (gval, good))
def test_grad_2d_inc_set_subtensor(self): def test_grad_2d_inc_set_subtensor(self):
for n_shape, m_shape in [ for n_shape, m_shape in [
...@@ -444,11 +444,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -444,11 +444,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
]: ]:
for op in [inc_subtensor, set_subtensor]: for op in [inc_subtensor, set_subtensor]:
subi = 2 subi = 2
data = numpy.asarray(rand(*n_shape), dtype=self.dtype) data = np.asarray(rand(*n_shape), dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
z = scal.constant(subi) z = scal.constant(subi)
m = matrix('m', dtype=self.dtype) m = matrix('m', dtype=self.dtype)
mv = numpy.asarray(rand(*m_shape), dtype=self.dtype) mv = np.asarray(rand(*m_shape), dtype=self.dtype)
t = op(n[:z, :z], m) t = op(n[:z, :z], m)
gn, gm = theano.tensor.grad(theano.tensor.sum(t), [n, m]) gn, gm = theano.tensor.grad(theano.tensor.sum(t), [n, m])
...@@ -456,7 +456,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -456,7 +456,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
utt.verify_grad(lambda nn: op(nn[:z, :z], mv), [data]) utt.verify_grad(lambda nn: op(nn[:z, :z], mv), [data])
def test_grad_0d(self): def test_grad_0d(self):
data = numpy.asarray(rand(2, 3), dtype=self.dtype) data = np.asarray(rand(2, 3), dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
t = n[1, 0] t = n[1, 0]
gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n) gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n)
...@@ -466,15 +466,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -466,15 +466,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
if not isinstance(node.op, self.ignore_topo)] if not isinstance(node.op, self.ignore_topo)]
if not self.fast_compile: if not self.fast_compile:
assert_equal(len(topo_), 6) assert_equal(len(topo_), 6)
assert numpy.sum([isinstance(node.op, self.inc_sub) assert np.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1 for node in topo_]) == 1
assert numpy.sum([isinstance(node.op, self.sub) assert np.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1 for node in topo_]) == 1
gval = f() gval = f()
good = numpy.zeros_like(data) good = np.zeros_like(data)
good[1, 0] = numpy.exp(data[1, 0]) good[1, 0] = np.exp(data[1, 0])
self.assertTrue(numpy.allclose(gval, good), (gval, good)) self.assertTrue(np.allclose(gval, good), (gval, good))
def test_ok_list(self): def test_ok_list(self):
for data, idx in [(rand(4), [1, 0]), for data, idx in [(rand(4), [1, 0]),
...@@ -492,7 +492,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -492,7 +492,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
(rand(4, 2, 3), (rand(4, 2, 3),
theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])), theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])),
]: ]:
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
t = n[idx] t = n[idx]
...@@ -505,7 +505,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -505,7 +505,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
else: else:
good = data[idx.data] good = data[idx.data]
self.assertTrue(val.ndim == data.ndim) self.assertTrue(val.ndim == data.ndim)
self.assertTrue(numpy.allclose(val, good), (val, good)) self.assertTrue(np.allclose(val, good), (val, good))
# Test reuse of output memory # Test reuse of output memory
if type(self.adv_sub1) == tensor.AdvancedSubtensor1: if type(self.adv_sub1) == tensor.AdvancedSubtensor1:
...@@ -524,7 +524,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -524,7 +524,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
gn = theano.grad(t.sum(), n) gn = theano.grad(t.sum(), n)
g = self.function([], gn, op=self.adv_incsub1) g = self.function([], gn, op=self.adv_incsub1)
utt.verify_grad(lambda m: m[[1, 3]], utt.verify_grad(lambda m: m[[1, 3]],
[numpy.random.rand(5, 5).astype(self.dtype)]) [np.random.rand(5, 5).astype(self.dtype)])
g() g()
utt.verify_grad(lambda m: m[idx], utt.verify_grad(lambda m: m[idx],
[data]) [data])
...@@ -533,21 +533,21 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -533,21 +533,21 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
data = rand(4, 2, 3) data = rand(4, 2, 3)
idx = [2, 2, 0, 0, 1, 1] idx = [2, 2, 0, 0, 1, 1]
n = self.shared(data) n = self.shared(data)
t = n[self.shared(numpy.asarray(idx).astype('int64'))[::2]] t = n[self.shared(np.asarray(idx).astype('int64'))[::2]]
self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor1)) self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor1))
val = self.eval_output_and_check(t, op_type=self.adv_sub1, length=2) val = self.eval_output_and_check(t, op_type=self.adv_sub1, length=2)
utt.assert_allclose(data[idx[::2]], val) utt.assert_allclose(data[idx[::2]], val)
def test_err_invalid_list(self): def test_err_invalid_list(self):
n = self.shared(numpy.asarray(5, dtype=self.dtype)) n = self.shared(np.asarray(5, dtype=self.dtype))
self.assertRaises(TypeError, n.__getitem__, [0, 0]) self.assertRaises(TypeError, n.__getitem__, [0, 0])
def test_err_invalid_2list_dtype(self): def test_err_invalid_2list_dtype(self):
n = self.shared(numpy.ones((3, 3), dtype=self.dtype) * 5) n = self.shared(np.ones((3, 3), dtype=self.dtype) * 5)
self.assertRaises(TypeError, n.__getitem__, ([0., 0], [1, 1])) self.assertRaises(TypeError, n.__getitem__, ([0., 0], [1, 1]))
def test_err_bound_list(self): def test_err_bound_list(self):
n = self.shared(numpy.ones((2, 3), dtype=self.dtype) * 5) n = self.shared(np.ones((2, 3), dtype=self.dtype) * 5)
l = lvector() l = lvector()
t = n[l] t = n[l]
# We test again AdvancedSubtensor1 as we transfer data to the cpu. # We test again AdvancedSubtensor1 as we transfer data to the cpu.
...@@ -557,7 +557,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -557,7 +557,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# the grad # the grad
g = self.function([l], g = self.function([l],
inc_subtensor(t, numpy.asarray([[1.]], self.dtype)), inc_subtensor(t, np.asarray([[1.]], self.dtype)),
op=self.adv_incsub1) op=self.adv_incsub1)
for shp in [[0, 4], [0, -3], [-10]]: for shp in [[0, 4], [0, -3], [-10]]:
...@@ -565,7 +565,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -565,7 +565,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertRaises(IndexError, g, shp) self.assertRaises(IndexError, g, shp)
def test_adv_sub1_broadcast(self): def test_adv_sub1_broadcast(self):
v = numpy.arange(3, dtype=self.dtype).reshape((1, 3)) v = np.arange(3, dtype=self.dtype).reshape((1, 3))
n = self.shared(v * 5, broadcastable=(True, False)) n = self.shared(v * 5, broadcastable=(True, False))
idx = tensor.lvector() idx = tensor.lvector()
t = n[idx] t = n[idx]
...@@ -579,10 +579,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -579,10 +579,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(isinstance(topo_[0].op, self.adv_sub1)) self.assertTrue(isinstance(topo_[0].op, self.adv_sub1))
f_0 = f([0]) f_0 = f([0])
self.assertTrue(f_0.shape == (1, 3)) self.assertTrue(f_0.shape == (1, 3))
self.assertTrue(numpy.allclose(f_0, v * 5)) self.assertTrue(np.allclose(f_0, v * 5))
f_00 = f([0, 0]) f_00 = f([0, 0])
self.assertTrue(f_00.shape == (2, 3)) self.assertTrue(f_00.shape == (2, 3))
self.assertTrue(numpy.allclose(f_00, v * 5)) self.assertTrue(np.allclose(f_00, v * 5))
self.assertRaises(IndexError, f, [0, 1]) self.assertRaises(IndexError, f, [0, 1])
# Test the gradient # Test the gradient
...@@ -591,30 +591,30 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -591,30 +591,30 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
g = self.function([idx], gn, op=self.adv_incsub1) g = self.function([idx], gn, op=self.adv_incsub1)
g_0 = g([0]) g_0 = g([0])
self.assertTrue(g_0.shape == (1, 3)) self.assertTrue(g_0.shape == (1, 3))
self.assertTrue(numpy.allclose(g_0, 1)) self.assertTrue(np.allclose(g_0, 1))
g_00 = g([0, 0]) g_00 = g([0, 0])
self.assertTrue(g_00.shape == (1, 3)) self.assertTrue(g_00.shape == (1, 3))
self.assertTrue(numpy.allclose(g_00, 2)) self.assertTrue(np.allclose(g_00, 2))
utt.verify_grad(lambda m: m[[1, 3]], utt.verify_grad(lambda m: m[[1, 3]],
[numpy.random.rand(5, 5).astype(self.dtype)]) [np.random.rand(5, 5).astype(self.dtype)])
def fun(x, y): def fun(x, y):
return advanced_inc_subtensor1(x, y, [1, 3]) return advanced_inc_subtensor1(x, y, [1, 3])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype), utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2, 5).astype(self.dtype)]) np.random.rand(2, 5).astype(self.dtype)])
def fun(x, y): def fun(x, y):
return advanced_set_subtensor1(x, y, [1, 3]) return advanced_set_subtensor1(x, y, [1, 3])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype), utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2, 5).astype(self.dtype)]) np.random.rand(2, 5).astype(self.dtype)])
# test set_subtensor broadcast # test set_subtensor broadcast
self.dtype = 'float32' self.dtype = 'float32'
x = tensor.tensor4('x', dtype=self.dtype) x = tensor.tensor4('x', dtype=self.dtype)
indexes = theano.shared(numpy.int32([1, 2, 3, 4])) indexes = theano.shared(np.int32([1, 2, 3, 4]))
W = self.shared(numpy.random.random( W = self.shared(np.random.random(
(10, 10, 3, 3)).astype(self.dtype)) (10, 10, 3, 3)).astype(self.dtype))
h = x + W h = x + W
...@@ -625,11 +625,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -625,11 +625,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
N = 3 N = 3
f = self.function([x], g, op=self.adv_incsub1, N=N) f = self.function([x], g, op=self.adv_incsub1, N=N)
f(numpy.random.random((10, 10, 3, 3)).astype(self.dtype)) f(np.random.random((10, 10, 3, 3)).astype(self.dtype))
def test_adv_sub1_idx_broadcast(self): def test_adv_sub1_idx_broadcast(self):
# The idx can be a broadcastable vector. # The idx can be a broadcastable vector.
ones = numpy.ones((4, 3), dtype=self.dtype) ones = np.ones((4, 3), dtype=self.dtype)
n = self.shared(ones * 5) n = self.shared(ones * 5)
idx = tensor.TensorType(dtype='int64', broadcastable=(True,))() idx = tensor.TensorType(dtype='int64', broadcastable=(True,))()
assert idx.type.broadcastable == (True,) assert idx.type.broadcastable == (True,)
...@@ -644,7 +644,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -644,7 +644,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(isinstance(topo_[0].op, self.adv_sub1)) self.assertTrue(isinstance(topo_[0].op, self.adv_sub1))
f_0 = f([0]) f_0 = f([0])
self.assertTrue(f_0.shape == (1, 3)) self.assertTrue(f_0.shape == (1, 3))
self.assertTrue(numpy.allclose(f_0, 5)) self.assertTrue(np.allclose(f_0, 5))
# Test the gradient # Test the gradient
c = t.sum() c = t.sum()
...@@ -652,15 +652,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -652,15 +652,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
g = self.function([idx], gn, op=self.adv_incsub1) g = self.function([idx], gn, op=self.adv_incsub1)
g_0 = g([0]) g_0 = g([0])
self.assertTrue(g_0.shape == (4, 3)) self.assertTrue(g_0.shape == (4, 3))
self.assertTrue(numpy.allclose(g_0[0], 1)) self.assertTrue(np.allclose(g_0[0], 1))
self.assertTrue(numpy.allclose(g_0[1:], 0)) self.assertTrue(np.allclose(g_0[1:], 0))
@attr('slow') @attr('slow')
def test_shape_i_const(self): def test_shape_i_const(self):
# Each axis is treated independently by shape_i/shape operators # Each axis is treated independently by shape_i/shape operators
mode_opt = self.mode.including("fast_run") mode_opt = self.mode.including("fast_run")
data = self.shared(numpy.array(numpy.arange(5), dtype=self.dtype)) data = self.shared(np.array(np.arange(5), dtype=self.dtype))
for start in [None] + [-8, -5, -1, 0, 1, 5, 8]: for start in [None] + [-8, -5, -1, 0, 1, 5, 8]:
outs = [] outs = []
shapes = [] shapes = []
...@@ -673,7 +673,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -673,7 +673,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
op=self.ops, N=0) op=self.ops, N=0)
t_shapes = f() t_shapes = f()
for t_shape, shape in zip(t_shapes, shapes): for t_shape, shape in zip(t_shapes, shapes):
assert numpy.all(t_shape == shape) assert np.all(t_shape == shape)
assert tensor.Subtensor not in [x.op assert tensor.Subtensor not in [x.op
for x in f.maker.fgraph.toposort()] for x in f.maker.fgraph.toposort()]
...@@ -682,7 +682,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -682,7 +682,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
mode_opt = self.mode.including("fast_run") mode_opt = self.mode.including("fast_run")
v_data = numpy.array(numpy.arange(5), dtype=self.dtype) v_data = np.array(np.arange(5), dtype=self.dtype)
t_data = self.shared(v_data) t_data = self.shared(v_data)
start = tensor.iscalar('b') start = tensor.iscalar('b')
stop = tensor.iscalar('e') stop = tensor.iscalar('e')
...@@ -697,8 +697,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -697,8 +697,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-3, -1, 2, 5]: for step in [-3, -1, 2, 5]:
assert numpy.all(f(start, stop, step) == assert np.all(f(start, stop, step) ==
v_data[start:stop:step].shape) v_data[start:stop:step].shape)
def test_slice_canonical_form_0(self): def test_slice_canonical_form_0(self):
start = tensor.iscalar('b') start = tensor.iscalar('b')
...@@ -713,15 +713,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -713,15 +713,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(start, stop, step, length) out = f(start, stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:step] v_out = a[start:stop:step]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_1(self): def test_slice_canonical_form_1(self):
stop = tensor.iscalar('e') stop = tensor.iscalar('e')
...@@ -735,14 +735,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -735,14 +735,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(stop, step, length) out = f(stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[:stop:step] v_out = a[:stop:step]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_2(self): def test_slice_canonical_form_2(self):
start = tensor.iscalar('b') start = tensor.iscalar('b')
...@@ -756,14 +756,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -756,14 +756,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(start, step, length) out = f(start, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:step] v_out = a[start:None:step]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_3(self): def test_slice_canonical_form_3(self):
start = tensor.iscalar('b') start = tensor.iscalar('b')
...@@ -777,14 +777,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -777,14 +777,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, stop, length) out = f(start, stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:None] v_out = a[start:stop:None]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_4(self): def test_slice_canonical_form_4(self):
step = tensor.iscalar('s') step = tensor.iscalar('s')
...@@ -797,13 +797,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -797,13 +797,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for step in [-6, -3, -1, 2, 5]: for step in [-6, -3, -1, 2, 5]:
out = f(step, length) out = f(step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:None:step] v_out = a[None:None:step]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_5(self): def test_slice_canonical_form_5(self):
start = tensor.iscalar('b') start = tensor.iscalar('b')
...@@ -816,13 +816,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -816,13 +816,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, length) out = f(start, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:None] v_out = a[start:None:None]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_6(self): def test_slice_canonical_form_6(self):
stop = tensor.iscalar('e') stop = tensor.iscalar('e')
...@@ -835,20 +835,20 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -835,20 +835,20 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops) tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5 length = 5
a = numpy.arange(length) a = np.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]: for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(stop, length) out = f(stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]] t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:stop:None] v_out = a[None:stop:None]
assert numpy.all(t_out == v_out) assert np.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape) assert np.all(t_out.shape == v_out.shape)
def grad_list_(self, idxs, data): def grad_list_(self, idxs, data):
n = self.shared(data) n = self.shared(data)
for idx in idxs: for idx in idxs:
# Should stay on the cpu. # Should stay on the cpu.
idx_ = _shared(numpy.asarray(idx)) idx_ = _shared(np.asarray(idx))
t = n[idx_] t = n[idx_]
gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n) gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n)
f = self.function([], [gn, gn.shape], op=self.adv_incsub1) f = self.function([], [gn, gn.shape], op=self.adv_incsub1)
...@@ -861,14 +861,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -861,14 +861,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for node in topo]) for node in topo])
assert any([isinstance(node.op, self.adv_sub1) for node in topo]) assert any([isinstance(node.op, self.adv_sub1) for node in topo])
gval, gshape = f() gval, gshape = f()
good = numpy.zeros_like(data) good = np.zeros_like(data)
# don't work when the same index is used many time # don't work when the same index is used many time
# good[idx] += numpy.exp(data[idx]) # good[idx] += np.exp(data[idx])
for i in idx: for i in idx:
good[i] += numpy.exp(data[i]) good[i] += np.exp(data[i])
self.assertTrue(gval.ndim == data.ndim) self.assertTrue(gval.ndim == data.ndim)
self.assertTrue(numpy.allclose(gval, good), (gval, good)) self.assertTrue(np.allclose(gval, good), (gval, good))
self.assertTrue(numpy.allclose(gshape, data.shape)) self.assertTrue(np.allclose(gshape, data.shape))
def fct(t): def fct(t):
return theano.tensor.sum(t[idx_]) return theano.tensor.sum(t[idx_])
...@@ -916,7 +916,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -916,7 +916,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
@attr('slow') @attr('slow')
def test_grad_list(self): def test_grad_list(self):
data = rand(4) data = rand(4)
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
idxs = [[i] for i in range(data.shape[0])] idxs = [[i] for i in range(data.shape[0])]
for i in range(data.shape[0]): for i in range(data.shape[0]):
for j in range(0, data.shape[0], 2): for j in range(0, data.shape[0], 2):
...@@ -924,11 +924,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -924,11 +924,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
data = rand(4, 3) data = rand(4, 3)
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
data = rand(4, 3, 2) data = rand(4, 3, 2)
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data) self.grad_list_(idxs, data)
def test_shape_list(self): def test_shape_list(self):
...@@ -938,12 +938,12 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -938,12 +938,12 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
(rand(4, 2, 3), [0, 3]), (rand(4, 2, 3), [0, 3]),
(rand(4, 2, 3), [3, 3, 1, 2, 2, ]), (rand(4, 2, 3), [3, 3, 1, 2, 2, ]),
]: ]:
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
t = n[idx] t = n[idx]
f = self.function([], t.shape, op=self.ops, N=0, N_fast=1) f = self.function([], t.shape, op=self.ops, N=0, N_fast=1)
val = f() val = f()
self.assertTrue(numpy.allclose(val, data[idx].shape)) self.assertTrue(np.allclose(val, data[idx].shape))
def test_grad_advanced_inc_subtensor(self): def test_grad_advanced_inc_subtensor(self):
def inc_slice(*s): def inc_slice(*s):
...@@ -958,40 +958,40 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -958,40 +958,40 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# vector # vector
utt.verify_grad( utt.verify_grad(
inc_slice(slice(2, 4, None)), inc_slice(slice(2, 4, None)),
(numpy.asarray([0, 1, 2, 3, 4, 5.]), numpy.asarray([9, 9.]),)) (np.asarray([0, 1, 2, 3, 4, 5.]), np.asarray([9, 9.]),))
# matrix # matrix
utt.verify_grad( utt.verify_grad(
inc_slice(slice(1, 2, None), slice(None, None, None)), inc_slice(slice(1, 2, None), slice(None, None, None)),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]), (np.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray([[9, 9.]]),)) np.asarray([[9, 9.]]),))
# single element # single element
utt.verify_grad( utt.verify_grad(
inc_slice(2, 1), inc_slice(2, 1),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]), numpy.asarray(9.),)) (np.asarray([[0, 1], [2, 3], [4, 5.]]), np.asarray(9.),))
def test_inc_and_set_subtensor(self): def test_inc_and_set_subtensor(self):
""" """
Test increment and set with broadcast Test increment and set with broadcast
""" """
X = self.shared(numpy.ones((9, 9)).astype(self.dtype)) X = self.shared(np.ones((9, 9)).astype(self.dtype))
y = set_subtensor(X[1::, 1::], 0) y = set_subtensor(X[1::, 1::], 0)
f = self.function([], [y], f = self.function([], [y],
op=self.inc_sub, op=self.inc_sub,
N=1) N=1)
out = f() out = f()
res = numpy.ones((9, 9)) res = np.ones((9, 9))
res[1::, 1::] = 0 res[1::, 1::] = 0
assert numpy.allclose(out, res) assert np.allclose(out, res)
def test_advanced1_inc_and_set(self): def test_advanced1_inc_and_set(self):
""" """
Test advanced increment and set. Test advanced increment and set.
""" """
rng = numpy.random.RandomState(seed=utt.fetch_seed()) rng = np.random.RandomState(seed=utt.fetch_seed())
all_inputs_var = [] all_inputs_var = []
all_inputs_num = [] all_inputs_num = []
all_outputs_var = [] all_outputs_var = []
...@@ -1001,9 +1001,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1001,9 +1001,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for inplace in (False, True): for inplace in (False, True):
for data_shape in ((10,), (4, 5), (1, 2, 3), (4, 5, 6, 7)): for data_shape in ((10,), (4, 5), (1, 2, 3), (4, 5, 6, 7)):
data_n_dims = len(data_shape) data_n_dims = len(data_shape)
data_size = numpy.product(data_shape) data_size = np.product(data_shape)
# Corresponding numeric variable. # Corresponding numeric variable.
data_num_init = numpy.arange(data_size, dtype=self.dtype) data_num_init = np.arange(data_size, dtype=self.dtype)
data_num_init = data_num_init.reshape(data_shape) data_num_init = data_num_init.reshape(data_shape)
inc_shapes = [data_shape[i:] inc_shapes = [data_shape[i:]
for i in xrange(0, len(data_shape) + 1)] for i in xrange(0, len(data_shape) + 1)]
...@@ -1031,7 +1031,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1031,7 +1031,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Corresponding numeric variable. # Corresponding numeric variable.
# If set_instead_of_inc, we want to avoid repeating # If set_instead_of_inc, we want to avoid repeating
# indices, as the order is not guaranteed. # indices, as the order is not guaranteed.
idx_num = rng.choice(numpy.arange(data_shape[0]), idx_num = rng.choice(np.arange(data_shape[0]),
n_to_inc, n_to_inc,
replace=(not set_instead_of_inc)) replace=(not set_instead_of_inc))
idx_num = idx_num.astype('int64') idx_num = idx_num.astype('int64')
...@@ -1049,7 +1049,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1049,7 +1049,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# The param dtype is needed when inc_shape is empty. # The param dtype is needed when inc_shape is empty.
# By default, it would return a float and rng.uniform # By default, it would return a float and rng.uniform
# with NumPy 1.10 will raise a Deprecation warning. # with NumPy 1.10 will raise a Deprecation warning.
inc_size = numpy.product(inc_shape, dtype='int') inc_size = np.product(inc_shape, dtype='int')
# Corresponding numeric variable. # Corresponding numeric variable.
inc_num = rng.uniform(size=inc_size).astype(self.dtype) inc_num = rng.uniform(size=inc_size).astype(self.dtype)
inc_num = inc_num.reshape(inc_shape) inc_num = inc_num.reshape(inc_shape)
...@@ -1100,7 +1100,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1100,7 +1100,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Ensure calling `f` will not alter `data_num`. # Ensure calling `f` will not alter `data_num`.
data_num = data_num.copy() data_num = data_num.copy()
f_out = f(data_num.copy(), idx_num, inc_num) f_out = f(data_num.copy(), idx_num, inc_num)
assert numpy.allclose(f_out, data_copy) assert np.allclose(f_out, data_copy)
if not inplace: if not inplace:
# Sanity check: `data_num` should be intact. # Sanity check: `data_num` should be intact.
assert (data_num == data_num_init).all() assert (data_num == data_num_init).all()
...@@ -1121,12 +1121,12 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1121,12 +1121,12 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for params, f_out, output_num in izip(all_params, f_outs, all_outputs_num): for params, f_out, output_num in izip(all_params, f_outs, all_outputs_num):
# NB: if this assert fails, it will probably be easier to debug if # NB: if this assert fails, it will probably be easier to debug if
# you enable the debug code above. # you enable the debug code above.
assert numpy.allclose(f_out, output_num), (params, f_out, output_num) assert np.allclose(f_out, output_num), (params, f_out, output_num)
def test_adv_constant_arg(self): def test_adv_constant_arg(self):
# Test case provided (and bug detected, gh-607) by John Salvatier # Test case provided (and bug detected, gh-607) by John Salvatier
m = matrix('m') m = matrix('m')
gv = numpy.array([0, 1, 3]) gv = np.array([0, 1, 3])
g = theano.tensor.constant(gv) g = theano.tensor.constant(gv)
i = theano.tensor.lvector('i') i = theano.tensor.lvector('i')
...@@ -1156,8 +1156,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1156,8 +1156,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0 m1_ref[:, idx] = 0
m2_ref[:, idx] += 1 m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref) assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref) assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
def test_adv1_inc_sub_notlastdim_2didx(self): def test_adv1_inc_sub_notlastdim_2didx(self):
# Test that taking 1-dimensional advanced indexing # Test that taking 1-dimensional advanced indexing
...@@ -1181,8 +1181,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1181,8 +1181,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0 m1_ref[:, idx] = 0
m2_ref[:, idx] += 1 m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref) assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref) assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
def test_adv1_inc_sub_notlastdim_1_2dval_broadcast(self): def test_adv1_inc_sub_notlastdim_1_2dval_broadcast(self):
# Test that taking 1-dimensional advanced indexing # Test that taking 1-dimensional advanced indexing
...@@ -1202,8 +1202,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1202,8 +1202,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for i, shp_i, shp_v in zip(sym_i, shape_i, shape_val): for i, shp_i, shp_v in zip(sym_i, shape_i, shape_val):
sub_m = m[:, i] sub_m = m[:, i]
m1 = set_subtensor(sub_m, numpy.zeros(shp_v)) m1 = set_subtensor(sub_m, np.zeros(shp_v))
m2 = inc_subtensor(sub_m, numpy.ones(shp_v)) m2 = inc_subtensor(sub_m, np.ones(shp_v))
f = theano.function([m, i], [m1, m2]) f = theano.function([m, i], [m1, m2])
m_val = rand(3, 5) m_val = rand(3, 5)
...@@ -1216,8 +1216,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1216,8 +1216,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0 m1_ref[:, idx] = 0
m2_ref[:, idx] += 1 m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref) assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref) assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
finally: finally:
config.warn.inc_set_subtensor1 = orig_warn config.warn.inc_set_subtensor1 = orig_warn
...@@ -1239,8 +1239,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1239,8 +1239,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
config.warn.inc_set_subtensor1 = False config.warn.inc_set_subtensor1 = False
for i, shp_i, shp_v in zip(sym_i, shape_i, shape_val): for i, shp_i, shp_v in zip(sym_i, shape_i, shape_val):
sub_m = m[:, i] sub_m = m[:, i]
m1 = set_subtensor(sub_m, numpy.zeros(shp_v)) m1 = set_subtensor(sub_m, np.zeros(shp_v))
m2 = inc_subtensor(sub_m, numpy.ones(shp_v)) m2 = inc_subtensor(sub_m, np.ones(shp_v))
f = theano.function([m, i], [m1, m2]) f = theano.function([m, i], [m1, m2])
m_val = rand(3, 5) m_val = rand(3, 5)
...@@ -1256,15 +1256,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin): ...@@ -1256,15 +1256,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0 m1_ref[:, idx] = 0
m2_ref[:, idx] += 1 m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref) assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref) assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
finally: finally:
config.warn.inc_set_subtensor1 = orig_warn config.warn.inc_set_subtensor1 = orig_warn
def test_take(self): def test_take(self):
a = tensor.matrix() a = tensor.matrix()
f = theano.function([a], a.take(0, axis=-1), allow_input_downcast=True) f = theano.function([a], a.take(0, axis=-1), allow_input_downcast=True)
f(numpy.random.normal(0, 1, (30, 4))) f(np.random.normal(0, 1, (30, 4)))
class TestIncSubtensor1(unittest.TestCase): class TestIncSubtensor1(unittest.TestCase):
...@@ -1272,7 +1272,7 @@ class TestIncSubtensor1(unittest.TestCase): ...@@ -1272,7 +1272,7 @@ class TestIncSubtensor1(unittest.TestCase):
# also tests set_subtensor # also tests set_subtensor
def setUp(self): def setUp(self):
self.rng = numpy.random.RandomState(seed=utt.fetch_seed()) self.rng = np.random.RandomState(seed=utt.fetch_seed())
self.s = tensor.iscalar() self.s = tensor.iscalar()
self.v = tensor.fvector() self.v = tensor.fvector()
...@@ -1298,7 +1298,7 @@ class TestIncSubtensor1(unittest.TestCase): ...@@ -1298,7 +1298,7 @@ class TestIncSubtensor1(unittest.TestCase):
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True) f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
aval = f([.4, .9, .1], [1, 2]) aval = f([.4, .9, .1], [1, 2])
assert numpy.allclose(aval, [.4, 0.9, 0.1]) assert np.allclose(aval, [.4, 0.9, 0.1])
def test_1d_inc_adv_selection(self): def test_1d_inc_adv_selection(self):
a = inc_subtensor(self.v[self.adv1q], self.v[self.adv1q]) a = inc_subtensor(self.v[self.adv1q], self.v[self.adv1q])
...@@ -1306,7 +1306,7 @@ class TestIncSubtensor1(unittest.TestCase): ...@@ -1306,7 +1306,7 @@ class TestIncSubtensor1(unittest.TestCase):
assert a.type == self.v.type assert a.type == self.v.type
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True) f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
aval = f([.4, .9, .1], [1, 2]) aval = f([.4, .9, .1], [1, 2])
assert numpy.allclose(aval, [.4, 1.8, 0.2]) assert np.allclose(aval, [.4, 1.8, 0.2])
def test_1d_inc_adv_selection_w_broadcasting(self): def test_1d_inc_adv_selection_w_broadcasting(self):
a = inc_subtensor(self.v[self.adv1q], 3.0) a = inc_subtensor(self.v[self.adv1q], 3.0)
...@@ -1314,7 +1314,7 @@ class TestIncSubtensor1(unittest.TestCase): ...@@ -1314,7 +1314,7 @@ class TestIncSubtensor1(unittest.TestCase):
assert a.type == self.v.type assert a.type == self.v.type
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True) f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
aval = f([.4, .9, .1], [1, 2]) aval = f([.4, .9, .1], [1, 2])
assert numpy.allclose(aval, [.4, 3.9, 3.1]) assert np.allclose(aval, [.4, 3.9, 3.1])
def test_assigning_matrix_to_vector_selection(self): def test_assigning_matrix_to_vector_selection(self):
self.assertRaises(TypeError, self.assertRaises(TypeError,
...@@ -1327,7 +1327,7 @@ class TestIncSubtensor1(unittest.TestCase): ...@@ -1327,7 +1327,7 @@ class TestIncSubtensor1(unittest.TestCase):
f = theano.function([self.m, idx], a2) f = theano.function([self.m, idx], a2)
mval = self.rng.random_sample((4, 10)) mval = self.rng.random_sample((4, 10))
idxval = numpy.array([[1, 2], [3, 2]]) idxval = np.array([[1, 2], [3, 2]])
a2val = f(mval, idxval) a2val = f(mval, idxval)
utt.assert_allclose(a2val[0], mval[0]) utt.assert_allclose(a2val[0], mval[0])
...@@ -1427,7 +1427,7 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1427,7 +1427,7 @@ class TestAdvancedSubtensor(unittest.TestCase):
(rand(2, 4, 3), (rand(2, 4, 3),
theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])), theano.tensor.constant([3, 3, 1, 1, 2, 2, 0, 0])),
]: ]:
data = numpy.asarray(data, dtype=self.dtype) data = np.asarray(data, dtype=self.dtype)
n = self.shared(data) n = self.shared(data)
t = n[0, idx] t = n[0, idx]
...@@ -1439,7 +1439,7 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1439,7 +1439,7 @@ class TestAdvancedSubtensor(unittest.TestCase):
else: else:
good = data[0, idx.data] good = data[0, idx.data]
self.assertTrue(val.ndim == data.ndim - 1) self.assertTrue(val.ndim == data.ndim - 1)
self.assertTrue(numpy.allclose(val, good), (val, good)) self.assertTrue(np.allclose(val, good), (val, good))
def test_inc_adv_subtensor_w_matrix(self): def test_inc_adv_subtensor_w_matrix(self):
subt = self.v[self.ix2] subt = self.v[self.ix2]
...@@ -1450,13 +1450,13 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1450,13 +1450,13 @@ class TestAdvancedSubtensor(unittest.TestCase):
mode=self.mode) mode=self.mode)
aval = f([.4, .9, .1], [[1, 2], aval = f([.4, .9, .1], [[1, 2],
[1, 2]]) [1, 2]])
assert numpy.allclose(aval, [.4, .9 * 3, .1 * 3]) assert np.allclose(aval, [.4, .9 * 3, .1 * 3])
def test_adv_subtensor_w_int_and_matrix(self): def test_adv_subtensor_w_int_and_matrix(self):
subt = self.ft4[0, :, self.ix2, :] subt = self.ft4[0, :, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode) f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32') ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]]) ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v) aval = f(ft4v, ix2v)
rval = ft4v[0, :, ix2v, :] rval = ft4v[0, :, ix2v, :]
utt.assert_allclose(rval, aval) utt.assert_allclose(rval, aval)
...@@ -1464,8 +1464,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1464,8 +1464,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_none_and_matrix(self): def test_adv_subtensor_w_none_and_matrix(self):
subt = self.ft4[:, None, :, self.ix2, :] subt = self.ft4[:, None, :, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode) f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32') ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]]) ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v) aval = f(ft4v, ix2v)
rval = ft4v[:, None, :, ix2v, :] rval = ft4v[:, None, :, ix2v, :]
utt.assert_allclose(rval, aval) utt.assert_allclose(rval, aval)
...@@ -1473,8 +1473,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1473,8 +1473,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_slice_and_matrix(self): def test_adv_subtensor_w_slice_and_matrix(self):
subt = self.ft4[:, 0:1, self.ix2, :] subt = self.ft4[:, 0:1, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode) f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32') ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]]) ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v) aval = f(ft4v, ix2v)
rval = ft4v[:, 0:1, ix2v, :] rval = ft4v[:, 0:1, ix2v, :]
utt.assert_allclose(rval, aval) utt.assert_allclose(rval, aval)
...@@ -1482,8 +1482,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1482,8 +1482,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_matrix_and_int(self): def test_adv_subtensor_w_matrix_and_int(self):
subt = self.ft4[:, :, self.ix2, 0] subt = self.ft4[:, :, self.ix2, 0]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode) f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32') ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]]) ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v) aval = f(ft4v, ix2v)
rval = ft4v[:, :, ix2v, 0] rval = ft4v[:, :, ix2v, 0]
utt.assert_allclose(rval, aval) utt.assert_allclose(rval, aval)
...@@ -1491,8 +1491,8 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1491,8 +1491,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_matrix_and_none(self): def test_adv_subtensor_w_matrix_and_none(self):
subt = self.ft4[:, :, self.ix2, None, :] subt = self.ft4[:, :, self.ix2, None, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode) f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32') ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]]) ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v) aval = f(ft4v, ix2v)
rval = ft4v[:, :, ix2v, None, :] rval = ft4v[:, :, ix2v, None, :]
utt.assert_allclose(rval, aval) utt.assert_allclose(rval, aval)
...@@ -1513,10 +1513,10 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1513,10 +1513,10 @@ class TestAdvancedSubtensor(unittest.TestCase):
[.5, .3, .15]], [.5, .3, .15]],
[1, 2, 1], [1, 2, 1],
[0, 1, 0]) [0, 1, 0])
assert numpy.allclose(aval, assert np.allclose(aval,
[[.4, .9, .1], [[.4, .9, .1],
[5 * 3, 6, 7], [5 * 3, 6, 7],
[.5, .3 * 2, .15]]), aval [.5, .3 * 2, .15]]), aval
def test_inc_adv_subtensor_with_broadcasting(self): def test_inc_adv_subtensor_with_broadcasting(self):
if not config.cxx: if not config.cxx:
...@@ -1535,11 +1535,11 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1535,11 +1535,11 @@ class TestAdvancedSubtensor(unittest.TestCase):
[1, 2, 1], [1, 2, 1],
[0, 1, 0], [0, 1, 0],
2.1) 2.1)
assert numpy.allclose(aval, assert np.allclose(aval,
[[.4, .9, .1], [[.4, .9, .1],
[5 + 2.1 * 2, 6, 7], [5 + 2.1 * 2, 6, 7],
[.5, .3 + 2.1, .15]]), aval [.5, .3 + 2.1, .15]]), aval
assert numpy.allclose(gval, 3.0), gval assert np.allclose(gval, 3.0), gval
def test_inc_adv_subtensor1_with_broadcasting(self): def test_inc_adv_subtensor1_with_broadcasting(self):
if not config.cxx: if not config.cxx:
...@@ -1557,11 +1557,11 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1557,11 +1557,11 @@ class TestAdvancedSubtensor(unittest.TestCase):
[.5, .3, .15]], [.5, .3, .15]],
[0, 1, 0], [0, 1, 0],
2.1) 2.1)
assert numpy.allclose(aval, assert np.allclose(aval,
[[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2], [[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2],
[5 + 2.1, 6 + 2.1, 7 + 2.1], [5 + 2.1, 6 + 2.1, 7 + 2.1],
[.5, .3, .15]]), aval [.5, .3, .15]]), aval
assert numpy.allclose(gval, 9.0), gval assert np.allclose(gval, 9.0), gval
def test_inc_adv_subtensor_with_index_broadcasting(self): def test_inc_adv_subtensor_with_index_broadcasting(self):
if not config.cxx: if not config.cxx:
...@@ -1578,14 +1578,14 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1578,14 +1578,14 @@ class TestAdvancedSubtensor(unittest.TestCase):
[0, 2, 0], [0, 2, 0],
[[0, 1, 0], [[0, 1, 0],
[2, 2, 2]]) [2, 2, 2]])
assert numpy.allclose(aval, assert np.allclose(aval,
[[.4 + 2 * 2.1, .9, .1 + 2 * 2.1], [[.4 + 2 * 2.1, .9, .1 + 2 * 2.1],
[5, 6, 7], [5, 6, 7],
[.5, .3 + 2.1, .15 + 2.1]]), aval [.5, .3 + 2.1, .15 + 2.1]]), aval
def test_advanced_indexing(self): def test_advanced_indexing(self):
# tests advanced indexing in Theano for 2D and 3D tensors # tests advanced indexing in Theano for 2D and 3D tensors
rng = numpy.random.RandomState(utt.seed_rng()) rng = np.random.RandomState(utt.seed_rng())
a = rng.uniform(size=(3, 3)) a = rng.uniform(size=(3, 3))
b = theano.shared(a) b = theano.shared(a)
i = tensor.iscalar() i = tensor.iscalar()
...@@ -1607,24 +1607,24 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1607,24 +1607,24 @@ class TestAdvancedSubtensor(unittest.TestCase):
# Reported in https://github.com/Theano/Theano/issues/5674 # Reported in https://github.com/Theano/Theano/issues/5674
X = tensor.tensor3("X") X = tensor.tensor3("X")
xx = numpy.zeros((3, 2, 2), config.floatX) xx = np.zeros((3, 2, 2), config.floatX)
for i in range(3): for i in range(3):
for j in range(2): for j in range(2):
for k in range(2): for k in range(2):
xx[i, j, k] = 100 * i + 10 * j + k xx[i, j, k] = 100 * i + 10 * j + k
b_idx = numpy.zeros((2, 2), 'int32') b_idx = np.zeros((2, 2), 'int32')
b_idx[0, 1] = 1 b_idx[0, 1] = 1
b_idx[1, 1] = 2 b_idx[1, 1] = 2
r_idx = numpy.arange(xx.shape[1])[:, numpy.newaxis] r_idx = np.arange(xx.shape[1])[:, np.newaxis]
c_idx = numpy.arange(xx.shape[2])[numpy.newaxis, :] c_idx = np.arange(xx.shape[2])[np.newaxis, :]
out = X[b_idx, r_idx, c_idx].eval({X: xx}) out = X[b_idx, r_idx, c_idx].eval({X: xx})
utt.assert_allclose(out, xx[b_idx, r_idx, c_idx]) utt.assert_allclose(out, xx[b_idx, r_idx, c_idx])
def test_grad(self): def test_grad(self):
ones = numpy.ones((1, 3), dtype=self.dtype) ones = np.ones((1, 3), dtype=self.dtype)
n = self.shared(ones * 5, broadcastable=(True, False)) n = self.shared(ones * 5, broadcastable=(True, False))
idx = tensor.lvector() idx = tensor.lvector()
idx2 = tensor.lvector() idx2 = tensor.lvector()
...@@ -1632,17 +1632,17 @@ class TestAdvancedSubtensor(unittest.TestCase): ...@@ -1632,17 +1632,17 @@ class TestAdvancedSubtensor(unittest.TestCase):
self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor)) self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor))
utt.verify_grad(lambda m: m[[1, 3], [2, 4]], utt.verify_grad(lambda m: m[[1, 3], [2, 4]],
[numpy.random.rand(5, 5).astype(self.dtype)]) [np.random.rand(5, 5).astype(self.dtype)])
def fun(x, y): def fun(x, y):
return advanced_inc_subtensor(x, y, [1, 3], [2, 4]) return advanced_inc_subtensor(x, y, [1, 3], [2, 4])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype), utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2).astype(self.dtype)]) np.random.rand(2).astype(self.dtype)])
def fun(x, y): def fun(x, y):
return advanced_set_subtensor(x, y, [1, 3], [2, 4]) return advanced_set_subtensor(x, y, [1, 3], [2, 4])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype), utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2).astype(self.dtype)]) np.random.rand(2).astype(self.dtype)])
class TestInferShape(utt.InferShapeTester): class TestInferShape(utt.InferShapeTester):
......
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 theano import theano
from theano.tensor.utils import (hash_from_ndarray, shape_of_variables) from theano.tensor.utils import (hash_from_ndarray, shape_of_variables)
...@@ -9,22 +9,22 @@ from theano.tensor.utils import (hash_from_ndarray, shape_of_variables) ...@@ -9,22 +9,22 @@ from theano.tensor.utils import (hash_from_ndarray, shape_of_variables)
def test_hash_from_ndarray(): def test_hash_from_ndarray():
hashs = [] hashs = []
rng = numpy.random.rand(5, 5) rng = np.random.rand(5, 5)
for data in [-2, -1, 0, 1, 2, numpy.zeros((1, 5)), numpy.zeros((1, 6)), for data in [-2, -1, 0, 1, 2, np.zeros((1, 5)), np.zeros((1, 6)),
# Data buffer empty but different shapes # Data buffer empty but different shapes
numpy.zeros((1, 0)), numpy.zeros((2, 0)), np.zeros((1, 0)), np.zeros((2, 0)),
# Same data buffer and shapes but different strides # Same data buffer and shapes but different strides
numpy.arange(25).reshape(5, 5), np.arange(25).reshape(5, 5),
numpy.arange(25).reshape(5, 5).T, np.arange(25).reshape(5, 5).T,
# Same data buffer, shapes and strides but different dtypes # Same data buffer, shapes and strides but different dtypes
numpy.zeros((5, 5), dtype="uint32"), np.zeros((5, 5), dtype="uint32"),
numpy.zeros((5, 5), dtype="int32"), np.zeros((5, 5), dtype="int32"),
# Test slice # Test slice
rng, rng[1:], rng[:4], rng[1:3], rng[::2], rng[::-1] rng, rng[1:], rng[:4], rng[1:3], rng[::2], rng[::-1]
]: ]:
data = numpy.asarray(data) data = np.asarray(data)
hashs.append(hash_from_ndarray(data)) hashs.append(hash_from_ndarray(data))
assert len(set(hashs)) == len(hashs) assert len(set(hashs)) == len(hashs)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论