提交 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'
from collections import OrderedDict
import numpy
import numpy as np
import theano
import theano.tensor as T
......@@ -17,12 +17,12 @@ import theano.tensor as T
def gen_data():
# generate the dataset
train_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64'))
valid_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64'))
test_set = (numpy.asarray(numpy.random.rand(10000, 784), dtype='float32'),
numpy.asarray(numpy.random.rand(10000)*10, dtype='int64'))
train_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
np.asarray(np.random.rand(10000)*10, dtype='int64'))
valid_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
np.asarray(np.random.rand(10000)*10, dtype='int64'))
test_set = (np.asarray(np.random.rand(10000, 784), dtype='float32'),
np.asarray(np.random.rand(10000)*10, dtype='int64'))
def shared_dataset(data_xy):
""" Function that loads the dataset into shared variables
......@@ -33,8 +33,8 @@ def gen_data():
variable) would lead to a large decrease in performance.
"""
data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX))
shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX))
shared_x = theano.shared(np.asarray(data_x, 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
# therefore we will store the labels as ``floatX`` as well
# (``shared_y`` does exactly that). But during our computations
......@@ -79,7 +79,7 @@ class LogisticRegression(object):
"""
# 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')
# compute vector of class-membership probabilities in symbolic form
......@@ -129,7 +129,7 @@ class HiddenLayer(object):
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
:type input: theano.tensor.dmatrix
......@@ -151,9 +151,9 @@ class HiddenLayer(object):
# from -6./sqrt(n_in+n_hidden) and 6./sqrt(n_in+n_hidden)
# the output of uniform if converted using asarray to dtype
# theano.config.floatX so that the code is runable on GPU
W_values = numpy.asarray( rng.uniform( \
low=-numpy.sqrt(6./(n_in+n_out)), \
high=numpy.sqrt(6./(n_in+n_out)), \
W_values = np.asarray( rng.uniform( \
low=-np.sqrt(6./(n_in+n_out)), \
high=np.sqrt(6./(n_in+n_out)), \
size=(n_in, n_out)), dtype=theano.config.floatX)
self.W = theano.shared(value=W_values, name=name_prefix+'W')
......@@ -176,7 +176,7 @@ class MLP(object):
def __init__(self, rng, input, n_in, n_hidden, n_out):
"""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
:type input: theano.tensor.TensorType
......@@ -265,7 +265,7 @@ def test_mlp():
y = T.ivector('y') # the labels are presented as 1D vector of
# [int] labels
rng = numpy.random.RandomState(1234)
rng = np.random.RandomState(1234)
# construct the MLP class
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
from itertools import product as itertools_product
from unittest import TestCase
import numpy
import numpy as np
from numpy import (arange, array, common_type, complex64, complex128, float32,
float64, newaxis, shape, transpose, zeros)
from numpy.testing import assert_array_almost_equal
......@@ -42,7 +42,7 @@ def test_dot_eq():
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):
......@@ -58,22 +58,22 @@ class t_gemm(TestCase):
def _gemm(z, a, x, y, b):
assert a.shape == ()
assert b.shape == ()
return b * z + a * numpy.dot(x, y)
return b * z + a * np.dot(x, y)
@staticmethod
def rand(*args):
return numpy.random.rand(*args)
return np.random.rand(*args)
def cmp(self, z_, a_, x_, y_, b_):
for dtype in ['float32', 'float64', 'complex64', 'complex128']:
z = numpy.asarray(z_, dtype=dtype)
a = numpy.asarray(a_, dtype=dtype)
x = numpy.asarray(x_, dtype=dtype)
y = numpy.asarray(y_, dtype=dtype)
b = numpy.asarray(b_, dtype=dtype)
z = np.asarray(z_, dtype=dtype)
a = np.asarray(a_, dtype=dtype)
x = np.asarray(x_, dtype=dtype)
y = np.asarray(y_, dtype=dtype)
b = np.asarray(b_, dtype=dtype)
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()
tz, ta, tx, ty, tb = [as_tensor_variable(p).type()
for p in (z, a, x, y, b)]
......@@ -91,7 +91,7 @@ class t_gemm(TestCase):
elif z_orig.size == 0:
self.assertTrue(z.size == 0)
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, 'py')
......@@ -172,7 +172,7 @@ class t_gemm(TestCase):
a = T.matrix()
b = 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)
lr2 = T.constant(2).astype(config.floatX)
......@@ -331,7 +331,7 @@ class t_gemm(TestCase):
z, a, x, y, b = [theano._asarray(p, dtype=dt)
for p in (z, a, x, y, b)]
z_orig = z.copy()
z_after = numpy.zeros_like(z_orig)
z_after = np.zeros_like(z_orig)
for i in xrange(3):
z_after[:, :, i] = self._gemm(z[:, :, i], a,
x[:, :, i], y[:, :, i], b)
......@@ -388,7 +388,7 @@ class t_as_scalar(TestCase):
def test0(self):
"""Test that it works on scalar constants"""
a = T.constant(2.5)
b = T.constant(numpy.asarray([[[0.5]]]))
b = T.constant(np.asarray([[[0.5]]]))
b2 = b.dimshuffle()
assert b2.ndim == 0
d_a = T.DimShuffle([], [])(a)
......@@ -403,7 +403,7 @@ class t_as_scalar(TestCase):
def test1(self):
"""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(T.DimShuffle([False], [0, 'x'])(a)))
......@@ -482,13 +482,13 @@ def just_gemm(i, o, ishapes=[(4, 3), (3, 5), (4, 5), (), ()],
# theano.printing.debugprint(f)
assert False, 'graphlen=%i>%i' % (graphlen, max_graphlen)
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[numpy.asarray(rng.randn(*sh), config.floatX)
rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes])
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[numpy.asarray(rng.randn(*sh), config.floatX)
rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[np.asarray(rng.randn(*sh), config.floatX)
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
if config.floatX == 'float32':
eps = 1.0e-6
......@@ -556,13 +556,13 @@ def test_gemm_opt_double_gemm():
# for node in g.maker.fgraph.apply_nodes:
# if node.op == gemm_inplace: raise Failure('gemm_inplace in graph')
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[numpy.asarray(rng.randn(*sh), config.floatX)
rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r0 = f(*[np.asarray(rng.randn(*sh), config.floatX)
for sh in ishapes])
rng = numpy.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[numpy.asarray(rng.randn(*sh), config.floatX)
rng = np.random.RandomState(unittest_tools.fetch_seed(234))
r1 = g(*[np.asarray(rng.randn(*sh), config.floatX)
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
if config.floatX == 'float32':
eps = 1.0e-6
......@@ -652,7 +652,7 @@ def test_upcasting_scalar_nogemm():
f = theano.function([w, v, t, alpha], rval)
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)
v = T.fmatrix('v')
......@@ -669,7 +669,7 @@ def test_upcasting_scalar_nogemm():
config.on_opt_error = on_opt_error
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)
......@@ -763,13 +763,13 @@ def test_gemm_unrolled():
"""
batch_size = 100
rep_size = 40
rng = numpy.random.RandomState([1, 2, 3])
rng = np.random.RandomState([1, 2, 3])
for num_rounds in range(1, 10):
W = sharedX(rng.randn(rep_size, rep_size), name='W')
V = sharedX(numpy.zeros((batch_size, rep_size)), name='V')
H = sharedX(numpy.zeros((batch_size, rep_size)), name='H')
G = sharedX(numpy.zeros((batch_size, rep_size)), name='G')
V = sharedX(np.zeros((batch_size, rep_size)), name='V')
H = sharedX(np.zeros((batch_size, rep_size)), name='H')
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_H = sharedX(rng.uniform(0, 1, (batch_size, rep_size)), name='init_H')
......@@ -844,7 +844,7 @@ def test_dot22():
else:
check = [isinstance(x.op, T.Dot) for x in topo]
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):
av = rng.uniform(size=a_shp).astype(dtype1)
......@@ -867,7 +867,7 @@ def test_dot22scalar():
# m = theano.compile.get_default_mode().including('local_dot_to_dot22',
# 'local_dot22_to_dot22scalar','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']:
a = T.matrix('a', dtype=dtype1)
for dtype2 in ['complex64', 'complex128']:
......@@ -1069,7 +1069,7 @@ def test_dot_w_self():
# normally be a gemm must not be because the output is aliased to
# one of the inputs.
A = shared(value=numpy.ones((2, 2)))
A = shared(value=np.ones((2, 2)))
B = T.matrix()
p = T.dot(A, A) * B
......@@ -1078,7 +1078,7 @@ def test_dot_w_self():
f = theano.function([B], p, updates=[(A, A - grad)])
# 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():
class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
def test_dot_vv(self):
''' Currently we generate a gemv for that case'''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32'))
w = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32'))
rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.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)
# Assert that the dot was optimized somehow
......@@ -1098,13 +1098,13 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True))
# 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):
''' Test vector dot matrix '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(numpy.array(rng.uniform(size=(2, 3)),
rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(np.array(rng.uniform(size=(2, 3)),
dtype='float32'))
f = theano.function([], theano.dot(v, m), mode=mode_blas_opt)
......@@ -1113,18 +1113,18 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True))
# 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
m.set_value(
m.get_value(borrow=True)[::-1, ::-1],
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):
''' Test matrix dot vector '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(numpy.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(numpy.array(rng.uniform(size=(3, 2)),
rng = np.random.RandomState(unittest_tools.fetch_seed())
v = theano.shared(np.array(rng.uniform(size=(2,)), dtype='float32'))
m = theano.shared(np.array(rng.uniform(size=(3, 2)),
dtype='float32'))
f = theano.function([], theano.dot(m, v), mode=mode_blas_opt)
......@@ -1133,29 +1133,29 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
self.assertFunctionContains1(f, Gemv(True))
# 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
m.set_value(
m.get_value(borrow=True)[::-1, ::-1],
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
def t_gemv1(m_shp):
''' test vector2+dot(matrix,vector1) '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(m_shp[1],)
rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(m_shp[1],)
), 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)
m = theano.shared(numpy.array(rng.uniform(size=m_shp),
m = theano.shared(np.array(rng.uniform(size=m_shp),
dtype='float32'))
f = theano.function([], v2 + theano.dot(m, v1), mode=mode_blas_opt)
# Assert they produce the same output
assert numpy.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig)
assert np.allclose(f(),
np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = f.maker.fgraph.toposort()
assert len(topo) == 1
assert isinstance(topo[0].op, Gemv)
......@@ -1167,8 +1167,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
g()
assert numpy.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig)
assert np.allclose(v2.get_value(),
np.dot(m.get_value(), v1.get_value()) + v2_orig)
topo = g.maker.fgraph.toposort()
assert len(topo) == 1
assert isinstance(topo[0].op, Gemv)
......@@ -1180,11 +1180,11 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
m.get_value(borrow=True)[::-1, ::-1],
borrow=True)
v2.set_value(v2_orig)
assert numpy.allclose(f(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig)
assert np.allclose(f(),
np.dot(m.get_value(), v1.get_value()) + v2_orig)
g()
assert numpy.allclose(v2.get_value(),
numpy.dot(m.get_value(), v1.get_value()) + v2_orig)
assert np.allclose(v2.get_value(),
np.dot(m.get_value(), v1.get_value()) + v2_orig)
@attr('slow')
def test_gemv1(self):
......@@ -1195,19 +1195,19 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
def test_gemv2(self):
''' test vector2+dot(vector1,matrix) '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(2,)),
rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(2,)),
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)
m = theano.shared(numpy.array(rng.uniform(size=(2, 3)),
m = theano.shared(np.array(rng.uniform(size=(2, 3)),
dtype='float32'))
f = theano.function([], v2 + theano.dot(v1, m), mode=mode_blas_opt)
# Assert they produce the same output
assert numpy.allclose(f(),
numpy.dot(v1.get_value(), m.get_value()) + v2.get_value())
assert np.allclose(f(),
np.dot(v1.get_value(), m.get_value()) + v2.get_value())
topo = f.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1
assert topo[-1].op.inplace == False
......@@ -1218,8 +1218,8 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
# Assert they produce the same output
g()
assert numpy.allclose(v2.get_value(),
numpy.dot(v1.get_value(), m.get_value()) + v2_orig)
assert np.allclose(v2.get_value(),
np.dot(v1.get_value(), m.get_value()) + v2_orig)
topo = g.maker.fgraph.toposort()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1
if config.mode != 'FAST_COMPILE':
......@@ -1230,38 +1230,38 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
m.get_value(borrow=True)[::-1, ::-1],
borrow=True)
v2.set_value(v2_orig)
assert numpy.allclose(f(),
numpy.dot(v1.get_value(), m.get_value()) + v2.get_value())
assert np.allclose(f(),
np.dot(v1.get_value(), m.get_value()) + v2.get_value())
g()
assert numpy.allclose(v2.get_value(),
numpy.dot(v1.get_value(), m.get_value()) + v2_orig)
assert np.allclose(v2.get_value(),
np.dot(v1.get_value(), m.get_value()) + v2_orig)
def test_gemv_broadcast(self):
''' test gemv with some broadcasted input '''
rng = numpy.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(numpy.array(rng.uniform(size=(2,)),
rng = np.random.RandomState(unittest_tools.fetch_seed())
v1 = theano.shared(np.array(rng.uniform(size=(2,)),
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)
m = theano.shared(numpy.array(rng.uniform(size=(1, 2)),
m = theano.shared(np.array(rng.uniform(size=(1, 2)),
dtype='float32'),
broadcastable=(True, False))
o = theano.dot(m, v1)
f = theano.function([], o + v2, mode=mode_blas_opt)
# Assert they produce the same output
assert numpy.allclose(
assert np.allclose(
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()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1
# call gemv directly for mixed broadcast pattern.
o = theano.tensor.blas.gemv_no_inplace(v2, 0.5, m, v1, 0.25)
f = theano.function([], o, mode=mode_blas_opt)
assert numpy.allclose(
assert np.allclose(
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()
assert sum(isinstance(node.op, Gemv) for node in topo) == 1
......@@ -1277,12 +1277,12 @@ class TestGemv(TestCase, unittest_tools.TestOptimizationMixin):
f = theano.function([A, x, y], z)
# Matrix value
A_val = numpy.ones((5, 3), dtype=config.floatX)
A_val = np.ones((5, 3), dtype=config.floatX)
# Different vector length
ones_3 = numpy.ones(3, dtype=config.floatX)
ones_4 = numpy.ones(4, dtype=config.floatX)
ones_5 = numpy.ones(5, dtype=config.floatX)
ones_6 = numpy.ones(6, dtype=config.floatX)
ones_3 = np.ones(3, dtype=config.floatX)
ones_4 = np.ones(4, dtype=config.floatX)
ones_5 = np.ones(5, dtype=config.floatX)
ones_6 = np.ones(6, dtype=config.floatX)
f(A_val, ones_3, ones_5)
f(A_val[::-1, ::-1], ones_3, ones_5)
......@@ -1322,7 +1322,7 @@ class BaseGemv(object):
shared = staticmethod(theano.shared)
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)
if self.dtype in [complex64, complex128]:
mult = array(1 + 1j, dtype=self.dtype)
......@@ -1652,7 +1652,7 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
return theano.function(inputs, outputs, self.mode, updates=updates)
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):
""" test local_gemm_to_ger opt"""
......@@ -1685,45 +1685,45 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
def test_outer(self):
f = self.function([self.x, self.y], T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger_destructive)
f(numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
def test_A_plus_outer(self):
f = self.function([self.A, self.x, self.y],
self.A + T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype),
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
def test_A_plus_scaled_outer(self):
f = self.function([self.A, self.x, self.y],
self.A + 0.1 * T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype),
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
def test_scaled_A_plus_scaled_outer(self):
f = self.function([self.A, self.x, self.y],
numpy.asarray(0.2, self.dtype) * self.A +
numpy.asarray(0.1, self.dtype) * T.outer(
np.asarray(0.2, self.dtype) * self.A +
np.asarray(0.1, self.dtype) * T.outer(
self.x, self.y))
# Why gemm? This make the graph simpler did we test that it
# make it faster?
self.assertFunctionContains(f, self.gemm)
f(numpy.random.rand(5, 4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(numpy.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(5).astype(self.dtype),
numpy.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype),
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
f(np.random.rand(5, 4).astype(self.dtype)[::-1, ::-1],
np.random.rand(5).astype(self.dtype),
np.random.rand(4).astype(self.dtype))
def given_dtype(self, dtype, M, N):
""" test corner case shape and dtype"""
......@@ -1731,12 +1731,12 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
f = self.function([self.A, self.x, self.y],
self.A + 0.1 * T.outer(self.x, self.y))
self.assertFunctionContains(f, self.ger)
f(numpy.random.rand(M, N).astype(self.dtype),
numpy.random.rand(M).astype(self.dtype),
numpy.random.rand(N).astype(self.dtype))
f(numpy.random.rand(M, N).astype(self.dtype)[::-1, ::-1],
numpy.random.rand(M).astype(self.dtype),
numpy.random.rand(N).astype(self.dtype))
f(np.random.rand(M, N).astype(self.dtype),
np.random.rand(M).astype(self.dtype),
np.random.rand(N).astype(self.dtype))
f(np.random.rand(M, N).astype(self.dtype)[::-1, ::-1],
np.random.rand(M).astype(self.dtype),
np.random.rand(N).astype(self.dtype))
def test_f32_0_0(self):
return self.given_dtype('float32', 0, 0)
......@@ -1769,19 +1769,19 @@ class TestGer(TestCase, unittest_tools.TestOptimizationMixin):
return self.given_dtype('complex128', 1, 9)
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], [],
updates=[(A, A + T.constant(0.1, dtype=self.dtype) *
T.outer(self.x, self.y))])
self.assertFunctionContains(f, self.ger_destructive)
f(numpy.random.rand(4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype))
f(np.random.rand(4).astype(self.dtype),
np.random.rand(5).astype(self.dtype))
A.set_value(
A.get_value(borrow=True, return_internal_type=True)[::-1, ::-1],
borrow=True)
f(numpy.random.rand(4).astype(self.dtype),
numpy.random.rand(5).astype(self.dtype))
f(np.random.rand(4).astype(self.dtype),
np.random.rand(5).astype(self.dtype))
class TestBlasStrides(TestCase):
......@@ -1789,13 +1789,13 @@ class TestBlasStrides(TestCase):
shared = staticmethod(tensor._shared)
mode = theano.compile.get_default_mode()
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):
return theano._asarray(self.rng.rand(*shape), dtype=self.dtype)
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)
cv = self.rand(*c_shp)
......@@ -1834,20 +1834,20 @@ class TestBlasStrides(TestCase):
c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True)
# 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])
f_nn()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_nt()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_tn()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_tt()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
def test_dot22(self):
self.cmp_dot22((3, 4), (4, 5))
......@@ -1865,10 +1865,10 @@ class TestBlasStrides(TestCase):
self.cmp_dot22((0, 0), (0, 0))
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)
cv = self.rand(*c_shp)
l = numpy.float32(0.2)
l = np.float32(0.2)
a = self.shared(av, 'a')
b = self.shared(bv, 'b')
......@@ -1904,20 +1904,20 @@ class TestBlasStrides(TestCase):
c_t.set_value(ct_dev.copy()[::c_step2, ::c_step1], borrow=True)
# 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])
f_nn()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_nt()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_tn()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
f_tt()
assert numpy.allclose(a.get_value(), a_n)
assert np.allclose(a.get_value(), a_n)
def test_dot22scalar(self):
self.cmp_dot22scalar((3, 4), (4, 5))
......@@ -1938,7 +1938,7 @@ class TestBlasStrides(TestCase):
av = self.rand(*a_shp)
bv = self.rand(*b_shp)
cv = self.rand(*c_shp)
l = numpy.float32(0.2)
l = np.float32(0.2)
a = self.shared(av, 'a')
b = self.shared(bv, 'b')
......@@ -1992,48 +1992,48 @@ class TestBlasStrides(TestCase):
# Numpy results
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]))
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)
# 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)
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)
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)
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)
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],
borrow=True)
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],
borrow=True)
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],
borrow=True)
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],
borrow=True)
f_ttt()
assert numpy.allclose(a_t.get_value(), at_n)
assert np.allclose(a_t.get_value(), at_n)
def test_gemm(self):
self.cmp_gemm((3, 5), (3, 4), (4, 5))
......@@ -2054,7 +2054,7 @@ class TestBlasStrides(TestCase):
av = self.rand(a_shp)
bv = self.rand(*b_shp)
cv = self.rand(c_shp)
l = numpy.float32(0.2)
l = np.float32(0.2)
a = self.shared(av, 'a')
b = self.shared(bv, 'b')
......@@ -2086,14 +2086,14 @@ class TestBlasStrides(TestCase):
c.set_value(c_dev.copy()[::c_step], borrow=True)
a_n = (av[::a_step]
+ l * numpy.dot(bv[::b_step1, ::b_step2],
+ l * np.dot(bv[::b_step1, ::b_step2],
cv[::c_step]))
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)
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):
self.cmp_gemv(3, (3, 5), 5)
......@@ -2109,7 +2109,7 @@ class TestBlasStrides(TestCase):
av = self.rand(*a_shp)
bv = self.rand(b_shp)
cv = self.rand(c_shp)
l = numpy.float32(0.2)
l = np.float32(0.2)
a = self.shared(av, 'a')
b = self.shared(bv, 'b')
......@@ -2142,13 +2142,13 @@ class TestBlasStrides(TestCase):
f_n()
n_n = (av[::a_step1, ::a_step2]
+ l * numpy.outer(bv[::b_step], cv[::c_step]))
assert numpy.allclose(a.get_value(), n_n), (a.get_value(), n_n)
+ l * np.outer(bv[::b_step], cv[::c_step]))
assert np.allclose(a.get_value(), n_n), (a.get_value(), n_n)
f_t()
n_t = (av.T[::a_step1, ::a_step2]
+ l * numpy.outer(bv[::b_step], cv[::c_step]).T)
assert numpy.allclose(a_t.get_value(), n_t),\
+ l * np.outer(bv[::b_step], cv[::c_step]).T)
assert np.allclose(a_t.get_value(), n_t),\
(a_t.get_value(), n_t)
def test_ger_strides(self):
......@@ -2163,9 +2163,9 @@ class TestBlasStrides(TestCase):
def test_gemm_non_contiguous(self):
"""test_gemm_non_contiguous: Test if GEMM works well with non-contiguous matrices."""
aval = numpy.ones((6, 2))
bval = numpy.ones((2, 7))
cval = numpy.arange(7) + numpy.arange(0, .6, .1)[:, numpy.newaxis]
aval = np.ones((6, 2))
bval = np.ones((2, 7))
cval = np.arange(7) + np.arange(0, .6, .1)[:, np.newaxis]
a = theano.shared(aval[:3], borrow=True)
b = theano.shared(bval[:, :5], borrow=True)
......@@ -2176,7 +2176,7 @@ class TestBlasStrides(TestCase):
f = theano.function([s], [], updates={c: upd_c})
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)
......@@ -2185,8 +2185,8 @@ class test_infer_shape(unittest_tools.InferShapeTester):
x, y = T.matrices('xy')
self._compile_and_check(
[x, y], [T.blas._dot22(x, y)],
[numpy.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX)],
[np.random.random((2, 3)).astype(config.floatX),
np.random.random((3, 4)).astype(config.floatX)],
T.blas.Dot22)
def test_dot22scalar(self):
......@@ -2194,9 +2194,9 @@ class test_infer_shape(unittest_tools.InferShapeTester):
a = T.scalar('a')
self._compile_and_check(
[x, y, a], [T.blas._dot22scalar(x, y, a)],
[numpy.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)],
[np.random.random((2, 3)).astype(config.floatX),
np.random.random((3, 4)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX)],
T.blas.Dot22Scalar)
def test_gemm(self):
......@@ -2205,11 +2205,11 @@ class test_infer_shape(unittest_tools.InferShapeTester):
b = T.scalar('b')
self._compile_and_check(
[x, y, a, z, b], [T.blas.gemm(z, a, x, y, b)],
[numpy.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX),
numpy.random.random((2, 4)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)],
[np.random.random((2, 3)).astype(config.floatX),
np.random.random((3, 4)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX),
np.random.random((2, 4)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX)],
T.blas.Gemm)
def test_gemv(self):
......@@ -2219,11 +2219,11 @@ class test_infer_shape(unittest_tools.InferShapeTester):
b = T.scalar('b')
self._compile_and_check(
[y, a, A, x, b], [T.blas.gemv(y, a, A, x, b)],
[numpy.random.random((2,)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX),
numpy.random.random((2, 3)).astype(config.floatX),
numpy.random.random((3,)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX)],
[np.random.random((2,)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX),
np.random.random((2, 3)).astype(config.floatX),
np.random.random((3,)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX)],
T.blas.Gemv)
def test_ger(self):
......@@ -2232,8 +2232,8 @@ class test_infer_shape(unittest_tools.InferShapeTester):
a = T.scalar('a')
self._compile_and_check(
[A, a, x, y], [T.blas.ger(A, a, x, y)],
[numpy.random.random((2, 3)).astype(config.floatX),
numpy.asarray(0.5, dtype=config.floatX),
numpy.random.random((2,)).astype(config.floatX),
numpy.random.random((3,)).astype(config.floatX)],
[np.random.random((2, 3)).astype(config.floatX),
np.asarray(0.5, dtype=config.floatX),
np.random.random((2,)).astype(config.floatX),
np.random.random((3,)).astype(config.floatX)],
T.blas.Ger)
from __future__ import absolute_import, print_function, division
import sys
import numpy
import numpy as np
import theano
import theano.tensor as tensor
from theano.tensor.blas_scipy import ScipyGer
......@@ -20,9 +20,9 @@ class TestScipyGer(TestCase, TestOptimizationMixin):
self.a = tensor.tensor(dtype=dtype, broadcastable=())
self.x = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.y = tensor.tensor(dtype=dtype, broadcastable=(False,))
self.Aval = numpy.ones((2, 3), dtype=dtype)
self.xval = numpy.asarray([1, 2], dtype=dtype)
self.yval = numpy.asarray([1.5, 2.7, 3.9], dtype=dtype)
self.Aval = np.ones((2, 3), dtype=dtype)
self.xval = np.asarray([1, 2], dtype=dtype)
self.yval = np.asarray([1.5, 2.7, 3.9], dtype=dtype)
if not theano.tensor.blas_scipy.have_fblas:
self.SkipTest()
......@@ -34,7 +34,7 @@ class TestScipyGer(TestCase, TestOptimizationMixin):
f(self.Aval[::-1, ::-1], self.xval[::-1], self.yval[::-1])
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):
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
import unittest
from six.moves import xrange
import theano
import numpy as np
from theano.tensor import *
from theano.tests import unittest_tools as utt
......@@ -12,30 +13,30 @@ class TestRealImag(unittest.TestCase):
def test0(self):
x = zvector()
rng = numpy.random.RandomState(23)
xval = numpy.asarray(list(numpy.complex(rng.randn(), rng.randn())
rng = np.random.RandomState(23)
xval = np.asarray(list(np.complex(rng.randn(), rng.randn())
for i in xrange(10)))
assert numpy.all(xval.real == theano.function([x], real(x))(xval))
assert numpy.all(xval.imag == theano.function([x], imag(x))(xval))
assert np.all(xval.real == theano.function([x], real(x))(xval))
assert np.all(xval.imag == theano.function([x], imag(x))(xval))
def test_on_real_input(self):
x = dvector()
rng = numpy.random.RandomState(23)
rng = np.random.RandomState(23)
xval = rng.randn(10)
numpy.all(0 == theano.function([x], imag(x))(xval))
numpy.all(xval == theano.function([x], real(x))(xval))
np.all(0 == theano.function([x], imag(x))(xval))
np.all(xval == theano.function([x], real(x))(xval))
x = imatrix()
xval = numpy.asarray(rng.randn(3, 3) * 100, dtype='int32')
numpy.all(0 == theano.function([x], imag(x))(xval))
numpy.all(xval == theano.function([x], real(x))(xval))
xval = np.asarray(rng.randn(3, 3) * 100, dtype='int32')
np.all(0 == theano.function([x], imag(x))(xval))
np.all(xval == theano.function([x], real(x))(xval))
def test_cast(self):
x = zvector()
self.assertRaises(TypeError, cast, x, 'int32')
def test_complex(self):
rng = numpy.random.RandomState(2333)
rng = np.random.RandomState(2333)
m = fmatrix()
c = complex(m[0], m[1])
assert c.type == cvector
......@@ -44,10 +45,10 @@ class TestRealImag(unittest.TestCase):
assert i.type == fvector
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)
assert numpy.all(rval == mval[0]), (rval, mval[0])
assert numpy.all(ival == mval[1]), (ival, mval[1])
assert np.all(rval == mval[0]), (rval, mval[0])
assert np.all(ival == mval[1]), (ival, mval[1])
@dec.skipif(True, "Complex grads not enabled, see #178")
def test_complex_grads(self):
......@@ -55,8 +56,8 @@ class TestRealImag(unittest.TestCase):
c = complex(m[0], m[1])
return .5 * real(c) + .9 * imag(c)
rng = numpy.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval])
@dec.skipif(True, "Complex grads not enabled, see #178")
......@@ -66,8 +67,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1])
return abs((ac)**2).sum()
rng = numpy.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
try:
utt.verify_grad(f, [aval])
except utt.verify_grad.E_grad as e:
......@@ -82,8 +83,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1])
return abs(ac).sum()
rng = numpy.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
try:
utt.verify_grad(f, [aval])
except utt.verify_grad.E_grad as e:
......@@ -98,8 +99,8 @@ class TestRealImag(unittest.TestCase):
ac = complex(a[0], a[1])
return abs((ac*b)**2).sum()
rng = numpy.random.RandomState(9333)
aval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
aval = np.asarray(rng.randn(2, 5))
bval = rng.randn(5)
try:
utt.verify_grad(f, [aval, bval])
......@@ -114,8 +115,8 @@ class TestRealImag(unittest.TestCase):
c = complex_from_polar(abs(m[0]), m[1])
return .5 * real(c) + .9 * imag(c)
rng = numpy.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval])
@dec.skipif(True, "Complex grads not enabled, see #178")
......@@ -124,6 +125,6 @@ class TestRealImag(unittest.TestCase):
c = complex(m[0], m[1])
return .5 * abs(c)
rng = numpy.random.RandomState(9333)
mval = numpy.asarray(rng.randn(2, 5))
rng = np.random.RandomState(9333)
mval = np.asarray(rng.randn(2, 5))
utt.verify_grad(f, [mval])
......@@ -3,7 +3,7 @@ from copy import copy
import unittest
import math
import numpy
import numpy as np
from nose.plugins.skip import SkipTest
from nose.tools import raises
from six.moves import xrange
......@@ -47,13 +47,13 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
x = self.type(self.dtype, ib)('x')
e = self.op(ib, shuffle)(x)
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
x = self.type(self.dtype, ib)('x')
e = self.op(ib, shuffle)(x)
f = copy(linker).accept(FunctionGraph([x],
[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
ib = [False, True, False]
......@@ -65,7 +65,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
x = self.type(self.dtype, ib)('x')
e = self.op(ib, (1, 2))(x)
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
xsh, shuffle, zsh = ((1, 1, 4), (0, 1, 2, 0), (1, 4))
......@@ -94,7 +94,7 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
((1,), ('x', 'x'))]:
ib = [(entry == 1) for entry in xsh]
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.op(ib, shuffle)(adtens)],
[adtens_val], self.op,
......@@ -102,50 +102,50 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
def test_too_big_rank(self):
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})
class test_reduce_axes(unittest.TestCase):
def test_sum_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.sum(a)
def test_mean_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.mean(a)
def test_max_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.max(a)
def test_min_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.min(a)
def test_argmax_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.argmax(a)
def test_var_axes(self):
axes = [None, 0, 1, [0, 1], numpy.array(1),
[numpy.array(0), numpy.array(1)]]
axes = [None, 0, 1, [0, 1], np.array(1),
[np.array(0), np.array(1)]]
for a in axes:
x = tensor.matrix()
x.var(a)
......@@ -166,12 +166,12 @@ class test_Broadcast(unittest.TestCase):
linkers = [gof.PerformLinker, gof.CLinker]
def rand_val(self, shp):
return numpy.asarray(numpy.random.rand(*shp),
dtype=theano.config.floatX)
return np.asarray(np.random.rand(*shp),
dtype=theano.config.floatX)
def rand_cval(self, shp):
return numpy.asarray(numpy.random.rand(*shp),
dtype=theano.config.floatX)
return np.asarray(np.random.rand(*shp),
dtype=theano.config.floatX)
def setUp(self):
unittest_tools.seed_rng()
......@@ -331,19 +331,19 @@ class test_Broadcast(unittest.TestCase):
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])
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):
out = identity
for i in range(a.size):
out = numpy.bitwise_and(a[i], out)
out = np.bitwise_and(a[i], 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):
......@@ -384,20 +384,20 @@ class test_CAReduce(unittest_tools.InferShapeTester):
tosum = list(range(len(xsh)))
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:
xv = numpy.asarray(xv, dtype=dtype)
xv = np.asarray(xv, dtype=dtype)
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 len(xsh) > 0:
xv = xv.flatten()
xv[0] = numpy.nan
xv[0] = np.nan
xv = xv.reshape(*xsh)
else:
xv = numpy.asarray(numpy.nan, dtype=dtype)
xv = np.asarray(np.nan, dtype=dtype)
zv = xv
if pre_scalar_op is not None:
zv = Elemwise(scalar_op=pre_scalar_op)(x).eval({x: xv})
......@@ -415,48 +415,48 @@ class test_CAReduce(unittest_tools.InferShapeTester):
tosum = tuple(axis2)
if tensor_op == tensor.all:
for axis in reversed(sorted(tosum)):
zv = numpy.all(zv, axis)
zv = np.all(zv, axis)
if len(tosum) == 0:
zv = zv != 0
elif tensor_op == tensor.any:
for axis in reversed(sorted(tosum)):
zv = numpy.any(zv, axis)
zv = np.any(zv, axis)
if len(tosum) == 0:
zv = zv != 0
elif scalar_op == scalar.add:
for axis in reversed(sorted(tosum)):
zv = numpy.add.reduce(zv, axis)
zv = np.add.reduce(zv, axis)
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)
elif scalar_op == scalar.mul:
for axis in reversed(sorted(tosum)):
zv = numpy.multiply.reduce(zv, axis)
zv = np.multiply.reduce(zv, axis)
elif scalar_op == scalar.maximum:
try:
for axis in reversed(sorted(tosum)):
zv = numpy.maximum.reduce(zv, axis)
zv = np.maximum.reduce(zv, axis)
except ValueError:
numpy_raised = True
elif scalar_op == scalar.minimum:
try:
for axis in reversed(sorted(tosum)):
zv = numpy.minimum.reduce(zv, axis)
zv = np.minimum.reduce(zv, axis)
except ValueError:
numpy_raised = True
elif scalar_op == scalar.or_:
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_:
for axis in reversed(sorted(tosum)):
zv = reduce_bitwise_and(zv, axis, dtype=dtype)
elif scalar_op == scalar.xor:
# There is no identity value for the xor function
# So we can't support shape of dimensions 0.
if numpy.prod(zv.shape) == 0:
if np.prod(zv.shape) == 0:
continue
for axis in reversed(sorted(tosum)):
zv = numpy.bitwise_xor.reduce(zv, axis)
zv = np.bitwise_xor.reduce(zv, axis)
else:
raise Exception(
"Test for CAReduce with scalar_op %s not implemented" %
......@@ -482,7 +482,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
try:
f_xv = f(xv)
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))
except NotImplementedError:
# GpuCAReduce don't implement all cases when size is 0
......@@ -498,7 +498,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
f = copy(linker).accept(FunctionGraph([x],
[e.shape])).make_function()
if not(scalar_op in [scalar.maximum, scalar.minimum] and
((xsh == () or numpy.prod(xsh) == 0))):
((xsh == () or np.prod(xsh) == 0))):
try:
assert all(f(xv) == zv.shape)
except NotImplementedError:
......@@ -579,7 +579,7 @@ class test_CAReduce(unittest_tools.InferShapeTester):
x = pre_scalar_op(x)
if tosum is None:
tosum = list(range(len(xsh)))
xv = numpy.asarray(numpy.random.rand(*xsh), dtype=dtype)
xv = np.asarray(np.random.rand(*xsh), dtype=dtype)
d = {}
if pre_scalar_op is not None:
xv = x.eval({x.owner.inputs[0]: xv})
......@@ -608,8 +608,8 @@ class test_Prod(unittest.TestCase):
# including zeros, as the case with zeros is important
# (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]],
dtype='float32')
x_val = np.asarray([[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]],
dtype='float32')
# now with verify_grad
unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode)
......@@ -623,8 +623,8 @@ class test_Prod(unittest.TestCase):
def test_verify_grad_with_zeros(self):
# including zeros, as the case with zeros is important
# (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.]],
dtype='float32')
x_val = np.asarray([[1., 2., 3.], [0., 5., 6.], [0., 0., 9.]],
dtype='float32')
x = theano.tensor.dmatrix()
# sanity check
......@@ -635,7 +635,7 @@ class test_Prod(unittest.TestCase):
# p2 = Prod(axis=1)(x2)
# fn = theano.function([x, x2], [p - p2], mode=self.mode)
# 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]])
# print(fn(x_val, x2_val))
# fn2 = theano.function([x], [theano.tensor.grad(p.sum(), x)],
......@@ -643,7 +643,7 @@ class test_Prod(unittest.TestCase):
# print("real grad")
# print(fn2(x_val))
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
unittest_tools.verify_grad(Prod(axis=1), [x_val], mode=self.mode)
......@@ -665,25 +665,25 @@ class test_Prod(unittest.TestCase):
@attr('slow')
def test_prod_no_zeros_in_input(self):
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)
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)
g = theano.grad(pwz, x)
gg = theano.grad(g.sum(), x)
fn = theano.function([x], g, mode=self.mode)
assert numpy.allclose(fn(x_val),
[[362880., 181440., 120960.],
[90720., 72576., 60480.],
[51840., 45360., 40320.]])
assert np.allclose(fn(x_val),
[[362880., 181440., 120960.],
[90720., 72576., 60480.],
[51840., 45360., 40320.]])
fn = theano.function([x], gg, mode=self.mode)
assert numpy.allclose(fn(x_val),
[[663696., 422568., 301872.],
[233964., 190800., 161016.],
[139248., 122652., 109584.]])
assert np.allclose(fn(x_val),
[[663696., 422568., 301872.],
[233964., 190800., 161016.],
[139248., 122652., 109584.]])
unittest_tools.verify_grad(Prod(axis=1, no_zeros_in_input=True),
[x_val],
mode=self.mode)
......@@ -697,14 +697,14 @@ class test_Prod(unittest.TestCase):
def test_prod_without_zeros(self):
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)
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)
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)
def test_prod_without_zeros_grad(self):
......@@ -716,33 +716,33 @@ class test_Prod(unittest.TestCase):
@attr('slow')
def test_other_grad_tests(self):
x = theano.tensor.dmatrix()
x_val1 = numpy.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]],
dtype='float32')
x_val2 = numpy.array([[1, 2, 0], [0, 5, 6], [7, 8, 9], [9, 10, 0]],
dtype='float32')
rng = rng = numpy.random.RandomState(43)
x_val1 = np.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]],
dtype='float32')
x_val2 = np.array([[1, 2, 0], [0, 5, 6], [7, 8, 9], [9, 10, 0]],
dtype='float32')
rng = rng = np.random.RandomState(43)
p = Prod(axis=1)
grad_p = theano.tensor.grad(p(x).sum(), x)
grad_fn = theano.function([x], grad_p, mode=self.mode)
assert numpy.allclose(
assert np.allclose(
grad_fn(x_val1),
[[6., 3., 2.], [30., 0., 0.], [0., 0., 0.]])
assert numpy.allclose(
assert np.allclose(
grad_fn(x_val2),
[[0., 0., 2.], [30., 0., 0.], [72., 63., 56.], [0., 0., 90.]])
p_axis0 = Prod(axis=0)
grad_p_axis0 = theano.tensor.grad(p_axis0(x).sum(), x)
grad_fn_axis0 = theano.function([x], grad_p_axis0, mode=self.mode)
assert numpy.allclose(
assert np.allclose(
grad_fn_axis0(x_val2),
[[0., 400., 0.], [63., 160., 0.], [0., 100., 0.], [0., 80., 0.]])
tensor.verify_grad(p, [x_val1], rng=rng, mode=self.mode)
def test_mul_without_zeros_zeros(self):
a = numpy.zeros((3, 3))
a = np.zeros((3, 3))
x = theano.tensor.dmatrix()
......@@ -763,13 +763,13 @@ class test_Prod(unittest.TestCase):
class test_IsInf_IsNan(unittest.TestCase):
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,
1,
numpy.nan,
numpy.inf,
-numpy.inf,
[numpy.nan, numpy.inf, -numpy.inf, 0, 1, -1],
np.nan,
np.inf,
-np.inf,
[np.nan, np.inf, -np.inf, 0, 1, -1],
]]
self.scalar = tensor.scalar()
self.vector = tensor.vector()
......@@ -784,7 +784,7 @@ class test_IsInf_IsNan(unittest.TestCase):
theano_isfunc = theano.function([input],
getattr(tensor, isfunc)(input),
mode=self.mode)
numpy_isfunc = getattr(numpy, isfunc)
numpy_isfunc = getattr(np, isfunc)
for x in self.test_vals:
if ((x.ndim == 0 and input is not self.scalar) or
(x.ndim == 1 and input is not self.vector)):
......@@ -830,7 +830,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
data = numpy.random.rand(3, 4) * 10
data = np.random.rand(3, 4) * 10
data = data.astype(dtype)
f(data)
......@@ -859,7 +859,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], (topo,
dtype)
data = numpy.random.rand(3, 4) * 10
data = np.random.rand(3, 4) * 10
data = data.astype(dtype)
f(data)
......@@ -887,7 +887,7 @@ class T_reduce_dtype(unittest.TestCase):
topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], \
(topo, output_dtype)
data = numpy.random.rand(3, 4) * 10
data = np.random.rand(3, 4) * 10
data = data.astype(input_dtype)
if output_dtype == 'float16' and method == 'prod':
# We will likely get something infinite,
......@@ -943,17 +943,17 @@ class T_reduce_dtype(unittest.TestCase):
def test_reduce_precision(self):
# Check that the default accumulator precision is sufficient
for method in self.methods:
x = theano.shared(numpy.asarray([1e8, 1, -1e8],
dtype='float32'))
x = theano.shared(np.asarray([1e8, 1, -1e8],
dtype='float32'))
s = getattr(x, method)()
f = theano.function([], s, mode=self.mode)
topo = f.maker.fgraph.toposort()
assert [n for n in topo if isinstance(n.op, self.op)], topo
s_val = f()
# 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)()
assert numpy.allclose(s_val, ret), (s_val, ret)
assert np.allclose(s_val, ret), (s_val, ret)
class T_mean_dtype(unittest.TestCase):
......@@ -971,7 +971,7 @@ class T_mean_dtype(unittest.TestCase):
else:
assert m.dtype == dtype, (m, m.dtype, dtype)
f = theano.function([x], m)
data = numpy.random.rand(3, 4) * 10
data = np.random.rand(3, 4) * 10
data = data.astype(dtype)
f(data)
......@@ -1005,7 +1005,7 @@ class T_mean_dtype(unittest.TestCase):
input_dtype != sum_dtype):
continue
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)
f(data)
# Check that we can take the gradient, when implemented
......@@ -1026,11 +1026,11 @@ class T_mean_dtype(unittest.TestCase):
def test_mean_precision(self):
# 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()
f = theano.function([], m)
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):
......@@ -1077,7 +1077,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
if 'complex' in dtype:
continue
f = theano.function([x], p)
data = numpy.random.rand(2, 3) * 3
data = np.random.rand(2, 3) * 3
data = data.astype(dtype)
f(data)
......@@ -1100,7 +1100,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
'complex' in input_dtype):
continue
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)
f(data)
......@@ -1129,7 +1129,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
input_dtype != acc_dtype):
continue
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)
f(data)
else:
......@@ -1143,7 +1143,7 @@ class T_prod_without_zeros_dtype(unittest.TestCase):
class TestBitOpReduceGrad(unittest.TestCase):
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):
x = tensor.bmatrix('x')
......@@ -1152,11 +1152,11 @@ class TestBitOpReduceGrad(unittest.TestCase):
f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random,
numpy.zeros_like(x_random),
numpy.ones_like(x_random)):
np.zeros_like(x_random),
np.ones_like(x_random)):
gx_val = f(x_val)
assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0)
assert np.all(gx_val == 0)
def test_any_grad(self):
x = tensor.bmatrix('x')
......@@ -1165,11 +1165,11 @@ class TestBitOpReduceGrad(unittest.TestCase):
f = theano.function([x], gx)
x_random = self.rng.binomial(n=1, p=0.5, size=(5, 7)).astype('int8')
for x_val in (x_random,
numpy.zeros_like(x_random),
numpy.ones_like(x_random)):
np.zeros_like(x_random),
np.ones_like(x_random)):
gx_val = f(x_val)
assert gx_val.shape == x_val.shape
assert numpy.all(gx_val == 0)
assert np.all(gx_val == 0)
class TestElemwise(unittest_tools.InferShapeTester):
......@@ -1195,8 +1195,8 @@ class TestElemwise(unittest_tools.InferShapeTester):
dtype = theano.config.floatX
t_left = TensorType(dtype, [(entry == 1) for entry in s_left])()
t_right = TensorType(dtype, [(entry == 1) for entry in s_right])()
t_left_val = numpy.zeros(s_left, dtype=dtype)
t_right_val = numpy.zeros(s_right, dtype=dtype)
t_left_val = np.zeros(s_left, dtype=dtype)
t_right_val = np.zeros(s_right, dtype=dtype)
self._compile_and_check(
[t_left, t_right],
[Elemwise(scalar.add)(t_left, t_right)],
......@@ -1210,7 +1210,7 @@ class TestElemwise(unittest_tools.InferShapeTester):
s = a + b + c + d + e + f
g = theano.function([a, b, c, d, e, f], s,
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():
......@@ -1226,9 +1226,9 @@ def test_gt_grad():
T = theano.tensor
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))
W_values = numpy.asarray(random_values, dtype=floatX)
W_values = np.asarray(random_values, dtype=floatX)
W = theano.shared(value=W_values, name='weights')
correct_score = T.dot(input_, W)
wrong_input = T.vector(dtype=floatX)
......@@ -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
# greater than z
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():
......@@ -1287,16 +1287,16 @@ def test_grad_useless_sum():
tensor.type.values_eq_approx_remove_nan)
try:
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:
TensorType.values_eq_approx = old_values_eq_approx
assert not any([isinstance(node.op, theano.tensor.elemwise.Sum) for node in nodes])
assert numpy.allclose(outputs, [[-3.72007598e-44],
[-0.26894142],
[-0.5],
[-0.73105858],
[-1.]])
assert np.allclose(outputs, [[-3.72007598e-44],
[-0.26894142],
[-0.5],
[-0.73105858],
[-1.]])
def test_elemwise_grad_broadcast():
......
......@@ -2,7 +2,6 @@ from __future__ import absolute_import, print_function, division
from functools import partial
import numpy as np
import numpy
import theano
from theano.tests import unittest_tools as utt
......@@ -23,7 +22,7 @@ from theano.tests.unittest_tools import attr
def test_cpu_contiguous():
a = T.fmatrix('a')
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]))
topo = f.maker.fgraph.toposort()
assert any([isinstance(node.op, CpuContiguous) for node in topo])
......@@ -33,7 +32,7 @@ def test_cpu_contiguous():
# Test the grad:
theano.tests.unittest_tools.verify_grad(cpu_contiguous,
[numpy.random.rand(5, 7, 2)])
[np.random.rand(5, 7, 2)])
class TestSearchsortedOp(utt.InferShapeTester):
......@@ -280,20 +279,20 @@ class SqueezeTester(utt.InferShapeTester):
def test_op(self):
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)()
f = theano.function([variable], self.op(variable))
expected = numpy.squeeze(data)
expected = np.squeeze(data)
tested = f(data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
assert np.allclose(tested, expected)
def test_infer_shape(self):
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)()
self._compile_and_check([variable],
......@@ -304,23 +303,23 @@ class SqueezeTester(utt.InferShapeTester):
def test_grad(self):
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])
def test_var_interface(self):
# same as test_op, but use a_theano_var.squeeze.
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)()
f = theano.function([variable], variable.squeeze())
expected = numpy.squeeze(data)
expected = np.squeeze(data)
tested = f(data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
assert np.allclose(tested, expected)
class CompressTester(utt.InferShapeTester):
......@@ -351,17 +350,17 @@ class CompressTester(utt.InferShapeTester):
for axis, cond, shape in zip(self.axis_list, self.cond_list,
self.shape_list):
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()
f = theano.function([cond_var, data_var],
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)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
assert np.allclose(tested, expected)
class TestRepeatOp(utt.InferShapeTester):
......@@ -388,7 +387,7 @@ class TestRepeatOp(utt.InferShapeTester):
for axis in self._possible_axis(ndim):
for dtype in tensor.integer_dtypes:
r_var = T.scalar(dtype=dtype)
r = numpy.asarray(3, dtype=dtype)
r = np.asarray(3, dtype=dtype)
if (dtype == 'uint64' or
(dtype in self.numpy_unsupported_dtypes and
r_var.ndim == 1)):
......@@ -441,13 +440,13 @@ class TestRepeatOp(utt.InferShapeTester):
def test_infer_shape(self):
for ndim in range(4):
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)
for axis in self._possible_axis(ndim):
for dtype in tensor.integer_dtypes:
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:
r_var = T.vector(dtype=dtype)
self.assertRaises(TypeError, repeat, x, r_var)
......@@ -501,17 +500,17 @@ class TestBartlett(utt.InferShapeTester):
def test_perform(self):
x = tensor.lscalar()
f = function([x], self.op(x))
M = numpy.random.randint(3, 51, size=())
assert numpy.allclose(f(M), numpy.bartlett(M))
assert numpy.allclose(f(0), numpy.bartlett(0))
assert numpy.allclose(f(-1), numpy.bartlett(-1))
b = numpy.array([17], dtype='uint8')
assert numpy.allclose(f(b[0]), numpy.bartlett(b[0]))
M = np.random.randint(3, 51, size=())
assert np.allclose(f(M), np.bartlett(M))
assert np.allclose(f(0), np.bartlett(0))
assert np.allclose(f(-1), np.bartlett(-1))
b = np.array([17], dtype='uint8')
assert np.allclose(f(b[0]), np.bartlett(b[0]))
def test_infer_shape(self):
x = tensor.lscalar()
self._compile_and_check([x], [self.op(x)],
[numpy.random.randint(3, 51, size=())],
[np.random.randint(3, 51, size=())],
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)
......@@ -519,7 +518,7 @@ class TestBartlett(utt.InferShapeTester):
class TestFillDiagonal(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
rng = np.random.RandomState(43)
def setUp(self):
super(TestFillDiagonal, self).setUp()
......@@ -531,21 +530,21 @@ class TestFillDiagonal(utt.InferShapeTester):
y = tensor.scalar()
f = function([x, y], fill_diagonal(x, y))
for shp in [(8, 8), (5, 8), (8, 5)]:
a = numpy.random.rand(*shp).astype(config.floatX)
val = numpy.cast[config.floatX](numpy.random.rand())
a = np.random.rand(*shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.rand())
out = f(a, val)
# We can't use numpy.fill_diagonal as it is bugged.
assert numpy.allclose(numpy.diag(out), val)
# We can't use np.fill_diagonal as it is bugged.
assert np.allclose(np.diag(out), val)
assert (out == val).sum() == min(a.shape)
# 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()
y = tensor.scalar()
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)
# 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[1, 1, 1] == val
assert out[2, 2, 2] == val
......@@ -553,11 +552,11 @@ class TestFillDiagonal(utt.InferShapeTester):
@attr('slow')
def test_gradient(self):
utt.verify_grad(fill_diagonal, [numpy.random.rand(5, 8),
numpy.random.rand()],
utt.verify_grad(fill_diagonal, [np.random.rand(5, 8),
np.random.rand()],
n_tests=1, rng=TestFillDiagonal.rng)
utt.verify_grad(fill_diagonal, [numpy.random.rand(8, 5),
numpy.random.rand()],
utt.verify_grad(fill_diagonal, [np.random.rand(8, 5),
np.random.rand()],
n_tests=1, rng=TestFillDiagonal.rng)
def test_infer_shape(self):
......@@ -565,20 +564,20 @@ class TestFillDiagonal(utt.InferShapeTester):
x = tensor.dmatrix()
y = tensor.dscalar()
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(8, 5),
numpy.random.rand()],
[np.random.rand(8, 5),
np.random.rand()],
self.op_class)
self._compile_and_check([z, y], [self.op(z, y)],
# must be square when nd>2
[numpy.random.rand(8, 8, 8),
numpy.random.rand()],
[np.random.rand(8, 8, 8),
np.random.rand()],
self.op_class,
warn=False)
class TestFillDiagonalOffset(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
rng = np.random.RandomState(43)
def setUp(self):
super(TestFillDiagonalOffset, self).setUp()
......@@ -593,11 +592,11 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
f = function([x, y, z], fill_diagonal_offset(x, y, z))
for test_offset in (-5, -4, -1, 0, 1, 4, 5):
for shp in [(8, 8), (5, 8), (8, 5), (5, 5)]:
a = numpy.random.rand(*shp).astype(config.floatX)
val = numpy.cast[config.floatX](numpy.random.rand())
a = np.random.rand(*shp).astype(config.floatX)
val = np.cast[config.floatX](np.random.rand())
out = f(a, val, test_offset)
# We can't use numpy.fill_diagonal as it is bugged.
assert numpy.allclose(numpy.diag(out, test_offset), val)
# We can't use np.fill_diagonal as it is bugged.
assert np.allclose(np.diag(out, test_offset), val)
if test_offset >= 0:
assert (out == val).sum() == min(min(a.shape),
a.shape[1] - test_offset)
......@@ -612,13 +611,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
return fill_diagonal_offset(a, val, test_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)
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)
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)
def test_infer_shape(self):
......@@ -627,13 +626,13 @@ class TestFillDiagonalOffset(utt.InferShapeTester):
z = tensor.iscalar()
for test_offset in (-5, -4, -1, 0, 1, 4, 5):
self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(8, 5),
numpy.random.rand(),
[np.random.rand(8, 5),
np.random.rand(),
test_offset],
self.op_class)
self._compile_and_check([x, y, z], [self.op(x, y, z)],
[numpy.random.rand(5, 8),
numpy.random.rand(),
[np.random.rand(5, 8),
np.random.rand(),
test_offset],
self.op_class)
......@@ -644,7 +643,7 @@ def test_to_one_hot():
f = theano.function([v], o)
out = f([1, 2, 3, 5, 6])
assert out.dtype == theano.config.floatX
assert numpy.allclose(
assert np.allclose(
out,
[[0., 1., 0., 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():
f = theano.function([v], o)
out = f([1, 2, 3, 5, 6])
assert out.dtype == "int32"
assert numpy.allclose(
assert np.allclose(
out,
[[0., 1., 0., 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
import numpy
import numpy as np
import unittest
import theano
......@@ -18,25 +18,25 @@ class TestFFT(unittest.TestCase):
def f_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)
def f_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)
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')
rfft = fft.rfft(x)
f_rfft = theano.function([x], rfft)
res_rfft = f_rfft(inputs_val)
res_rfft_comp = (numpy.asarray(res_rfft[:, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, 1]))
res_rfft_comp = (np.asarray(res_rfft[:, :, 0]) +
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)
......@@ -46,7 +46,7 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([m], irfft)
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
# enough epsilon to get good accuracy.
......@@ -54,30 +54,30 @@ class TestFFT(unittest.TestCase):
def f_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)
def f_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)
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)
rfft = fft.rfft(inputs)
f_rfft = theano.function([], rfft)
res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1]))
res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
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)
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)
rfft = fft.rfft(inputs)
......@@ -89,9 +89,9 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([m], irfft)
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)
irfft = fft.irfft(inputs)
......@@ -99,22 +99,22 @@ class TestFFT(unittest.TestCase):
res_irfft = f_irfft()
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)
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)
# Unitary normalization
rfft = fft.rfft(inputs, norm='ortho')
f_rfft = theano.function([], rfft)
res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1]))
res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
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)
......@@ -122,13 +122,13 @@ class TestFFT(unittest.TestCase):
rfft = fft.rfft(inputs, norm='no_norm')
f_rfft = theano.function([], rfft)
res_rfft = f_rfft()
res_rfft_comp = (numpy.asarray(res_rfft[:, :, :, 0]) +
1j * numpy.asarray(res_rfft[:, :, :, 1]))
res_rfft_comp = (np.asarray(res_rfft[:, :, :, 0]) +
1j * np.asarray(res_rfft[:, :, :, 1]))
utt.assert_allclose(rfft_ref, res_rfft_comp, atol=1e-4, rtol=1e-4)
# 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_ref = inputs_val[..., 0] + 1j * inputs_val[..., 1]
......@@ -137,7 +137,7 @@ class TestFFT(unittest.TestCase):
f_irfft = theano.function([], 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)
......@@ -149,12 +149,12 @@ class TestFFT(unittest.TestCase):
utt.assert_allclose(irfft_ref * N**2, res_irfft, atol=1e-4, rtol=1e-4)
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)
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)
self.assertRaises(ValueError, fft.irfft, inputs, norm=123)
......@@ -167,20 +167,20 @@ class TestFFT(unittest.TestCase):
def f_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)
def f_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)
def f_rfft(inp):
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)
def f_irfft(inp):
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)
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
from numpy.testing import dec
import theano
......@@ -10,7 +10,7 @@ from theano.tensor.fourier import Fourier, fft
class TestFourier(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
rng = np.random.RandomState(43)
def setUp(self):
super(TestFourier, self).setUp()
......@@ -20,24 +20,24 @@ class TestFourier(utt.InferShapeTester):
def test_perform(self):
a = tensor.dmatrix()
f = theano.function([a], self.op(a, n=10, axis=0))
a = numpy.random.rand(8, 6)
assert numpy.allclose(f(a), numpy.fft.fft(a, 10, 0))
a = np.random.rand(8, 6)
assert np.allclose(f(a), np.fft.fft(a, 10, 0))
def test_infer_shape(self):
a = tensor.dvector()
self._compile_and_check([a], [self.op(a, 16, 0)],
[numpy.random.rand(12)],
[np.random.rand(12)],
self.op_class)
a = tensor.dmatrix()
for var in [self.op(a, 16, 1), self.op(a, None, 1),
self.op(a, 16, None), self.op(a, None, None)]:
self._compile_and_check([a], [var],
[numpy.random.rand(12, 4)],
[np.random.rand(12, 4)],
self.op_class)
b = tensor.iscalar()
for var in [self.op(a, 16, b), self.op(a, None, b)]:
self._compile_and_check([a, b], [var],
[numpy.random.rand(12, 4), 0],
[np.random.rand(12, 4), 0],
self.op_class)
@dec.skipif(True, "Complex grads not enabled, see #178")
......@@ -54,10 +54,10 @@ class TestFourier(utt.InferShapeTester):
def fft_test4(a):
return self.op(a, 4, 0)
pts = [numpy.random.rand(5, 2, 4, 3),
numpy.random.rand(2, 3, 4),
numpy.random.rand(2, 5),
numpy.random.rand(5)]
pts = [np.random.rand(5, 2, 4, 3),
np.random.rand(2, 3, 4),
np.random.rand(2, 5),
np.random.rand(5)]
for fft_test in [fft_test1, fft_test2, fft_test3, fft_test4]:
for pt in pts:
theano.gradient.verify_grad(fft_test, [pt],
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import six.moves.cPickle as pickle
from six.moves import xrange
import theano
......@@ -13,10 +13,10 @@ def test_no_reuse():
f = theano.function([x, y], x + y)
# 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:
f(numpy.ones(10))
f(np.ones(10))
except TypeError:
return
assert not 'should not get here'
......@@ -78,8 +78,8 @@ def test_gc_never_pickles_temporaries():
# now run the function once to create temporaries within the no-gc
# linker
f(numpy.ones(100, dtype='float64'))
g(numpy.ones(100, dtype='float64'))
f(np.ones(100, dtype='float64'))
g(np.ones(100, dtype='float64'))
# serialize the functions again
post_f = pickle.dumps(f)
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import unittest
from theano.tests import unittest_tools as utt
import theano
......@@ -43,13 +43,13 @@ class Test_inc_subtensor(unittest.TestCase):
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_sl2_end = 2
result = f(val_a, val_inc, val_sl2_end)
expected_result = numpy.copy(val_a)
expected_result = np.copy(val_a)
if do_set:
expected_result[:, :val_sl2_end] = val_inc
else:
......@@ -71,7 +71,7 @@ class Test_inc_subtensor(unittest.TestCase):
# These symbolic graphs legitimate, as long as increment has exactly
# 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):
return rng.rand(*shape).astype(theano.config.floatX)
......@@ -101,7 +101,7 @@ class Test_inc_subtensor(unittest.TestCase):
sl2 = slice(sl2_end)
sl3 = 2
val_a = numpy.ones((5, 3, 4))
val_a = np.ones((5, 3, 4))
val_inc = 2.3
val_sl2_end = 2
......@@ -112,7 +112,7 @@ class Test_inc_subtensor(unittest.TestCase):
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)
if method is tt.set_subtensor:
......@@ -127,7 +127,7 @@ class Test_inc_subtensor(unittest.TestCase):
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)
if method is tt.set_subtensor:
......@@ -152,23 +152,23 @@ class Test_inc_subtensor(unittest.TestCase):
# vector
utt.verify_grad(
f_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
utt.verify_grad(
f_slice(slice(1, 2, None), slice(None, None, 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.]]), ))
# single element
utt.verify_grad(
f_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.),))
# broadcast
utt.verify_grad(
f_slice(2),
(numpy.asarray([[0, 1], [2, 3], [4, 5.]]),
numpy.asarray(9.),))
(np.asarray([[0, 1], [2, 3], [4, 5.]]),
np.asarray(9.),))
......@@ -2,17 +2,17 @@ from __future__ import absolute_import, print_function, division
import unittest
import theano
from theano import tensor, function, Variable, Generic
import numpy
import numpy as np
import os
class T_load_tensor(unittest.TestCase):
def setUp(self):
self.data = numpy.arange(5, dtype=numpy.int32)
self.data = np.arange(5, dtype=np.int32)
self.filename = os.path.join(
theano.config.compiledir,
"_test.npy")
numpy.save(self.filename, self.data)
np.save(self.filename, self.data)
def test0(self):
path = Variable(Generic())
......@@ -49,7 +49,7 @@ class T_load_tensor(unittest.TestCase):
path = Variable(Generic())
x = tensor.load(path, 'int32', (False,), mmap_mode='c')
fn = function([path], x)
assert type(fn(self.filename)) == numpy.core.memmap
assert type(fn(self.filename)) == np.core.memmap
def tearDown(self):
os.remove(os.path.join(
......
from __future__ import absolute_import, print_function, division
import unittest
import numpy
import numpy as np
from six import integer_types
import theano
......@@ -42,7 +42,7 @@ class TestKeepDims(unittest.TestCase):
def test_keepdims(self):
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
# by the ops tests.
mode = theano.compile.Mode(optimizer="fast_compile", linker="py")
......@@ -60,7 +60,7 @@ class TestKeepDims(unittest.TestCase):
axis)],
mode=mode)
ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
f = function([x], [op(x, axis=axis, keepdims=True)[1],
......@@ -69,7 +69,7 @@ class TestKeepDims(unittest.TestCase):
axis)],
mode=mode)
ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
# the following ops can be specified with either a single axis or every
......@@ -84,7 +84,7 @@ class TestKeepDims(unittest.TestCase):
axis)],
mode=mode)
ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
# the following ops can be specified with a freely specified axis
......@@ -103,13 +103,13 @@ class TestKeepDims(unittest.TestCase):
mode=mode)
ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
def test_norm(self):
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")
for axis in [0, 1, 2, [0], [1], [2], None,
......@@ -121,7 +121,7 @@ class TestKeepDims(unittest.TestCase):
], mode=mode)
ans1, ans2 = f(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
g = function([x], [x.norm(L=2, axis=axis, keepdims=True),
......@@ -129,5 +129,5 @@ class TestKeepDims(unittest.TestCase):
], mode=mode)
ans1, ans2 = g(a)
assert numpy.allclose(ans1, ans2)
assert np.allclose(ans1, ans2)
assert ans1.shape == ans2.shape
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
from theano.gof.type import Type
from theano.gof.graph import Variable, Apply, Constant
from theano.gof.op import Op
......@@ -62,8 +62,8 @@ def test_merge_with_weird_eq():
"""numpy arrays don't compare equal like other python objects"""
# SCALAR CASE
x = T.constant(numpy.asarray(1), name='x')
y = T.constant(numpy.asarray(1), name='y')
x = T.constant(np.asarray(1), name='x')
y = T.constant(np.asarray(1), name='y')
g = Env([x, y], [x+y])
MergeOptimizer().optimize(g)
......@@ -74,8 +74,8 @@ def test_merge_with_weird_eq():
# NONSCALAR CASE
# This was created to test TensorConstantSignature
x = T.constant(numpy.ones(5), name='x')
y = T.constant(numpy.ones(5), name='y')
x = T.constant(np.ones(5), name='x')
y = T.constant(np.ones(5), name='y')
g = Env([x, y], [x+y])
MergeOptimizer().optimize(g)
......
from __future__ import absolute_import, print_function, division
import copy
import sys
import numpy
import numpy as np
import theano
from theano import tensor
from theano.tensor.nnet import crossentropy_softmax_argmax_1hot_with_bias
......@@ -12,7 +12,7 @@ def test_bug_2009_06_02_trac_387():
f = theano.function([y],
tensor.int_div(
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
# checking. What was the bug, and how is it being tested?
......@@ -25,15 +25,15 @@ def test_bug_2009_07_17_borrowed_output():
g = theano.function([a, b],
theano.Out(theano.tensor.dot(a, b), borrow=False))
x = numpy.zeros((1, 2))
y = numpy.ones((2, 5))
x = np.zeros((1, 2))
y = np.ones((2, 5))
z = g(x, y)
print(z) # Should be zero.
x.fill(1)
print(g(x, y)) # Should be non-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
# 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():
g = theano.function([test_output_activation_no_bias, test_b2, test_target],
theano.Out(output, borrow=False))
a = numpy.zeros((1, 5))
b = numpy.ones(5)
c = numpy.zeros(1, dtype=numpy.int32)
a = np.zeros((1, 5))
b = np.ones(5)
c = np.zeros(1, dtype=np.int32)
z = g(a, b, c)
z_backup = copy.copy(z)
......@@ -80,5 +80,5 @@ def test_deepcopied_type_filter():
# As of commit 731e2d2fa68487733320d341d08b454a50c90d12
# it was failing.
a.type.filter(
numpy.ones((2, 2), dtype=a.dtype),
np.ones((2, 2), dtype=a.dtype),
strict=True)
from __future__ import absolute_import, print_function, division
import unittest
import numpy
import numpy as np
import numpy.linalg
from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose
......@@ -28,7 +28,7 @@ from nose.tools import assert_raises
def test_pseudoinverse_correctness():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
d1 = rng.randint(4) + 2
d2 = rng.randint(4) + 2
r = rng.randn(d1, d2).astype(theano.config.floatX)
......@@ -41,8 +41,8 @@ def test_pseudoinverse_correctness():
assert ri.shape[1] == r.shape[0]
assert ri.dtype == r.dtype
# Note that pseudoinverse can be quite unprecise so I prefer to compare
# the result with what numpy.linalg returns
assert _allclose(ri, numpy.linalg.pinv(r))
# the result with what np.linalg returns
assert _allclose(ri, np.linalg.pinv(r))
class test_MatrixInverse(utt.InferShapeTester):
......@@ -50,7 +50,7 @@ class test_MatrixInverse(utt.InferShapeTester):
super(test_MatrixInverse, self).setUp()
self.op_class = MatrixInverse
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):
......@@ -63,11 +63,11 @@ class test_MatrixInverse(utt.InferShapeTester):
assert ri.shape == r.shape
assert ri.dtype == r.dtype
rir = numpy.dot(ri, r)
rri = numpy.dot(r, ri)
rir = np.dot(ri, r)
rri = np.dot(r, ri)
assert _allclose(numpy.identity(4), rir), rir
assert _allclose(numpy.identity(4), rri), rri
assert _allclose(np.identity(4), rir), rir
assert _allclose(np.identity(4), rri), rri
def test_infer_shape(self):
......@@ -81,7 +81,7 @@ class test_MatrixInverse(utt.InferShapeTester):
def test_matrix_dot():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
n = rng.randint(4) + 2
rs = []
xs = []
......@@ -93,26 +93,26 @@ def test_matrix_dot():
theano_sol = function(xs, sol)(*rs)
numpy_sol = rs[0]
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)
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 = rng.rand(4, 4).astype(theano.config.floatX)
f = function([A], qr(A))
t_qr = f(a)
n_qr = numpy.linalg.qr(a)
n_qr = np.linalg.qr(a)
assert _allclose(n_qr, t_qr)
for mode in ["reduced", "r", "raw"]:
f = function([A], qr(A, mode))
t_qr = f(a)
n_qr = numpy.linalg.qr(a, mode)
n_qr = np.linalg.qr(a, mode)
if isinstance(n_qr, (list, tuple)):
assert _allclose(n_qr[0], t_qr[0])
assert _allclose(n_qr[1], t_qr[1])
......@@ -120,7 +120,7 @@ def test_qr_modes():
assert _allclose(n_qr, t_qr)
try:
n_qr = numpy.linalg.qr(a, "complete")
n_qr = np.linalg.qr(a, "complete")
f = function([A], qr(A, "complete"))
t_qr = f(a)
assert _allclose(n_qr, t_qr)
......@@ -129,12 +129,12 @@ def test_qr_modes():
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)
U, V, T = svd(A)
fn = function([A], [U, V, T])
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)
assert _allclose(n_u, t_u)
......@@ -143,19 +143,19 @@ def test_svd():
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)
B = tensor.matrix("B", dtype=theano.config.floatX)
X = tensorsolve(A, B)
fn = function([A, B], [X])
# slightly modified example from numpy.linalg.tensorsolve docstring
a = numpy.eye(2 * 3 * 4).astype(theano.config.floatX)
# slightly modified example from np.linalg.tensorsolve docstring
a = np.eye(2 * 3 * 4).astype(theano.config.floatX)
a.shape = (2 * 3, 4, 2, 3 * 4)
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)
assert _allclose(n_x, t_x)
......@@ -165,10 +165,10 @@ def test_tensorsolve():
Y = tensorsolve(C, D)
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)
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)
assert _allclose(n_y, t_y)
assert n_y.dtype == Y.dtype
......@@ -179,68 +179,68 @@ def test_tensorsolve():
Z = tensorsolve(E, F)
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)
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)
assert _allclose(n_z, t_z)
assert n_z.dtype == Z.dtype
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)
a = tensor.matrix()
f = function([a], matrix_inverse(a))
try:
f(singular)
except numpy.linalg.LinAlgError:
except np.linalg.LinAlgError:
return
assert False
def test_inverse_grad():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
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)
tensor.verify_grad(matrix_inverse, [r], rng=numpy.random)
tensor.verify_grad(matrix_inverse, [r], rng=np.random)
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)
x = tensor.matrix()
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():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
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():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX)
x = tensor.matrix()
f = theano.function([x], det(x))
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):
"""
Test that linalg.diag has the same behavior as numpy.diag.
numpy.diag has two behaviors:
Test that linalg.diag has the same behavior as np.diag.
np.diag has two behaviors:
(1) when given a vector, it returns a matrix with that vector as the
diagonal.
(2) when given a matrix, returns a vector which is the diagonal of the
......@@ -263,7 +263,7 @@ class test_diag(unittest.TestCase):
super(test_diag, self).__init__(name)
def test_alloc_diag(self):
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
x = theano.tensor.vector()
g = alloc_diag(x)
f = theano.function([x], g)
......@@ -271,7 +271,7 @@ class test_diag(unittest.TestCase):
# test "normal" scenario (5x5 matrix) and special cases of 0x0 and 1x1
for shp in [5, 0, 1]:
m = rng.rand(shp).astype(self.floatX)
v = numpy.diag(m)
v = np.diag(m)
r = f(m)
# The right matrix is created
assert (r == v).all()
......@@ -295,7 +295,7 @@ class test_diag(unittest.TestCase):
assert (f(m) == m.shape).all()
def test_alloc_diag_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
x = rng.rand(5)
tensor.verify_grad(alloc_diag, [x], rng=rng)
......@@ -322,7 +322,7 @@ class test_diag(unittest.TestCase):
# not testing the view=True case since it is not used anywhere.
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)
x = self.shared(m)
g = extract_diag(x)
......@@ -334,7 +334,7 @@ class test_diag(unittest.TestCase):
for shp in [(2, 3), (3, 2), (3, 3), (1, 1), (0, 0)]:
m = rng.rand(*shp).astype(self.floatX)
x.set_value(m)
v = numpy.diag(m)
v = np.diag(m)
r = f()
# The right diagonal is extracted
assert (r == v).all()
......@@ -360,13 +360,13 @@ class test_diag(unittest.TestCase):
assert f() == min(shp)
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)
tensor.verify_grad(extract_diag, [x], rng=rng)
@attr('slow')
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)
assert [isinstance(node.inputs[0].type, self.type)
......@@ -375,14 +375,14 @@ class test_diag(unittest.TestCase):
def test_trace():
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
x = theano.tensor.matrix()
g = trace(x)
f = theano.function([x], g)
for shp in [(2, 3), (3, 2), (3, 3)]:
m = rng.rand(*shp).astype(config.floatX)
v = numpy.trace(m)
v = np.trace(m)
assert v == f(m)
xx = theano.tensor.vector()
......@@ -401,9 +401,9 @@ class test_Eig(utt.InferShapeTester):
def setUp(self):
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.X = numpy.asarray(self.rng.rand(5, 5),
self.X = np.asarray(self.rng.rand(5, 5),
dtype=self.dtype)
self.S = self.X.dot(self.X.T)
......@@ -423,7 +423,7 @@ class test_Eig(utt.InferShapeTester):
[[1.0], [[1.0]]])
x = [[0, 1], [1, 0]]
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):
......@@ -435,8 +435,8 @@ class test_Eigh(test_Eig):
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')]
assert_array_almost_equal(wu, wl)
assert_array_almost_equal(vu * numpy.sign(vu[0, :]),
vl * numpy.sign(vl[0, :]))
assert_array_almost_equal(vu * np.sign(vu[0, :]),
vl * np.sign(vl[0, :]))
def test_grad(self):
X = self.X
......@@ -466,12 +466,12 @@ class T_lstsq(unittest.TestCase):
z = tensor.lscalar()
b = theano.tensor.nlinalg.lstsq()(x, y, z)
f = function([x, y, z], b)
TestMatrix1 = numpy.asarray([[2, 1], [3, 4]])
TestMatrix2 = numpy.asarray([[17, 20], [43, 50]])
TestScalar = numpy.asarray(1)
TestMatrix1 = np.asarray([[2, 1], [3, 4]])
TestMatrix2 = np.asarray([[17, 20], [43, 50]])
TestScalar = np.asarray(1)
f = function([x, y, z], b)
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):
x = tensor.vector()
......@@ -479,7 +479,7 @@ class T_lstsq(unittest.TestCase):
z = tensor.scalar()
b = theano.tensor.nlinalg.lstsq()(x, y, z)
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):
x = tensor.vector()
......@@ -487,24 +487,24 @@ class T_lstsq(unittest.TestCase):
z = tensor.vector()
b = theano.tensor.nlinalg.lstsq()(x, y, z)
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):
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)
Q = matrix_power(A, 3)
fn = function([A], [Q])
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)
assert numpy.allclose(n_p, t_p)
assert np.allclose(n_p, t_p)
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)
Q = matrix_power(A, 3)
f = function([A], [Q])
......@@ -524,10 +524,10 @@ class T_NormTests(unittest.TestCase):
self.assertRaises(ValueError, norm, 3, None)
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):
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
M = tensor.matrix("A", dtype=theano.config.floatX)
V = tensor.vector("V", dtype=theano.config.floatX)
......@@ -543,7 +543,7 @@ class T_NormTests(unittest.TestCase):
for i in range(0, 14):
f = function([A[1][i]], norm(A[1][i], A[0][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)
......@@ -552,9 +552,9 @@ class test_TensorInv(utt.InferShapeTester):
super(test_TensorInv, self).setUp()
self.A = tensor.tensor4("A", 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.b = numpy.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.a = np.random.rand(4, 6, 8, 3).astype(theano.config.floatX)
self.b = np.random.rand(2, 15, 30).astype(theano.config.floatX)
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):
A = self.A
......@@ -567,7 +567,7 @@ class test_TensorInv(utt.InferShapeTester):
def test_eval(self):
A = self.A
Ai = tensorinv(A)
n_ainv = numpy.linalg.tensorinv(self.a)
n_ainv = np.linalg.tensorinv(self.a)
tf_a = function([A], [Ai])
t_ainv = tf_a(self.a)
assert _allclose(n_ainv, t_ainv)
......@@ -575,8 +575,8 @@ class test_TensorInv(utt.InferShapeTester):
B = self.B
Bi = tensorinv(B)
Bi1 = tensorinv(B, ind=1)
n_binv = numpy.linalg.tensorinv(self.b)
n_binv1 = numpy.linalg.tensorinv(self.b1, ind=1)
n_binv = np.linalg.tensorinv(self.b)
n_binv1 = np.linalg.tensorinv(self.b1, ind=1)
tf_b = function([B], [Bi])
tf_b1 = function([B], [Bi1])
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
import unittest
import numpy
import numpy as np
import theano
from theano import function, config
......@@ -28,7 +28,7 @@ class T_max_and_argmax(unittest.TestCase):
'canonicalize', 'fast_run')
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()
f = function([n], tensor.max_and_argmax(n, axis)[0], mode=mode)
......@@ -49,7 +49,7 @@ class T_min_max(unittest.TestCase):
'canonicalize', 'fast_run')
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()
for axis in [0, 1, -1]:
......@@ -82,7 +82,7 @@ class T_min_max(unittest.TestCase):
f(data)
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()
for axis in [0, 1, -1]:
......@@ -206,4 +206,4 @@ def test_local_dimshuffle_subtensor():
topo = f.maker.fgraph.toposort()
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
import numpy
import numpy as np
import pickle
from theano.tests import unittest_tools as utt
......@@ -19,7 +19,7 @@ class T_random_function(utt.InferShapeTester):
utt.seed_rng()
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 getattr(rf, 'destroy_map', {}) == {}
......@@ -33,23 +33,23 @@ class T_random_function(utt.InferShapeTester):
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_1 = f(rng_state0)
assert numpy.all(f_0 == f_1)
assert np.all(f_0 == f_1)
def test_inplace_norun(self):
rf = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector,
rf = RandomFunction(np.random.RandomState.uniform, tensor.dvector,
inplace=True)
assert rf.inplace
assert getattr(rf, 'destroy_map', {}) != {}
def test_args(self):
"""Test that arguments to RandomFunction are honored"""
rf2 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector)
rf4 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector,
rf2 = RandomFunction(np.random.RandomState.uniform, tensor.dvector)
rf4 = RandomFunction(np.random.RandomState.uniform, tensor.dvector,
inplace=True)
rng_R = random_state_type()
......@@ -64,7 +64,7 @@ class T_random_function(utt.InferShapeTester):
# be maintained by post_r4
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r4,
mutable=True)],
[out2, out4, out2_4, out2_4_4],
......@@ -84,19 +84,19 @@ class T_random_function(utt.InferShapeTester):
# print f2_4_4b
# 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
# 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
assert not numpy.allclose(f4, f4b), (f4, f4b)
assert not np.allclose(f4, f4b), (f4, f4b)
def test_inplace_optimization(self):
"""Test that FAST_RUN includes the random_make_inplace optimization"""
#inplace = False
rf2 = RandomFunction(numpy.random.RandomState.uniform, tensor.dvector)
rf2 = RandomFunction(np.random.RandomState.uniform, tensor.dvector)
rng_R = random_state_type()
# If calling RandomFunction directly, all args have to be specified,
......@@ -105,7 +105,7 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r2,
mutable=True)],
out2,
......@@ -121,7 +121,7 @@ class T_random_function(utt.InferShapeTester):
val1 = f()
assert id0 == id(f[rng_R])
assert not numpy.allclose(val0, val1)
assert not np.allclose(val0, val1)
def test_no_inplace(self):
"""Test that when not running inplace, the RandomState is
......@@ -131,10 +131,10 @@ class T_random_function(utt.InferShapeTester):
post_r, out = rf(rng_R, (3,), 0., 1.)
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)
rng_ = numpy.random.RandomState(utt.fetch_seed())
rng_ = np.random.RandomState(utt.fetch_seed())
# rng should still be in a fresh state
self.assertTrue(rng_R.type.values_eq(rng, rng_))
# rng0 should be in an updated state
......@@ -178,7 +178,7 @@ class T_random_function(utt.InferShapeTester):
f_ok = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_out2_4_4,
mutable=True)],
[out4, out1_4, out2_4_4],
......@@ -188,8 +188,8 @@ class T_random_function(utt.InferShapeTester):
o4, o1_4, o2_4_4 = f_ok()
# Check the sanity of the answers
self.assertTrue(numpy.allclose(o4, o1_4))
self.assertTrue(numpy.allclose(o4, o2_4_4[0]))
self.assertTrue(np.allclose(o4, o1_4))
self.assertTrue(np.allclose(o4, o2_4_4[0]))
def test_random_function_noshape_args(self):
'''Test if random_function helper works with args but without shape'''
......@@ -199,7 +199,7 @@ class T_random_function(utt.InferShapeTester):
post_out, out = uniform(rng_R, size=None, ndim=2)
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_out,
mutable=True)],
[out],
......@@ -219,7 +219,7 @@ class T_random_function(utt.InferShapeTester):
[low,
high,
compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_out2,
mutable=True)],
[out2],
......@@ -242,7 +242,7 @@ class T_random_function(utt.InferShapeTester):
# If using numpy's uniform distribution, ndim_added should be 0,
# because the shape provided as argument is the output shape.
# 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.
def ndim_added_deco(ndim_added):
def randomfunction(random_state, size=(), low=0.0, high=0.0,
......@@ -282,27 +282,27 @@ class T_random_function(utt.InferShapeTester):
f11 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=p_uni11, mutable=True)],
[uni11], accept_inplace=True)
f12 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=p_uni12, mutable=True)],
[uni12], accept_inplace=True)
fm11 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=p_unim11, mutable=True)],
[unim11], accept_inplace=True)
fm12 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=p_unim12, mutable=True)],
[unim12], accept_inplace=True)
f0 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=p_uni02, mutable=True)],
[uni01, uni02], accept_inplace=True)
self.assertRaises(ValueError, f11)
......@@ -310,7 +310,7 @@ class T_random_function(utt.InferShapeTester):
self.assertRaises(ValueError, fm11)
self.assertRaises(ValueError, fm12)
u01, u02 = f0()
self.assertTrue(numpy.allclose(u01, u02[0]))
self.assertTrue(np.allclose(u01, u02[0]))
def test_uniform(self):
"""Test that raw_random.uniform generates the same results as numpy."""
......@@ -321,17 +321,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.allclose(val1, numpy_val1))
self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(np.allclose(val1, numpy_val1))
def test_binomial(self):
"""Test that raw_random.binomial generates the same results
......@@ -344,17 +344,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[bin], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.all(val1 == numpy_val1))
self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(np.all(val1 == numpy_val1))
def test_normal(self):
"""Test that raw_random.normal generates the same results as numpy."""
......@@ -365,17 +365,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.allclose(val1, numpy_val1))
self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(np.allclose(val1, numpy_val1))
def test_random_integers(self):
# Test that raw_random.random_integers generates the same
......@@ -390,17 +390,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.allclose(val1, numpy_val1))
self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(np.allclose(val1, numpy_val1))
def test_permutation_helper(self):
"""Test that raw_random.permutation_helper generates the same
......@@ -418,21 +418,21 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
# numpy_rng.permutation outputs one vector at a time,
# 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)])
numpy_val1 = numpy.asarray([numpy_rng.permutation(8)
numpy_val1 = np.asarray([numpy_rng.permutation(8)
for i in range(7)])
self.assertTrue(numpy.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1))
self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(np.all(val1 == numpy_val1))
# This call lacks "ndim_added=1", so ndim_added defaults to 0.
# A ValueError should be raised.
......@@ -440,7 +440,7 @@ class T_random_function(utt.InferShapeTester):
post_r0, out0 = rf0(rng_R, (7,), 8)
f0 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r0, mutable=True)],
[out0], accept_inplace=True)
self.assertRaises(ValueError, f0)
......@@ -451,11 +451,11 @@ class T_random_function(utt.InferShapeTester):
post_r2, out2 = rf2(rng_R, (7,), 8)
f2 = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r2, mutable=True)],
[out2], accept_inplace=True)
self.assertRaises(ValueError, f2)
def test_choice(self):
"""Test that raw_random.choice generates the same
results as numpy."""
......@@ -467,17 +467,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.allclose(val1, numpy_val1))
self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(np.allclose(val1, numpy_val1))
def test_poisson(self):
"""Test that raw_random.poisson generates the same
......@@ -490,17 +490,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0 = f()
val1 = f()
numpy_val0 = 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(numpy.allclose(val1, numpy_val1))
self.assertTrue(np.allclose(val0, numpy_val0))
self.assertTrue(np.allclose(val1, numpy_val1))
def test_permutation(self):
"""Test that raw_random.permutation generates the same
......@@ -509,33 +509,33 @@ class T_random_function(utt.InferShapeTester):
post_r, out = permutation(rng_R, size=(9,), n=6)
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=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.
# numpy_rng.permutation outputs one vector at a time,
# so we call it iteratively to generate all the samples.
val0 = f()
val1 = f()
numpy_val0 = numpy.asarray([numpy_rng.permutation(6)
numpy_val0 = np.asarray([numpy_rng.permutation(6)
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)])
self.assertTrue(numpy.all(val0 == numpy_val0))
self.assertTrue(numpy.all(val1 == numpy_val1))
self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(np.all(val1 == numpy_val1))
# Test that we can generate a list: have size=None or ().
for ndim in [1, None]:
post_r, out = permutation(rng_R, n=10, size=None, ndim=ndim)
inp = compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)
f = theano.function([inp], out)
o = f()
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
self.assertRaises(TypeError, permutation, rng_R, size=None, ndim=2)
......@@ -548,17 +548,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function(
[compile.In(rng_R,
value=numpy.random.RandomState(utt.fetch_seed()),
value=np.random.RandomState(utt.fetch_seed()),
update=post_r, mutable=True)],
[out], accept_inplace=True)
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
val0, = f()
val1, = f()
numpy_val0 = 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(numpy.all(val1 == numpy_val1))
self.assertTrue(np.all(val0 == numpy_val0))
self.assertTrue(np.all(val1 == numpy_val1))
self.assertTrue(val0.shape == (7, 3, 5))
self.assertTrue(val1.shape == (7, 3, 5))
......@@ -568,7 +568,7 @@ class T_random_function(utt.InferShapeTester):
shape = tensor.lvector()
post_r, out = uniform(rng_R, shape, ndim=2)
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, [4, 8]).shape == (4, 8)
......@@ -583,7 +583,7 @@ class T_random_function(utt.InferShapeTester):
shape = (shape0, 3)
post_r, u = uniform(rng_R, size=shape, ndim=2)
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, 8).shape == (8, 3)
......@@ -601,7 +601,7 @@ class T_random_function(utt.InferShapeTester):
post_r, u = uniform(rng_R, size=shape, ndim=2)
assert u.broadcastable == (False, True)
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, 8).shape == (8, 1)
......@@ -617,25 +617,25 @@ class T_random_function(utt.InferShapeTester):
post_r, out = uniform(rng_R)
f = compile.function([rng_R], [post_r, out], accept_inplace=True)
rng_state0 = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0)
post1, val1 = f(post0)
numpy_val0 = numpy.asarray(numpy_rng.uniform(),
numpy_val0 = np.asarray(numpy_rng.uniform(),
dtype=theano.config.floatX)
numpy_val1 = numpy.asarray(numpy_rng.uniform(),
numpy_val1 = np.asarray(numpy_rng.uniform(),
dtype=theano.config.floatX)
assert numpy.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1)
assert np.all(val0 == numpy_val0)
assert np.all(val1 == numpy_val1)
post_r, out = multinomial(rng_R)
g = compile.function([rng_R], [post_r, out], accept_inplace=True)
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)
assert numpy.all(val2 == numpy_val2)
assert np.all(val2 == numpy_val2)
def test_vector_arguments(self):
rng_R = random_state_type()
......@@ -645,17 +645,17 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, low], [post_r, out], accept_inplace=True)
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())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0, [-5, .5, 0, 1])
post1, val1 = f(post0, as_floatX([.9]))
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))
assert numpy.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1)
assert np.all(val0 == numpy_val0)
assert np.all(val1 == numpy_val1)
high = tensor.vector()
post_rb, outb = uniform(rng_R, low=low, high=high)
......@@ -667,8 +667,8 @@ class T_random_function(utt.InferShapeTester):
post1b, val1b = fb(post0b, [-4.], [-1])
numpy_val0b = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
numpy_val1b = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
assert numpy.all(val0b == numpy_val0b)
assert numpy.all(val1b == numpy_val1b)
assert np.all(val0b == numpy_val0b)
assert np.all(val1b == numpy_val1b)
self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1, 0, 1])
# TODO: do we want that?
#self.assertRaises(ValueError, fb, post1b, [-4., -2], [-1])
......@@ -681,8 +681,8 @@ class T_random_function(utt.InferShapeTester):
post1c, val1c = fc(post0c, [-4.], [-1], [1])
numpy_val0c = as_floatX(numpy_rng.uniform(low=[-4., -2], high=[-1, 0]))
numpy_val1c = as_floatX(numpy_rng.uniform(low=[-4.], high=[-1]))
assert numpy.all(val0c == numpy_val0c)
assert numpy.all(val1c == numpy_val1c)
assert np.all(val0c == numpy_val0c)
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, 2])
self.assertRaises(ValueError, fc, post1c, [-4., -2], [-1, 0], [2, 1])
......@@ -699,8 +699,8 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, low, high], [post_r, out],
accept_inplace=True)
rng_state0 = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng_state0 = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0, [-5, .5, 0, 1], [[1.]])
post1, val1 = f(post0, [.9], [[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):
numpy_val2 = numpy_rng.uniform(low=[-5, .5, 0, 1],
high=[[1.], [1.1], [1.5]])
assert numpy.all(val0 == numpy_val0), (val0, numpy_val0)
assert numpy.all(val1 == numpy_val1)
assert numpy.all(val2 == numpy_val2)
assert np.all(val0 == numpy_val0), (val0, numpy_val0)
assert np.all(val1 == numpy_val1)
assert np.all(val2 == numpy_val2)
def test_uniform_vector(self):
rng_R = random_state_type()
......@@ -724,22 +724,22 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True)
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])
high_val = as_floatX([1.1, 2.2, 3.3])
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,)
rng0, val0 = f(rng, low_val, 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,)
rng1, val1 = f(rng0, low_val[:-1], high_val[:-1])
numpy_val1 = as_floatX(numpy_rng.uniform(low=low_val[:-1],
high=high_val[:-1]))
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = compile.function([rng_R, low, high],
......@@ -748,7 +748,7 @@ class T_random_function(utt.InferShapeTester):
rng2, val2 = g(rng1, low_val, high_val)
numpy_val2 = as_floatX(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, rng2, low_val[:-1], high_val[:-1])
def test_binomial_vector(self):
......@@ -761,19 +761,19 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True)
n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
prob_val = np.asarray([.1, .2, .3], dtype=config.floatX)
rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,)
rng0, val0 = f(rng, n_val, 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,)
rng1, val1 = f(rng0, n_val[:-1], 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
g = compile.function([rng_R, n, prob],
......@@ -781,7 +781,7 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True)
rng2, val2 = g(rng1, n_val, prob_val)
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])
def test_normal_vector(self):
......@@ -794,35 +794,35 @@ class T_random_function(utt.InferShapeTester):
accept_inplace=True)
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]
std_val = as_floatX([.1, .2, .3])
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,)
rng0, val0 = f(rng, avg_val, std_val)
numpy_val0 = as_floatX(numpy_rng.normal(loc=as_floatX(avg_val),
scale=as_floatX(std_val)))
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
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]),
dtype=theano.config.floatX)
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = compile.function([rng_R, avg, std],
normal(rng_R, avg=avg, std=std, size=(3,)),
accept_inplace=True)
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,)),
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])
def test_random_integers_vector(self):
......@@ -836,29 +836,29 @@ class T_random_function(utt.InferShapeTester):
low_val = [100, 200, 300]
high_val = [110, 220, 330]
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,)
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)])
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
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])])
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = compile.function([rng_R, low, high],
random_integers(rng_R, low=low, high=high, size=(3,)),
accept_inplace=True)
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)])
assert numpy.all(val2 == numpy_val2)
assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, low_val[:-1], high_val[:-1])
# Vectorized permutation don't make sense: the only parameter, n,
......@@ -875,30 +875,30 @@ class T_random_function(utt.InferShapeTester):
n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX)
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
pvals_val = np.asarray(pvals_val, dtype=config.floatX)
rng = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(utt.fetch_seed())
# Arguments of size (3,)
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)])
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
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])])
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = compile.function([rng_R, n, pvals],
multinomial(rng_R, n=n, pvals=pvals, size=(3,)),
accept_inplace=True)
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)])
assert numpy.all(val2 == numpy_val2)
assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, rng2, n_val[:-1], pvals_val[:-1])
def test_multinomial_tensor3_a(self):
......@@ -914,15 +914,15 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, pvals], [post_r, out],
accept_inplace=True)
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.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)
new_rng, draw = f(rng, pvals_val)
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):
# Test the examples given in the multinomial documentation regarding
......@@ -937,15 +937,15 @@ class T_random_function(utt.InferShapeTester):
f = compile.function([rng_R, pvals], [post_r, out],
accept_inplace=True)
rng = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.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)
out_rng, draw = f(rng, pvals_val)
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):
rng_R = random_state_type()
......@@ -956,13 +956,13 @@ class T_random_function(utt.InferShapeTester):
assert out.dtype == 'int8'
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)
assert val0.dtype == 'int8'
rng1, val1 = f(rng0, 255, 257)
assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1)
assert np.all(abs(val1) <= 1)
def test_dtype_normal_uniform_687(self):
# Regression test for #687.
......@@ -978,7 +978,7 @@ class T_random_function(utt.InferShapeTester):
def test_infer_shape(self):
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
post_r, out = uniform(rng_R)
......
......@@ -3,7 +3,7 @@ __docformat__ = "restructuredtext en"
import sys
import unittest
import numpy
import numpy as np
from theano.tensor import raw_random
from theano.tensor.shared_randomstreams import RandomStreams
......@@ -27,12 +27,12 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], rv_n, no_default_updates=True) #Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
assert numpy.all(f() != f())
assert numpy.all(g() == g())
assert numpy.all(abs(nearly_zeros()) < 1e-5)
assert np.all(f() != f())
assert np.all(g() == g())
assert np.all(abs(nearly_zeros()) < 1e-5)
assert isinstance(rv_u.rng.get_value(borrow=True),
numpy.random.RandomState)
np.random.RandomState)
def test_basics(self):
random = RandomStreams(utt.fetch_seed())
......@@ -44,20 +44,20 @@ class T_SharedRandomStreams(unittest.TestCase):
gn_val0 = gn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
# print fn_val0
numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val0, numpy_val0)
print(fn_val0)
print(numpy_val0)
print(fn_val1)
print(numpy_val1)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val1, numpy_val1)
def test_seed_fn(self):
random = RandomStreams(234)
......@@ -68,16 +68,16 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) #int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) #int() is for 32bit
# print fn_val0
numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2))
# print numpy_val0
assert numpy.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val1, numpy_val1)
def test_getitem(self):
......@@ -87,15 +87,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(utt.fetch_seed())
rng = numpy.random.RandomState()
rng = np.random.RandomState()
rng.set_state(random[out.rng].get_state()) # tests getitem
fn_val0 = fn()
fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val1, numpy_val1)
def test_setitem(self):
......@@ -105,15 +105,15 @@ class T_SharedRandomStreams(unittest.TestCase):
random.seed(888)
rng = numpy.random.RandomState(utt.fetch_seed())
random[out.rng] = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
random[out.rng] = np.random.RandomState(utt.fetch_seed())
fn_val0 = fn()
fn_val1 = fn()
numpy_val0 = rng.uniform(size=(2, 2))
numpy_val1 = rng.uniform(size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val1, numpy_val1)
def test_ndim(self):
"""Test that the behaviour of 'ndim' optional parameter"""
......@@ -130,7 +130,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = fn()
val2 = fn2()
assert numpy.all(val1 == val2)
assert np.all(val1 == val2)
# ndim specified, inconsistent with shape, should raise ValueError
random3 = RandomStreams(utt.fetch_seed())
......@@ -144,13 +144,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.uniform(-1, 1, size=(2, 2))
numpy_val1 = rng.uniform(-1, 1, size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val1, numpy_val1)
def test_normal(self):
"""Test that RandomStreams.normal generates the same results as numpy"""
......@@ -161,13 +161,13 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.normal(-1, 2, size=(2, 2))
numpy_val1 = rng.normal(-1, 2, size=(2, 2))
assert numpy.allclose(fn_val0, numpy_val0)
assert numpy.allclose(fn_val1, numpy_val1)
assert np.allclose(fn_val0, numpy_val0)
assert np.allclose(fn_val1, numpy_val1)
def test_random_integers(self):
# Test that RandomStreams.random_integers generates the same
......@@ -180,14 +180,14 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.randint(-5, 6, size=(20, 20))
numpy_val1 = rng.randint(-5, 6, size=(20, 20))
assert numpy.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
def test_choice(self):
"""Test that RandomStreams.choice generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated.
......@@ -196,30 +196,30 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.choice(10, (11, 8), True, None)
numpy_val1 = rng.choice(10, (11, 8), True, None)
assert numpy.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
def test_poisson(self):
"""Test that RandomStreams.poisson generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated.
random = RandomStreams(utt.fetch_seed())
fn = function([], random.poisson(lam=5, size=(11, 8)))
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.poisson(lam=5, size=(11, 8))
numpy_val1 = rng.poisson(lam=5, size=(11, 8))
assert numpy.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
def test_permutation(self):
"""Test that RandomStreams.permutation generates the same results as numpy"""
......@@ -230,15 +230,15 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
# rng.permutation outputs one vector at a time, so we iterate.
numpy_val0 = numpy.asarray([rng.permutation(10) for i in range(20)])
numpy_val1 = numpy.asarray([rng.permutation(10) for i in range(20)])
numpy_val0 = np.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 numpy.all(fn_val1 == numpy_val1)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
def test_multinomial(self):
"""Test that RandomStreams.multinomial generates the same results as numpy"""
......@@ -249,39 +249,39 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = 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 numpy.all(fn_val1 == numpy_val1)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
def test_shuffle_row_elements(self):
"""Test that RandomStreams.shuffle_row_elements generates the right results"""
# 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.
# 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.
random = RandomStreams(utt.fetch_seed())
m_input = tensor.dmatrix()
f = function([m_input], random.shuffle_row_elements(m_input), updates=random.updates())
# 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))
fn_mval0 = f(in_mval)
fn_mval1 = f(in_mval)
print(in_mval[0])
print(fn_mval0[0])
print(fn_mval1[0])
assert not numpy.all(in_mval == fn_mval0)
assert not numpy.all(in_mval == fn_mval1)
assert not numpy.all(fn_mval0 == fn_mval1)
assert not np.all(in_mval == fn_mval0)
assert not np.all(in_mval == fn_mval1)
assert not np.all(fn_mval0 == fn_mval1)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed))
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed))
numpy_mval0 = in_mval.copy()
numpy_mval1 = in_mval.copy()
for row in numpy_mval0:
......@@ -289,10 +289,10 @@ class T_SharedRandomStreams(unittest.TestCase):
for row in numpy_mval1:
rng.shuffle(row)
assert numpy.all(numpy_mval0 == fn_mval0)
assert numpy.all(numpy_mval1 == fn_mval1)
assert np.all(numpy_mval0 == fn_mval0)
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.
random1 = RandomStreams(utt.fetch_seed())
v_input = tensor.dvector()
......@@ -301,12 +301,12 @@ class T_SharedRandomStreams(unittest.TestCase):
in_vval = val_rng.uniform(-3, 3, size=(12,))
fn_vval = f1(in_vval)
numpy_vval = in_vval.copy()
vrng = numpy.random.RandomState(int(rng_seed))
vrng = np.random.RandomState(int(rng_seed))
vrng.shuffle(numpy_vval)
print(in_vval)
print(fn_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
# matrices, or vice versa, raises a TypeError
......@@ -320,10 +320,10 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_a = function([], out_a)
fn_a_val0 = 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)
assert numpy.all(abs(nearly_zeros()) < 1e-5)
assert np.all(abs(nearly_zeros()) < 1e-5)
# Explicit updates #1
random_b = RandomStreams(utt.fetch_seed())
......@@ -331,8 +331,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_b = function([], out_b, updates=random_b.updates())
fn_b_val0 = fn_b()
fn_b_val1 = fn_b()
assert numpy.all(fn_b_val0 == fn_a_val0)
assert numpy.all(fn_b_val1 == fn_a_val1)
assert np.all(fn_b_val0 == fn_a_val0)
assert np.all(fn_b_val1 == fn_a_val1)
# Explicit updates #2
random_c = RandomStreams(utt.fetch_seed())
......@@ -340,8 +340,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_c = function([], out_c, updates=[out_c.update])
fn_c_val0 = fn_c()
fn_c_val1 = fn_c()
assert numpy.all(fn_c_val0 == fn_a_val0)
assert numpy.all(fn_c_val1 == fn_a_val1)
assert np.all(fn_c_val0 == fn_a_val0)
assert np.all(fn_c_val1 == fn_a_val1)
# No updates at all
random_d = RandomStreams(utt.fetch_seed())
......@@ -349,8 +349,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_d = function([], out_d, no_default_updates=True)
fn_d_val0 = fn_d()
fn_d_val1 = fn_d()
assert numpy.all(fn_d_val0 == fn_a_val0)
assert numpy.all(fn_d_val1 == fn_d_val0)
assert np.all(fn_d_val0 == fn_a_val0)
assert np.all(fn_d_val1 == fn_d_val0)
# No updates for out
random_e = RandomStreams(utt.fetch_seed())
......@@ -358,8 +358,8 @@ class T_SharedRandomStreams(unittest.TestCase):
fn_e = function([], out_e, no_default_updates=[out_e.rng])
fn_e_val0 = fn_e()
fn_e_val1 = fn_e()
assert numpy.all(fn_e_val0 == fn_a_val0)
assert numpy.all(fn_e_val1 == fn_e_val0)
assert np.all(fn_e_val0 == fn_a_val0)
assert np.all(fn_e_val1 == fn_e_val0)
def test_symbolic_shape(self):
random = RandomStreams(utt.fetch_seed())
......@@ -407,21 +407,21 @@ class T_SharedRandomStreams(unittest.TestCase):
g = function([], random.multinomial())
# seed_rng is generator for generating *seeds* for RandomStates
seed_rng = numpy.random.RandomState(utt.fetch_seed())
uniform_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = numpy.random.RandomState(int(seed_rng.randint(2**30)))
seed_rng = np.random.RandomState(utt.fetch_seed())
uniform_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
multinomial_rng = np.random.RandomState(int(seed_rng.randint(2**30)))
val0 = f()
val1 = f()
numpy_val0 = uniform_rng.uniform()
numpy_val1 = uniform_rng.uniform()
assert numpy.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1)
assert np.allclose(val0, numpy_val0)
assert np.allclose(val1, numpy_val1)
for i in range(10): # every test has 50% chance of passing even with non-matching random states
val2 = g()
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):
random = RandomStreams(utt.fetch_seed())
......@@ -430,27 +430,27 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 1
f = function([low], out)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
val0 = f([-5, .5, 0, 1])
val1 = f([.9])
numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1)
numpy_val1 = numpy_rng.uniform(low=[.9], high=1)
assert numpy.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1)
assert np.all(val0 == numpy_val0)
assert np.all(val1 == numpy_val1)
high = tensor.vector()
outb = random.uniform(low=low, high=high)
assert outb.ndim == 1
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])
val1b = fb([-4.], [-1])
numpy_val0b = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1b = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0b == numpy_val0b)
assert numpy.all(val1b == numpy_val1b)
assert np.all(val0b == numpy_val0b)
assert np.all(val1b == numpy_val1b)
self.assertRaises(ValueError, fb, [-4., -2], [-1, 0, 1])
# TODO: do we want that?
#self.assertRaises(ValueError, fb, [-4., -2], [-1])
......@@ -459,13 +459,13 @@ class T_SharedRandomStreams(unittest.TestCase):
outc = random.uniform(low=low, high=high, size=size, ndim=1)
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])
val1c = fc([-4.], [-1], [1])
numpy_val0c = numpy_rng.uniform(low=[-4., -2], high=[-1, 0])
numpy_val1c = numpy_rng.uniform(low=[-4.], high=[-1])
assert numpy.all(val0c == numpy_val0c)
assert numpy.all(val1c == numpy_val1c)
assert np.all(val0c == numpy_val0c)
assert np.all(val1c == numpy_val1c)
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], [2, 1])
......@@ -481,8 +481,8 @@ class T_SharedRandomStreams(unittest.TestCase):
assert out.ndim == 2
f = function([low, high], out)
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = numpy.random.RandomState(int(rng_seed))
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = np.random.RandomState(int(rng_seed))
val0 = f([-5, .5, 0, 1], [[1.]])
val1 = f([.9], [[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):
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]])
assert numpy.all(val0 == numpy_val0)
assert numpy.all(val1 == numpy_val1)
assert numpy.all(val2 == numpy_val2)
assert np.all(val0 == numpy_val0)
assert np.all(val1 == numpy_val1)
assert np.all(val2 == numpy_val2)
def test_uniform_vector(self):
random = RandomStreams(utt.fetch_seed())
......@@ -505,29 +505,29 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [.1, .2, .3]
high_val = [1.1, 2.2, 3.3]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(low_val, high_val)
numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
print('THEANO', val0)
print('NUMPY', numpy_val0)
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
print('THEANO', val1)
print('NUMPY', numpy_val1)
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([low, high], random.uniform(low=low, high=high, size=(3,)))
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,))
assert numpy.all(val2 == numpy_val2)
assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
def test_binomial_vector(self):
......@@ -539,26 +539,26 @@ class T_SharedRandomStreams(unittest.TestCase):
f = function([n, prob], out)
n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
prob_val = np.asarray([.1, .2, .3], dtype=config.floatX)
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(n_val, 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,)
val1 = f(n_val[:-1], 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
g = function([n, prob], random.binomial(n=n, p=prob, size=(3,)))
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,))
assert numpy.all(val2 == numpy_val2)
assert np.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1])
def test_normal_vector(self):
......@@ -571,25 +571,25 @@ class T_SharedRandomStreams(unittest.TestCase):
avg_val = [1, 2, 3]
std_val = [.1, .2, .3]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(avg_val, 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,)
val1 = f(avg_val[:-1], 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
g = function([avg, std], random.normal(avg=avg, std=std, size=(3,)))
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,))
assert numpy.allclose(val2, numpy_val2)
assert np.allclose(val2, numpy_val2)
self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1])
def test_random_integers_vector(self):
......@@ -602,28 +602,28 @@ class T_SharedRandomStreams(unittest.TestCase):
low_val = [100, 200, 300]
high_val = [110, 220, 330]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
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)])
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
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])])
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([low, high], random.random_integers(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = np.asarray([numpy_rng.randint(low=lv, high=hv+1)
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])
# Vectorized permutation don't make sense: the only parameter, n,
......@@ -639,29 +639,29 @@ class T_SharedRandomStreams(unittest.TestCase):
n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
pvals_val = np.asarray(pvals_val, dtype=config.floatX)
seed_gen = np.random.RandomState(utt.fetch_seed())
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
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)])
assert numpy.all(val0 == numpy_val0)
assert np.all(val0 == numpy_val0)
# arguments of size (2,)
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])])
assert numpy.all(val1 == numpy_val1)
assert np.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,)))
val2 = g(n_val, pvals_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
numpy_rng = np.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = np.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
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])
def test_dtype(self):
......@@ -677,7 +677,7 @@ class T_SharedRandomStreams(unittest.TestCase):
val1 = f(255, 257)
assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1)
assert np.all(abs(val1) <= 1)
def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed())
......@@ -704,11 +704,11 @@ class T_SharedRandomStreams(unittest.TestCase):
outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX
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
def test_shared_constructor_borrow(self):
rng = numpy.random.RandomState(123)
rng = np.random.RandomState(123)
s_rng_default = shared(rng)
s_rng_True = shared(rng, borrow=True)
s_rng_False = shared(rng, borrow=False)
......@@ -728,7 +728,7 @@ class T_SharedRandomStreams(unittest.TestCase):
def test_get_value_borrow(self):
rng = numpy.random.RandomState(123)
rng = np.random.RandomState(123)
s_rng = shared(rng)
r_ = s_rng.container.storage[0]
......@@ -745,7 +745,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand()
def test_get_value_internal_type(self):
rng = numpy.random.RandomState(123)
rng = np.random.RandomState(123)
s_rng = shared(rng)
# there is no special behaviour required of return_internal_type
......@@ -765,11 +765,11 @@ class T_SharedRandomStreams(unittest.TestCase):
assert r_.rand() == r_F.rand()
def test_set_value_borrow(self):
rng = numpy.random.RandomState(123)
rng = np.random.RandomState(123)
s_rng = shared(rng)
new_rng = numpy.random.RandomState(234234)
new_rng = np.random.RandomState(234234)
# Test the borrow contract is respected:
# assigning with borrow=False makes a copy
......@@ -778,7 +778,7 @@ class T_SharedRandomStreams(unittest.TestCase):
assert new_rng.randn() == s_rng.container.storage[0].randn()
# 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)
assert rr is s_rng.container.storage[0]
......@@ -811,7 +811,7 @@ class T_SharedRandomStreams(unittest.TestCase):
for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):
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__':
......
from __future__ import absolute_import, print_function, division
import numpy
import numpy as np
import unittest
import warnings
......@@ -24,7 +24,7 @@ def makeSharedTester(shared_constructor_,
test_internal_type_,
theano_fct_,
ref_fct_,
cast_value_=numpy.asarray,
cast_value_=np.asarray,
op_by_matrix_=False,
name=None,
):
......@@ -82,8 +82,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x)
......@@ -94,17 +94,17 @@ def makeSharedTester(shared_constructor_,
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
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)
x /= values_to_div
total_val_2 = total_func()
# 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)
......@@ -113,7 +113,7 @@ def makeSharedTester(shared_constructor_,
total_val_3 = total_func()
# 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
x = x_shared.get_value(borrow=True)
......@@ -122,17 +122,17 @@ def makeSharedTester(shared_constructor_,
# this is not required by the contract but it is a feature we've
# implemented for some type of SharedVariable.
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:
assert numpy.allclose(x_ref, total_func())
assert np.allclose(x_ref, total_func())
def test_shape(self):
dtype = self.dtype
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x)
......@@ -142,7 +142,7 @@ def makeSharedTester(shared_constructor_,
f = theano.function([], x_shared.shape)
topo = f.maker.fgraph.toposort()
assert numpy.all(f() == (2, 4))
assert np.all(f() == (2, 4))
if theano.config.mode != 'FAST_COMPILE':
assert len(topo) == 3
assert isinstance(topo[0].op, tensor.opt.Shape_i)
......@@ -154,8 +154,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x)
......@@ -165,7 +165,7 @@ def makeSharedTester(shared_constructor_,
f = theano.function([], x_shared.shape[1])
topo = f.maker.fgraph.toposort()
assert numpy.all(f() == (4))
assert np.all(f() == (4))
if theano.config.mode != 'FAST_COMPILE':
assert len(topo) == 1
assert isinstance(topo[0].op, tensor.opt.Shape_i)
......@@ -175,8 +175,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x)
......@@ -193,12 +193,12 @@ def makeSharedTester(shared_constructor_,
if self.op_by_matrix:
# supported for cudandarray, but not ndarray.
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
# this is not required by the contract but it is a feature we can
# 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)
assert self.test_internal_type(x)
......@@ -206,7 +206,7 @@ def makeSharedTester(shared_constructor_,
x /= values_to_div # supported by ndarray and CudaNdarray
# 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):
"""
......@@ -216,8 +216,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x_orig = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x_orig = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x_cast = self.cast_value(x_orig)
if self.shared_constructor_accept_ndarray:
x_shared = self.shared_constructor(x_orig, borrow=False)
......@@ -231,8 +231,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(0, 1, [2, 4]), dtype=dtype)
x = self.cast_value(x)
x_orig = x
......@@ -247,7 +247,7 @@ def makeSharedTester(shared_constructor_,
values_to_div = .5
if self.op_by_matrix:
# 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)
# test if that theano shared variable optimize set_value(borrow=True)
......@@ -260,7 +260,7 @@ def makeSharedTester(shared_constructor_,
assert x is get_x
else:
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)
get_x = x_shared.get_value(borrow=True, return_internal_type=True)
......@@ -280,8 +280,8 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
x = self.cast_value(x)
x_ref = self.ref_fct(x)
......@@ -293,20 +293,20 @@ def makeSharedTester(shared_constructor_,
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
if self.op_by_matrix:
# 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)
x /= values_to_div
# not required by the contract but it is a feature we've implemented
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:
assert numpy.allclose(x_ref, total_func())
assert np.allclose(x_ref, total_func())
def test_inplace_set_value(self):
"""
......@@ -319,25 +319,25 @@ def makeSharedTester(shared_constructor_,
shp = (100//4, 1024) # 100KB
x = numpy.zeros(shp, dtype=dtype)
x = np.zeros(shp, dtype=dtype)
x = self.cast_value(x)
x_shared = self.shared_constructor(x, borrow=True)
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':
# sparse matrix don't support inplace affectation
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!
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))
nd[0] += 1
x_shared.container.value[0] = nd[0]
assert (numpy.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)[0]) == nd[0]).all()
assert (np.asarray(x_shared.get_value(borrow=True)[1:]) == nd[1:]).all()
# This should always share value!
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))
......@@ -347,7 +347,7 @@ def makeSharedTester(shared_constructor_,
nd += 1
# 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
#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])
x_shared.get_value(borrow=True)
......@@ -355,7 +355,7 @@ def makeSharedTester(shared_constructor_,
nd += 1
old_data = x_shared.container.storage[0]
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)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace
......@@ -364,7 +364,7 @@ def makeSharedTester(shared_constructor_,
nd += 1
old_data = x_shared.container.storage[0]
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)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_cast_value_inplace
......@@ -372,7 +372,7 @@ def makeSharedTester(shared_constructor_,
nd += 1
old_data = x_shared.container.storage[0]
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)))
assert may_share_memory(old_data, x_shared.container.storage[0]) == self.set_value_inplace
......@@ -380,7 +380,7 @@ def makeSharedTester(shared_constructor_,
nd += 1
old_data = x_shared.container.storage[0]
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
def test_specify_shape(self):
......@@ -388,19 +388,19 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x1_1 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
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)
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)
# Test that we can replace with values of the same shape
x1_shared = self.shared_constructor(x1_1)
x1_specify_shape = tensor.specify_shape(x1_shared, x1_1.shape)
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))
shape_op_fct = theano.function([], x1_shared.shape)
topo = shape_op_fct.maker.fgraph.toposort()
......@@ -412,14 +412,14 @@ def makeSharedTester(shared_constructor_,
# Test that we forward the input
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))
topo_specify = specify_shape_fct.maker.fgraph.toposort()
assert len(topo_specify) == 2
# Test that we put the shape info into the graph
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()
if theano.config.mode != 'FAST_COMPILE':
assert len(topo_cst) == 1
......@@ -454,12 +454,12 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
x1_1 = numpy.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
x1_1 = np.asarray(rng.uniform(1, 2, [4, 2]), dtype=dtype)
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)
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)
# Test that we can replace with values of the same shape
......@@ -468,7 +468,7 @@ def makeSharedTester(shared_constructor_,
(tensor.as_tensor_variable(x1_1.shape[0]),
x1_shared.shape[1]))
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_2))
shape_op_fct = theano.function([], x1_shared.shape)
......@@ -484,7 +484,7 @@ def makeSharedTester(shared_constructor_,
specify_shape_fct = theano.function([], x1_specify_shape)
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))
topo_specify = specify_shape_fct.maker.fgraph.toposort()
if theano.config.mode != 'FAST_COMPILE':
......@@ -493,7 +493,7 @@ def makeSharedTester(shared_constructor_,
# Test that we put the shape info into the graph
shape_constant_fct = theano.function([], x1_specify_shape.shape)
# 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()
if theano.config.mode != 'FAST_COMPILE':
assert len(topo_cst) == 2
......@@ -516,14 +516,14 @@ def makeSharedTester(shared_constructor_,
if dtype is None:
dtype = theano.config.floatX
rng = numpy.random.RandomState(utt.fetch_seed())
a = numpy.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
rng = np.random.RandomState(utt.fetch_seed())
a = np.asarray(rng.uniform(1, 2, [40, 40]), dtype=dtype)
a = self.cast_value(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_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_shared = self.shared_constructor(s)
f = theano.function([],
......@@ -546,7 +546,7 @@ def makeSharedTester(shared_constructor_,
+ s_shared_specify)])
topo = f.maker.fgraph.toposort()
shp = f()
assert numpy.all(shp == (40, 40))
assert np.all(shp == (40, 40))
if theano.config.mode != 'FAST_COMPILE':
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))
......@@ -562,7 +562,7 @@ def makeSharedTester(shared_constructor_,
+ s_shared_specify)])
topo = f.maker.fgraph.toposort()
shp = f()
assert numpy.all(shp == (40, 40))
assert np.all(shp == (40, 40))
if theano.config.mode != 'FAST_COMPILE':
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))
......@@ -578,9 +578,9 @@ def makeSharedTester(shared_constructor_,
shp = (1024, 1024)
# Test the case with all zeros element
rng = numpy.random.RandomState(utt.fetch_seed())
for x in [numpy.asarray(rng.rand(*shp), dtype=dtype),
numpy.zeros(shp, dtype=dtype)]:
rng = np.random.RandomState(utt.fetch_seed())
for x in [np.asarray(rng.rand(*shp), dtype=dtype),
np.zeros(shp, dtype=dtype)]:
zeros = (x == 0).all()
x = self.cast_value(x)
x_shared = self.shared_constructor(x, borrow=True)
......@@ -592,7 +592,7 @@ def makeSharedTester(shared_constructor_,
assert x_shared.type.values_eq(x, x)
assert x_shared.type.values_eq_approx(x, x)
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_approx(x, y)
......@@ -612,11 +612,11 @@ test_shared_options = makeSharedTester(
set_value_inplace_=False,
set_cast_value_inplace_=False,
shared_constructor_accept_ndarray_=True,
internal_type_=numpy.ndarray,
test_internal_type_=lambda a: isinstance(a, numpy.ndarray),
internal_type_=np.ndarray,
test_internal_type_=lambda a: isinstance(a, np.ndarray),
theano_fct_=lambda a: a*2,
ref_fct_=lambda a: numpy.asarray((a*2)),
cast_value_=numpy.asarray,
ref_fct_=lambda a: np.asarray((a*2)),
cast_value_=np.asarray,
op_by_matrix_=False,
name='test_shared_options')
......@@ -624,4 +624,4 @@ test_shared_options = makeSharedTester(
def test_scalar_shared_options():
# Simple test to make sure we do not loose that fonctionality.
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
import unittest
import numpy
import numpy as np
import numpy.linalg
from numpy.testing import assert_array_almost_equal
from numpy.testing import dec, assert_array_equal, assert_allclose
......@@ -35,25 +35,25 @@ def check_lower_triangular(pd, ch_f):
ch = ch_f(pd)
assert ch[0, pd.shape[1] - 1] == 0
assert ch[pd.shape[0] - 1, 0] != 0
assert numpy.allclose(numpy.dot(ch, ch.T), pd)
assert not numpy.allclose(numpy.dot(ch.T, ch), pd)
assert np.allclose(np.dot(ch, ch.T), pd)
assert not np.allclose(np.dot(ch.T, ch), pd)
def check_upper_triangular(pd, ch_f):
ch = ch_f(pd)
assert ch[4, 0] == 0
assert ch[0, 4] != 0
assert numpy.allclose(numpy.dot(ch.T, ch), pd)
assert not numpy.allclose(numpy.dot(ch, ch.T), pd)
assert np.allclose(np.dot(ch.T, ch), pd)
assert not np.allclose(np.dot(ch, ch.T), pd)
def test_cholesky():
if not imported_scipy:
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)
pd = numpy.dot(r, r.T)
pd = np.dot(r, r.T)
x = tensor.matrix()
chol = cholesky(x)
# Check the default.
......@@ -72,7 +72,7 @@ def test_cholesky():
def test_cholesky_grad():
if not imported_scipy:
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)
# The dots are inside the graph since Cholesky needs separable matrices
......@@ -93,7 +93,7 @@ def test_cholesky_and_cholesky_grad_shape():
if not imported_scipy:
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()
for l in (cholesky(x), Cholesky(lower=True)(x), Cholesky(lower=False)(x)):
f_chol = theano.function([x], l.shape)
......@@ -107,9 +107,9 @@ def test_cholesky_and_cholesky_grad_shape():
assert sum([node.op.__class__ == CholeskyGrad
for node in topo_cholgrad]) == 0
for shp in [2, 3, 5]:
m = numpy.cov(rng.randn(shp, shp + 10)).astype(config.floatX)
yield numpy.testing.assert_equal, f_chol(m), (shp, shp)
yield numpy.testing.assert_equal, f_cholgrad(m), (shp, shp)
m = np.cov(rng.randn(shp, shp + 10)).astype(config.floatX)
yield np.testing.assert_equal, f_chol(m), (shp, shp)
yield np.testing.assert_equal, f_cholgrad(m), (shp, shp)
def test_eigvalsh():
......@@ -121,13 +121,13 @@ def test_eigvalsh():
B = theano.tensor.dmatrix('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 = 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)
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
# complain, as this isn't a valid ndarray.
......@@ -136,7 +136,7 @@ def test_eigvalsh():
f = function([A], eigvalsh(A, B))
w = f(a)
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():
......@@ -144,12 +144,12 @@ def test_eigvalsh_grad():
raise SkipTest("Scipy needed for the geigvalsh op.")
import scipy.linalg
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
a = rng.randn(5, 5)
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]),
[a, b], rng=numpy.random)
[a, b], rng=np.random)
class test_Solve(utt.InferShapeTester):
......@@ -161,27 +161,27 @@ class test_Solve(utt.InferShapeTester):
def test_infer_shape(self):
if not imported_scipy:
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()
b = theano.tensor.matrix()
self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs
# A must be square
[numpy.asarray(rng.rand(5, 5),
[np.asarray(rng.rand(5, 5),
dtype=config.floatX),
numpy.asarray(rng.rand(5, 1),
np.asarray(rng.rand(5, 1),
dtype=config.floatX)],
self.op_class,
warn=False)
rng = numpy.random.RandomState(utt.fetch_seed())
rng = np.random.RandomState(utt.fetch_seed())
A = theano.tensor.matrix()
b = theano.tensor.vector()
self._compile_and_check([A, b], # theano.function inputs
[self.op(A, b)], # theano.function outputs
# A must be square
[numpy.asarray(rng.rand(5, 5),
[np.asarray(rng.rand(5, 5),
dtype=config.floatX),
numpy.asarray(rng.rand(5),
np.asarray(rng.rand(5),
dtype=config.floatX)],
self.op_class,
warn=False)
......@@ -189,7 +189,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_correctness(self):
if not imported_scipy:
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()
b = theano.tensor.matrix()
y = self.op(A, b)
......@@ -205,23 +205,23 @@ class test_Solve(utt.InferShapeTester):
y_upper = self.op(U, b)
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
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:
A_val = numpy.dot(A_val.transpose(), A_val)
assert numpy.allclose(scipy.linalg.solve(A_val, b_val),
A_val = np.dot(A_val.transpose(), A_val)
assert np.allclose(scipy.linalg.solve(A_val, b_val),
gen_solve_func(A_val, b_val))
# 2-test lower traingular case
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))
# 3-test upper traingular case
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))
def test_solve_dtype(self):
......@@ -232,8 +232,8 @@ class test_Solve(utt.InferShapeTester):
'int8', 'int16', 'int32', 'int64',
'float16', 'float32', 'float64']
A_val = numpy.eye(2)
b_val = numpy.ones((2, 1))
A_val = np.eye(2)
b_val = np.ones((2, 1))
# try all dtype combinations
for A_dtype, b_dtype in itertools.product(dtypes, dtypes):
......@@ -249,11 +249,11 @@ class test_Solve(utt.InferShapeTester):
# ensure diagonal elements of A relatively large to avoid numerical
# precision issues
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':
A_val = numpy.tril(A_val)
A_val = np.tril(A_val)
elif A_structure == 'upper_triangular':
A_val = numpy.triu(A_val)
A_val = np.triu(A_val)
if n is None:
b_val = rng.normal(size=m).astype(config.floatX)
else:
......@@ -267,7 +267,7 @@ class test_Solve(utt.InferShapeTester):
def test_solve_grad(self):
if not imported_scipy:
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']
for A_structure in structures:
lower = (A_structure == 'lower_triangular')
......@@ -282,7 +282,7 @@ class test_Solve(utt.InferShapeTester):
def test_expm():
if not imported_scipy:
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)
ref = scipy.linalg.expm(A)
......@@ -292,14 +292,14 @@ def test_expm():
expm_f = function([x], m)
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():
# with symmetric matrix (real eigenvectors)
if not imported_scipy:
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.
A = rng.randn(5, 5)
A = A + A.T
......@@ -311,12 +311,12 @@ def test_expm_grad_2():
# with non-symmetric matrix with real eigenspecta
if not imported_scipy:
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.
A = rng.randn(5, 5)
w = rng.randn(5)**2
A = (numpy.diag(w**0.5)).dot(A + A.T).dot(numpy.diag(w**(-0.5)))
assert not numpy.allclose(A, A.T)
A = (np.diag(w**0.5)).dot(A + A.T).dot(np.diag(w**(-0.5)))
assert not np.allclose(A, A.T)
tensor.verify_grad(expm, [A], rng=rng)
......@@ -325,7 +325,7 @@ def test_expm_grad_3():
# with non-symmetric matrix (complex eigenvectors)
if not imported_scipy:
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.
A = rng.randn(5, 5)
......@@ -334,7 +334,7 @@ def test_expm_grad_3():
class TestKron(utt.InferShapeTester):
rng = numpy.random.RandomState(43)
rng = np.random.RandomState(43)
def setUp(self):
super(TestKron, self).setUp()
......@@ -347,7 +347,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]:
x = tensor.tensor(dtype='floatX',
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)]:
if len(shp0) + len(shp1) == 2:
continue
......@@ -360,7 +360,7 @@ class TestKron(utt.InferShapeTester):
# so we have to add a dimension to a and flatten the result.
if len(shp0) + len(shp1) == 3:
scipy_val = scipy.linalg.kron(
a[numpy.newaxis, :], b).flatten()
a[np.newaxis, :], b).flatten()
else:
scipy_val = scipy.linalg.kron(a, b)
utt.assert_allclose(out, scipy_val)
......@@ -369,7 +369,7 @@ class TestKron(utt.InferShapeTester):
for shp0 in [(2, 3)]:
x = tensor.tensor(dtype='floatX',
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)]:
if len(shp0) + len(shp1) == 2:
continue
......@@ -378,4 +378,4 @@ class TestKron(utt.InferShapeTester):
f = function([x, y], kron(x, y))
b = self.rng.rand(*shp1).astype(config.floatX)
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
import sys
import unittest
import numpy
import numpy as np
from nose.plugins.skip import SkipTest
from nose.tools import assert_equal
from numpy.testing import assert_array_equal
......@@ -119,7 +119,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
def test0_err_invalid(self):
# 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:
n[0]
except ValueError as e:
......@@ -128,7 +128,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail()
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
config.compute_test_value = 'off'
try:
......@@ -150,7 +150,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
_logger.setLevel(oldlevel)
def test1_err_subslice(self):
n = self.shared(numpy.ones(3, dtype=self.dtype))
n = self.shared(np.ones(3, dtype=self.dtype))
try:
n[slice(0, slice(1, 2, None), None)]
except Exception:
......@@ -162,7 +162,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail()
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
......@@ -170,24 +170,24 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [0, 1]).all())
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
for idx in [(slice(0, 2), 3), ((slice(0, 2), -1)), (slice(0, 2), -4)]:
t = n[idx] # l]#0:2,3]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
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):
n = self.shared(numpy.ones((), dtype=self.dtype))
n = self.shared(np.ones((), dtype=self.dtype))
t = self.sub([])(n)
self.assertTrue(isinstance(t.owner.op, Subtensor))
self.eval_output_and_check(
t, mode=self.mode.excluding("local_useless_subtensor"))
def test1_err_invalid(self):
n = self.shared(numpy.ones(1, dtype=self.dtype))
n = self.shared(np.ones(1, dtype=self.dtype))
try:
n[0, 0]
except ValueError as e:
......@@ -196,7 +196,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.fail()
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
......@@ -204,7 +204,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(tval == 5.0)
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:]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
......@@ -212,7 +212,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [1.0, 2.0]).all())
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
......@@ -225,7 +225,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue((tval == [0.0, 2.0]).all())
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
config.compute_test_value = 'off'
try:
......@@ -245,7 +245,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
config.compute_test_value = ctv_backup
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
old_stderr = sys.stderr
......@@ -257,107 +257,107 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
sys.stderr = old_stderr
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == ())
self.assertTrue(numpy.all(tval == 2))
self.assertTrue(np.all(tval == 2))
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]
self.assertFalse(any(n.type.broadcastable))
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
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):
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
self.assertFalse(any(n.type.broadcastable))
tval = self.eval_output_and_check(t)
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):
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
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):
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:]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == (1,))
self.assertTrue(numpy.all(tval == 5))
self.assertTrue(np.all(tval == 5))
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
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):
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]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
self.assertTrue(tval.shape == ())
self.assertTrue(numpy.all(tval == 0))
self.assertTrue(np.all(tval == 0))
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)]
self.assertTrue(isinstance(t.owner.op, Subtensor))
tval = self.eval_output_and_check(t)
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):
# 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.
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)])
def test_list_slice(self):
x = theano.tensor.arange(100).reshape((5, 5, 4))
res = x[[slice(1, -1)] * x.ndim].eval()
x = numpy.arange(100).reshape((5, 5, 4))
numpy.allclose(res, x[[slice(1, -1)] * x.ndim])
x = np.arange(100).reshape((5, 5, 4))
np.allclose(res, x[[slice(1, -1)] * x.ndim])
def test_slice_symbol(self):
x = self.shared(numpy.random.rand(5, 4).astype(self.dtype))
y = self.shared(numpy.random.rand(1, 2, 3).astype(self.dtype))
x = self.shared(np.random.rand(5, 4).astype(self.dtype))
y = self.shared(np.random.rand(1, 2, 3).astype(self.dtype))
o = x[:y.shape[0], None, :]
f = theano.function([], o, mode=self.mode)
ret = f()
assert ret.shape == (1, 1, 4)
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)
test_cases = [
(0, Subtensor, self.sub, numpy.index_exp[...]),
(1, Subtensor, self.sub, numpy.index_exp[..., 1]),
(1, Subtensor, self.sub, numpy.index_exp[1, ...]),
(1, Subtensor, self.sub, numpy.index_exp[..., 1, 2, 3]),
(1, Subtensor, self.sub, numpy.index_exp[1, ..., 2, 3]),
(1, Subtensor, self.sub, numpy.index_exp[1, 2, 3, ...]),
(0, Subtensor, self.sub, np.index_exp[...]),
(1, Subtensor, self.sub, np.index_exp[..., 1]),
(1, Subtensor, self.sub, np.index_exp[1, ...]),
(1, Subtensor, self.sub, np.index_exp[..., 1, 2, 3]),
(1, Subtensor, self.sub, np.index_exp[1, ..., 2, 3]),
(1, Subtensor, self.sub, np.index_exp[1, 2, 3, ...]),
(3, DimShuffle, self.dimshuffle,
numpy.index_exp[..., [0, 2, 3]]),
np.index_exp[..., [0, 2, 3]]),
(1, DimShuffle, self.dimshuffle,
numpy.index_exp[numpy.newaxis, ...]),
np.index_exp[np.newaxis, ...]),
(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:
numpy_tval = numpy_n[slice_]
......@@ -379,9 +379,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
interaction with the Subtensor Op (which has no support of its own for
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
n4 = n[newaxis, :, :, :]
......@@ -405,15 +405,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
vs1, vn3, vn4 = theano.function([s], [s1, n3, n4])(-2.0)
assert numpy.all(vs1 == [-2.0])
assert numpy.all(vn3 ==
numpy.arange(24)[newaxis, :, newaxis])
assert numpy.all(vn4 ==
numpy.arange(24).reshape((2, 3, 4))[:, :, :, newaxis])
assert np.all(vs1 == [-2.0])
assert np.all(vn3 ==
np.arange(24)[newaxis, :, newaxis])
assert np.all(vn4 ==
np.arange(24).reshape((2, 3, 4))[:, :, :, newaxis])
def test_grad_1d(self):
subi = 0
data = numpy.asarray(rand(2, 3), dtype=self.dtype)
data = np.asarray(rand(2, 3), dtype=self.dtype)
n = self.shared(data)
z = scal.constant(subi).astype('int32')
t = n[z:, z]
......@@ -425,15 +425,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.ignore_topo)]
if not self.fast_compile:
assert len(topo_) == 6
assert numpy.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1
assert numpy.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1
assert np.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1
assert np.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1
gval = f()
good = numpy.zeros_like(data)
good[subi:, subi] = numpy.exp(data[subi:, subi])
self.assertTrue(numpy.allclose(gval, good), (gval, good))
good = np.zeros_like(data)
good[subi:, subi] = np.exp(data[subi:, subi])
self.assertTrue(np.allclose(gval, good), (gval, good))
def test_grad_2d_inc_set_subtensor(self):
for n_shape, m_shape in [
......@@ -444,11 +444,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
]:
for op in [inc_subtensor, set_subtensor]:
subi = 2
data = numpy.asarray(rand(*n_shape), dtype=self.dtype)
data = np.asarray(rand(*n_shape), dtype=self.dtype)
n = self.shared(data)
z = scal.constant(subi)
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)
gn, gm = theano.tensor.grad(theano.tensor.sum(t), [n, m])
......@@ -456,7 +456,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
utt.verify_grad(lambda nn: op(nn[:z, :z], mv), [data])
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)
t = n[1, 0]
gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n)
......@@ -466,15 +466,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
if not isinstance(node.op, self.ignore_topo)]
if not self.fast_compile:
assert_equal(len(topo_), 6)
assert numpy.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1
assert numpy.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1
assert np.sum([isinstance(node.op, self.inc_sub)
for node in topo_]) == 1
assert np.sum([isinstance(node.op, self.sub)
for node in topo_]) == 1
gval = f()
good = numpy.zeros_like(data)
good[1, 0] = numpy.exp(data[1, 0])
self.assertTrue(numpy.allclose(gval, good), (gval, good))
good = np.zeros_like(data)
good[1, 0] = np.exp(data[1, 0])
self.assertTrue(np.allclose(gval, good), (gval, good))
def test_ok_list(self):
for data, idx in [(rand(4), [1, 0]),
......@@ -492,7 +492,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
(rand(4, 2, 3),
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)
t = n[idx]
......@@ -505,7 +505,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
else:
good = data[idx.data]
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
if type(self.adv_sub1) == tensor.AdvancedSubtensor1:
......@@ -524,7 +524,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
gn = theano.grad(t.sum(), n)
g = self.function([], gn, op=self.adv_incsub1)
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()
utt.verify_grad(lambda m: m[idx],
[data])
......@@ -533,21 +533,21 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
data = rand(4, 2, 3)
idx = [2, 2, 0, 0, 1, 1]
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))
val = self.eval_output_and_check(t, op_type=self.adv_sub1, length=2)
utt.assert_allclose(data[idx[::2]], val)
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])
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]))
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()
t = n[l]
# We test again AdvancedSubtensor1 as we transfer data to the cpu.
......@@ -557,7 +557,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# the grad
g = self.function([l],
inc_subtensor(t, numpy.asarray([[1.]], self.dtype)),
inc_subtensor(t, np.asarray([[1.]], self.dtype)),
op=self.adv_incsub1)
for shp in [[0, 4], [0, -3], [-10]]:
......@@ -565,7 +565,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertRaises(IndexError, g, shp)
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))
idx = tensor.lvector()
t = n[idx]
......@@ -579,10 +579,10 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(isinstance(topo_[0].op, self.adv_sub1))
f_0 = f([0])
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])
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])
# Test the gradient
......@@ -591,30 +591,30 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
g = self.function([idx], gn, op=self.adv_incsub1)
g_0 = g([0])
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])
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]],
[numpy.random.rand(5, 5).astype(self.dtype)])
[np.random.rand(5, 5).astype(self.dtype)])
def fun(x, y):
return advanced_inc_subtensor1(x, y, [1, 3])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2, 5).astype(self.dtype)])
utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
np.random.rand(2, 5).astype(self.dtype)])
def fun(x, y):
return advanced_set_subtensor1(x, y, [1, 3])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2, 5).astype(self.dtype)])
utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
np.random.rand(2, 5).astype(self.dtype)])
# test set_subtensor broadcast
self.dtype = 'float32'
x = tensor.tensor4('x', dtype=self.dtype)
indexes = theano.shared(numpy.int32([1, 2, 3, 4]))
W = self.shared(numpy.random.random(
indexes = theano.shared(np.int32([1, 2, 3, 4]))
W = self.shared(np.random.random(
(10, 10, 3, 3)).astype(self.dtype))
h = x + W
......@@ -625,11 +625,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
N = 3
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):
# 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)
idx = tensor.TensorType(dtype='int64', broadcastable=(True,))()
assert idx.type.broadcastable == (True,)
......@@ -644,7 +644,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.assertTrue(isinstance(topo_[0].op, self.adv_sub1))
f_0 = f([0])
self.assertTrue(f_0.shape == (1, 3))
self.assertTrue(numpy.allclose(f_0, 5))
self.assertTrue(np.allclose(f_0, 5))
# Test the gradient
c = t.sum()
......@@ -652,15 +652,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
g = self.function([idx], gn, op=self.adv_incsub1)
g_0 = g([0])
self.assertTrue(g_0.shape == (4, 3))
self.assertTrue(numpy.allclose(g_0[0], 1))
self.assertTrue(numpy.allclose(g_0[1:], 0))
self.assertTrue(np.allclose(g_0[0], 1))
self.assertTrue(np.allclose(g_0[1:], 0))
@attr('slow')
def test_shape_i_const(self):
# Each axis is treated independently by shape_i/shape operators
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]:
outs = []
shapes = []
......@@ -673,7 +673,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
op=self.ops, N=0)
t_shapes = f()
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
for x in f.maker.fgraph.toposort()]
......@@ -682,7 +682,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
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)
start = tensor.iscalar('b')
stop = tensor.iscalar('e')
......@@ -697,8 +697,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
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 step in [-3, -1, 2, 5]:
assert numpy.all(f(start, stop, step) ==
v_data[start:stop:step].shape)
assert np.all(f(start, stop, step) ==
v_data[start:stop:step].shape)
def test_slice_canonical_form_0(self):
start = tensor.iscalar('b')
......@@ -713,15 +713,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
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 step in [-6, -3, -1, 2, 5]:
out = f(start, stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_1(self):
stop = tensor.iscalar('e')
......@@ -735,14 +735,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]:
out = f(stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[:stop:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_2(self):
start = tensor.iscalar('b')
......@@ -756,14 +756,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]:
out = f(start, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_3(self):
start = tensor.iscalar('b')
......@@ -777,14 +777,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for start 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)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_4(self):
step = tensor.iscalar('s')
......@@ -797,13 +797,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for step in [-6, -3, -1, 2, 5]:
out = f(step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:None:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_5(self):
start = tensor.iscalar('b')
......@@ -816,13 +816,13 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_6(self):
stop = tensor.iscalar('e')
......@@ -835,20 +835,20 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
a = np.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:stop:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
assert np.all(t_out == v_out)
assert np.all(t_out.shape == v_out.shape)
def grad_list_(self, idxs, data):
n = self.shared(data)
for idx in idxs:
# Should stay on the cpu.
idx_ = _shared(numpy.asarray(idx))
idx_ = _shared(np.asarray(idx))
t = n[idx_]
gn = theano.tensor.grad(theano.tensor.sum(theano.tensor.exp(t)), n)
f = self.function([], [gn, gn.shape], op=self.adv_incsub1)
......@@ -861,14 +861,14 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for node in topo])
assert any([isinstance(node.op, self.adv_sub1) for node in topo])
gval, gshape = f()
good = numpy.zeros_like(data)
good = np.zeros_like(data)
# 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:
good[i] += numpy.exp(data[i])
good[i] += np.exp(data[i])
self.assertTrue(gval.ndim == data.ndim)
self.assertTrue(numpy.allclose(gval, good), (gval, good))
self.assertTrue(numpy.allclose(gshape, data.shape))
self.assertTrue(np.allclose(gval, good), (gval, good))
self.assertTrue(np.allclose(gshape, data.shape))
def fct(t):
return theano.tensor.sum(t[idx_])
......@@ -916,7 +916,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
@attr('slow')
def test_grad_list(self):
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])]
for i in range(data.shape[0]):
for j in range(0, data.shape[0], 2):
......@@ -924,11 +924,11 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
self.grad_list_(idxs, data)
data = rand(4, 3)
data = numpy.asarray(data, dtype=self.dtype)
data = np.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data)
data = rand(4, 3, 2)
data = numpy.asarray(data, dtype=self.dtype)
data = np.asarray(data, dtype=self.dtype)
self.grad_list_(idxs, data)
def test_shape_list(self):
......@@ -938,12 +938,12 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
(rand(4, 2, 3), [0, 3]),
(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)
t = n[idx]
f = self.function([], t.shape, op=self.ops, N=0, N_fast=1)
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 inc_slice(*s):
......@@ -958,40 +958,40 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# vector
utt.verify_grad(
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
utt.verify_grad(
inc_slice(slice(1, 2, None), slice(None, None, 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.]]),))
# single element
utt.verify_grad(
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):
"""
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)
f = self.function([], [y],
op=self.inc_sub,
N=1)
out = f()
res = numpy.ones((9, 9))
res = np.ones((9, 9))
res[1::, 1::] = 0
assert numpy.allclose(out, res)
assert np.allclose(out, res)
def test_advanced1_inc_and_set(self):
"""
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_num = []
all_outputs_var = []
......@@ -1001,9 +1001,9 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
for inplace in (False, True):
for data_shape in ((10,), (4, 5), (1, 2, 3), (4, 5, 6, 7)):
data_n_dims = len(data_shape)
data_size = numpy.product(data_shape)
data_size = np.product(data_shape)
# 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)
inc_shapes = [data_shape[i:]
for i in xrange(0, len(data_shape) + 1)]
......@@ -1031,7 +1031,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Corresponding numeric variable.
# If set_instead_of_inc, we want to avoid repeating
# 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,
replace=(not set_instead_of_inc))
idx_num = idx_num.astype('int64')
......@@ -1049,7 +1049,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# The param dtype is needed when inc_shape is empty.
# By default, it would return a float and rng.uniform
# 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.
inc_num = rng.uniform(size=inc_size).astype(self.dtype)
inc_num = inc_num.reshape(inc_shape)
......@@ -1100,7 +1100,7 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
# Ensure calling `f` will not alter `data_num`.
data_num = data_num.copy()
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:
# Sanity check: `data_num` should be intact.
assert (data_num == data_num_init).all()
......@@ -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):
# NB: if this assert fails, it will probably be easier to debug if
# 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):
# Test case provided (and bug detected, gh-607) by John Salvatier
m = matrix('m')
gv = numpy.array([0, 1, 3])
gv = np.array([0, 1, 3])
g = theano.tensor.constant(gv)
i = theano.tensor.lvector('i')
......@@ -1156,8 +1156,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0
m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref)
assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
def test_adv1_inc_sub_notlastdim_2didx(self):
# Test that taking 1-dimensional advanced indexing
......@@ -1181,8 +1181,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0
m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref)
assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
def test_adv1_inc_sub_notlastdim_1_2dval_broadcast(self):
# Test that taking 1-dimensional advanced indexing
......@@ -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):
sub_m = m[:, i]
m1 = set_subtensor(sub_m, numpy.zeros(shp_v))
m2 = inc_subtensor(sub_m, numpy.ones(shp_v))
m1 = set_subtensor(sub_m, np.zeros(shp_v))
m2 = inc_subtensor(sub_m, np.ones(shp_v))
f = theano.function([m, i], [m1, m2])
m_val = rand(3, 5)
......@@ -1216,8 +1216,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0
m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref)
assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
finally:
config.warn.inc_set_subtensor1 = orig_warn
......@@ -1239,8 +1239,8 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
config.warn.inc_set_subtensor1 = False
for i, shp_i, shp_v in zip(sym_i, shape_i, shape_val):
sub_m = m[:, i]
m1 = set_subtensor(sub_m, numpy.zeros(shp_v))
m2 = inc_subtensor(sub_m, numpy.ones(shp_v))
m1 = set_subtensor(sub_m, np.zeros(shp_v))
m2 = inc_subtensor(sub_m, np.ones(shp_v))
f = theano.function([m, i], [m1, m2])
m_val = rand(3, 5)
......@@ -1256,15 +1256,15 @@ class T_subtensor(unittest.TestCase, utt.TestOptimizationMixin):
m1_ref[:, idx] = 0
m2_ref[:, idx] += 1
assert numpy.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert numpy.allclose(m2_val, m2_ref), (m2_val, m2_ref)
assert np.allclose(m1_val, m1_ref), (m1_val, m1_ref)
assert np.allclose(m2_val, m2_ref), (m2_val, m2_ref)
finally:
config.warn.inc_set_subtensor1 = orig_warn
def test_take(self):
a = tensor.matrix()
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):
......@@ -1272,7 +1272,7 @@ class TestIncSubtensor1(unittest.TestCase):
# also tests set_subtensor
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.v = tensor.fvector()
......@@ -1298,7 +1298,7 @@ class TestIncSubtensor1(unittest.TestCase):
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
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):
a = inc_subtensor(self.v[self.adv1q], self.v[self.adv1q])
......@@ -1306,7 +1306,7 @@ class TestIncSubtensor1(unittest.TestCase):
assert a.type == self.v.type
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
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):
a = inc_subtensor(self.v[self.adv1q], 3.0)
......@@ -1314,7 +1314,7 @@ class TestIncSubtensor1(unittest.TestCase):
assert a.type == self.v.type
f = theano.function([self.v, self.adv1q], a, allow_input_downcast=True)
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):
self.assertRaises(TypeError,
......@@ -1327,7 +1327,7 @@ class TestIncSubtensor1(unittest.TestCase):
f = theano.function([self.m, idx], a2)
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)
utt.assert_allclose(a2val[0], mval[0])
......@@ -1427,7 +1427,7 @@ class TestAdvancedSubtensor(unittest.TestCase):
(rand(2, 4, 3),
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)
t = n[0, idx]
......@@ -1439,7 +1439,7 @@ class TestAdvancedSubtensor(unittest.TestCase):
else:
good = data[0, idx.data]
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):
subt = self.v[self.ix2]
......@@ -1450,13 +1450,13 @@ class TestAdvancedSubtensor(unittest.TestCase):
mode=self.mode)
aval = f([.4, .9, .1], [[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):
subt = self.ft4[0, :, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]])
ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v)
rval = ft4v[0, :, ix2v, :]
utt.assert_allclose(rval, aval)
......@@ -1464,8 +1464,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_none_and_matrix(self):
subt = self.ft4[:, None, :, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]])
ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v)
rval = ft4v[:, None, :, ix2v, :]
utt.assert_allclose(rval, aval)
......@@ -1473,8 +1473,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_slice_and_matrix(self):
subt = self.ft4[:, 0:1, self.ix2, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]])
ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v)
rval = ft4v[:, 0:1, ix2v, :]
utt.assert_allclose(rval, aval)
......@@ -1482,8 +1482,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_matrix_and_int(self):
subt = self.ft4[:, :, self.ix2, 0]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]])
ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v)
rval = ft4v[:, :, ix2v, 0]
utt.assert_allclose(rval, aval)
......@@ -1491,8 +1491,8 @@ class TestAdvancedSubtensor(unittest.TestCase):
def test_adv_subtensor_w_matrix_and_none(self):
subt = self.ft4[:, :, self.ix2, None, :]
f = theano.function([self.ft4, self.ix2], subt, mode=self.mode)
ft4v = numpy.random.random((2, 3, 4, 5)).astype('float32')
ix2v = numpy.asarray([[0, 1], [1, 0]])
ft4v = np.random.random((2, 3, 4, 5)).astype('float32')
ix2v = np.asarray([[0, 1], [1, 0]])
aval = f(ft4v, ix2v)
rval = ft4v[:, :, ix2v, None, :]
utt.assert_allclose(rval, aval)
......@@ -1513,10 +1513,10 @@ class TestAdvancedSubtensor(unittest.TestCase):
[.5, .3, .15]],
[1, 2, 1],
[0, 1, 0])
assert numpy.allclose(aval,
[[.4, .9, .1],
[5 * 3, 6, 7],
[.5, .3 * 2, .15]]), aval
assert np.allclose(aval,
[[.4, .9, .1],
[5 * 3, 6, 7],
[.5, .3 * 2, .15]]), aval
def test_inc_adv_subtensor_with_broadcasting(self):
if not config.cxx:
......@@ -1535,11 +1535,11 @@ class TestAdvancedSubtensor(unittest.TestCase):
[1, 2, 1],
[0, 1, 0],
2.1)
assert numpy.allclose(aval,
[[.4, .9, .1],
[5 + 2.1 * 2, 6, 7],
[.5, .3 + 2.1, .15]]), aval
assert numpy.allclose(gval, 3.0), gval
assert np.allclose(aval,
[[.4, .9, .1],
[5 + 2.1 * 2, 6, 7],
[.5, .3 + 2.1, .15]]), aval
assert np.allclose(gval, 3.0), gval
def test_inc_adv_subtensor1_with_broadcasting(self):
if not config.cxx:
......@@ -1557,11 +1557,11 @@ class TestAdvancedSubtensor(unittest.TestCase):
[.5, .3, .15]],
[0, 1, 0],
2.1)
assert numpy.allclose(aval,
[[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2],
[5 + 2.1, 6 + 2.1, 7 + 2.1],
[.5, .3, .15]]), aval
assert numpy.allclose(gval, 9.0), gval
assert np.allclose(aval,
[[.4 + 2.1 * 2, .9 + 2.1 * 2, .1 + 2.1 * 2],
[5 + 2.1, 6 + 2.1, 7 + 2.1],
[.5, .3, .15]]), aval
assert np.allclose(gval, 9.0), gval
def test_inc_adv_subtensor_with_index_broadcasting(self):
if not config.cxx:
......@@ -1578,14 +1578,14 @@ class TestAdvancedSubtensor(unittest.TestCase):
[0, 2, 0],
[[0, 1, 0],
[2, 2, 2]])
assert numpy.allclose(aval,
[[.4 + 2 * 2.1, .9, .1 + 2 * 2.1],
[5, 6, 7],
[.5, .3 + 2.1, .15 + 2.1]]), aval
assert np.allclose(aval,
[[.4 + 2 * 2.1, .9, .1 + 2 * 2.1],
[5, 6, 7],
[.5, .3 + 2.1, .15 + 2.1]]), aval
def test_advanced_indexing(self):
# 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))
b = theano.shared(a)
i = tensor.iscalar()
......@@ -1607,24 +1607,24 @@ class TestAdvancedSubtensor(unittest.TestCase):
# Reported in https://github.com/Theano/Theano/issues/5674
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 j in range(2):
for k in range(2):
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[1, 1] = 2
r_idx = numpy.arange(xx.shape[1])[:, numpy.newaxis]
c_idx = numpy.arange(xx.shape[2])[numpy.newaxis, :]
r_idx = np.arange(xx.shape[1])[:, np.newaxis]
c_idx = np.arange(xx.shape[2])[np.newaxis, :]
out = X[b_idx, r_idx, c_idx].eval({X: xx})
utt.assert_allclose(out, xx[b_idx, r_idx, c_idx])
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))
idx = tensor.lvector()
idx2 = tensor.lvector()
......@@ -1632,17 +1632,17 @@ class TestAdvancedSubtensor(unittest.TestCase):
self.assertTrue(isinstance(t.owner.op, tensor.AdvancedSubtensor))
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):
return advanced_inc_subtensor(x, y, [1, 3], [2, 4])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2).astype(self.dtype)])
utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
np.random.rand(2).astype(self.dtype)])
def fun(x, y):
return advanced_set_subtensor(x, y, [1, 3], [2, 4])
utt.verify_grad(fun, [numpy.random.rand(5, 5).astype(self.dtype),
numpy.random.rand(2).astype(self.dtype)])
utt.verify_grad(fun, [np.random.rand(5, 5).astype(self.dtype),
np.random.rand(2).astype(self.dtype)])
class TestInferShape(utt.InferShapeTester):
......
from __future__ import absolute_import, print_function, division
import unittest
import numpy
import numpy as np
import theano
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():
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
numpy.zeros((1, 0)), numpy.zeros((2, 0)),
np.zeros((1, 0)), np.zeros((2, 0)),
# Same data buffer and shapes but different strides
numpy.arange(25).reshape(5, 5),
numpy.arange(25).reshape(5, 5).T,
np.arange(25).reshape(5, 5),
np.arange(25).reshape(5, 5).T,
# Same data buffer, shapes and strides but different dtypes
numpy.zeros((5, 5), dtype="uint32"),
numpy.zeros((5, 5), dtype="int32"),
np.zeros((5, 5), dtype="uint32"),
np.zeros((5, 5), dtype="int32"),
# Test slice
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))
assert len(set(hashs)) == len(hashs)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论