提交 7ae82b46 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Updates in tests due to allow_input_downcast=False, when floatX==float32

上级 fb9913c1
import unittest
from theano import config
from theano import gof
from theano import compile
......@@ -23,7 +24,9 @@ class T_OpFromGraph(unittest.TestCase):
op = OpFromGraph([x, y, z], [e], mode='FAST_RUN')
f = op(x, y, z) - op(y, z, x) #(1+3*5=array of 16) - (3+1*5=array of 8)
fn = function([x, y, z], f)
xv, yv, zv = N.ones((2, 2)), N.ones((2, 2))*3, N.ones((2, 2))*5
xv = N.ones((2, 2), dtype=config.floatX)
yv = N.ones((2, 2), dtype=config.floatX)*3
zv = N.ones((2, 2), dtype=config.floatX)*5
print function, function.__module__
print fn.maker.env.toposort()
print fn(xv, yv, zv)
......@@ -36,7 +39,9 @@ class T_OpFromGraph(unittest.TestCase):
op = OpFromGraph([x, y], [e], mode='FAST_RUN')
f = op(x, op(y, z))
fn = function([x, y, z], f)
xv, yv, zv = N.ones((2, 3)), N.ones((3, 4))*3, N.ones((4, 5))*5
xv = N.ones((2, 3), dtype=config.floatX)
yv = N.ones((3, 4), dtype=config.floatX)*3
zv = N.ones((4, 5), dtype=config.floatX)*5
res = fn(xv, yv, zv)
assert res.shape == (2, 5)
assert numpy.all(180.0 == res)
......@@ -51,7 +56,9 @@ class T_OpFromGraph(unittest.TestCase):
f = op(x, y, z)
f = f - T.grad(T.sum(f), y)
fn = function([x, y, z], f)
xv, yv, zv = N.ones((2, 2)), N.ones((2, 2))*3, N.ones((2, 2))*5
xv = N.ones((2, 2), dtype=config.floatX)
yv = N.ones((2, 2), dtype=config.floatX)*3
zv = N.ones((2, 2), dtype=config.floatX)*5
assert numpy.all(11.0 == fn(xv, yv, zv))
......
import sys
import numpy
from theano import config
from theano import gof
import theano
import theano.tensor
......@@ -447,23 +448,27 @@ class Test_check_isfinite(unittest.TestCase):
g = theano.function([x], theano.tensor.log(x), mode='DEBUG_MODE')
# this should work
f(numpy.log([3, 4, 5]))
f(numpy.log([3, 4, 5]).astype(config.floatX))
# if TensorType.filter_checks_isfinite were true, these would raise ValueError
# if not, DebugMode will check internally, and raise InvalidValueError
# passing an invalid value as an input should trigger ValueError
self.failUnlessRaises(debugmode.InvalidValueError, f, numpy.log([3, -4, 5]))
self.failUnlessRaises(debugmode.InvalidValueError, f, numpy.asarray([0, 1.0, 0])/0)
self.failUnlessRaises(debugmode.InvalidValueError, f, numpy.asarray([1.0, 1.0, 1.0])/0)
self.failUnlessRaises(debugmode.InvalidValueError, f,
numpy.log([3, -4, 5]).astype(config.floatX))
self.failUnlessRaises(debugmode.InvalidValueError, f,
(numpy.asarray([0, 1.0, 0])/0).astype(config.floatX))
self.failUnlessRaises(debugmode.InvalidValueError, f,
(numpy.asarray([1.0, 1.0, 1.0])/0).astype(config.floatX))
# generating an invalid value internally should trigger InvalidValueError
self.failUnlessRaises(debugmode.InvalidValueError, g, [3,-4,5])
self.failUnlessRaises(debugmode.InvalidValueError, g,
numpy.asarray([3,-4,5], dtype=config.floatX))
# this should disable the exception
theano.tensor.TensorType.filter_checks_isfinite = False
theano.compile.mode.predefined_modes['DEBUG_MODE'].check_isfinite = False
# insert several Inf
f(numpy.asarray([1.0, 1.0, 1.0])/0)
f(numpy.asarray(numpy.asarray([1.0, 1.0, 1.0])/0, dtype=config.floatX))
def test_check_isfinite_disabled(self):
......
......@@ -467,7 +467,8 @@ class T_picklefunction(unittest.TestCase):
assert f2.container[s].storage is f1.container[s].storage
# now put in a function with non-scalar
f3 = function([x, In(v, value=numpy.asarray([2,3,4.]))], x+v)
v_value = numpy.asarray([2,3,4.], dtype=config.floatX)
f3 = function([x, In(v, value=v_value)], x+v)
list_of_things.append(f3)
# try to pickle the entire things
......
......@@ -331,7 +331,10 @@ def test_uniform():
steps = int(1e3)
x = tensor.matrix()
for size,var_input,input in [(sample_size,[],[]), (x.shape,[x],[numpy.zeros(sample_size)])]:
for size, var_input, input in [
(sample_size, [], []),
(x.shape, [x], [numpy.zeros(sample_size, dtype=config.floatX)])
]:
print ''
print 'ON CPU with size=(%s):'%str(size)
......@@ -394,7 +397,10 @@ def test_binomial():
x = tensor.matrix()
v = tensor.vector()
for mean in [0.1, 0.5]:
for size,var_input,input in [(sample_size,[],[]), (x.shape,[x],[numpy.zeros(sample_size)])]:
for size, var_input, input in [
(sample_size, [], []),
(x.shape, [x], [numpy.zeros(sample_size, dtype=config.floatX)])
]:
print ''
print 'ON CPU with size=(%s) and mean(%d):'%(str(size),mean)
......@@ -442,10 +448,12 @@ def test_normal0():
rtol=.01
sample_size_odd = (sample_size[0],sample_size[1]-1)
x = tensor.matrix()
for size,const_size,var_input,input in [(sample_size,sample_size,[],[]), (x.shape,sample_size,[x],[numpy.zeros(sample_size)]),
(sample_size_odd,sample_size_odd,[],[]),#test odd value
(x.shape,sample_size_odd,[x],[numpy.zeros(sample_size_odd)]),#test odd value
]:
for size, const_size, var_input, input in [
(sample_size, sample_size, [], []),
(x.shape, sample_size, [x], [numpy.zeros(sample_size, dtype=config.floatX)]),
(sample_size_odd, sample_size_odd, [], []),#test odd value
(x.shape, sample_size_odd, [x], [numpy.zeros(sample_size_odd, dtype=config.floatX)]),#test odd value
]:
print ''
print 'ON CPU:'
......
......@@ -4,6 +4,7 @@ import numpy
from nose.plugins.skip import SkipTest
import theano
from theano import config
from theano import tensor as T
from theano import tensor
from theano import gof
......@@ -62,7 +63,7 @@ class T_Softmax(unittest.TestCase):
x = T.vector()
f = theano.function([x], softmax(x))
xv = numpy.random.randn(6)
xv = numpy.random.randn(6).astype(config.floatX)
assert numpy.allclose(f(xv), numpy.exp(xv) / numpy.exp(xv).sum())
def test_vector_grad(self):
def f(a):
......@@ -241,15 +242,16 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
f = theano.function([x, one_of_n], xe)
xe_val = f(numpy.asarray([[.4, .6, .0], [.1, .8, .1]]), [0,1])
x_val = numpy.asarray([[.4, .6, .0], [.1, .8, .1]],
dtype=config.floatX)
xe_val = f(x_val, [0,1])
assert numpy.allclose(xe_val, -numpy.log([.4, .8]))
def oplike(x):
return op(x, [0,1])
tensor.verify_grad(oplike, [numpy.asarray([[.4, .6, .0], [.1, .8, .1]])],
rng=numpy.random)
tensor.verify_grad(oplike, [x_val], rng=numpy.random)
def test_softmax_optimizations(self):
......@@ -958,7 +960,7 @@ class Test_softmax_opt:
print '==='
assert len(f_ops) == 1
assert softmax in f_ops
f(self.rng.rand(3,4))
f(self.rng.rand(3,4).astype(config.floatX))
def test_grad(self):
c = T.matrix()
......
import unittest
import theano
from theano import tensor as T
from theano import config
from theano import gof
import numpy
from theano.tests import unittest_tools as utt
......@@ -97,7 +98,7 @@ class T_softplus_opts(unittest.TestCase):
assert isinstance(topo[0].op.scalar_op, theano.scalar.Neg)
assert isinstance(topo[1].op.scalar_op, theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[2].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54))
f(numpy.random.rand(54).astype(config.floatX))
def test_log1msigm_to_softplus(self):
x = T.vector()
......@@ -108,7 +109,7 @@ class T_softplus_opts(unittest.TestCase):
assert len(topo)==2
assert isinstance(topo[0].op.scalar_op, theano.tensor.nnet.sigm.ScalarSoftplus)
assert isinstance(topo[1].op.scalar_op, theano.scalar.Neg)
f(numpy.random.rand(54))
f(numpy.random.rand(54).astype(config.floatX))
def test_log1pexp_to_softplus(self):
m = theano.config.mode
......@@ -122,4 +123,4 @@ class T_softplus_opts(unittest.TestCase):
topo = f.maker.env.toposort()
assert len(topo)==1
assert isinstance(topo[0].op.scalar_op,theano.tensor.nnet.sigm.ScalarSoftplus)
f(numpy.random.rand(54))
f(numpy.random.rand(54).astype(config.floatX))
......@@ -32,8 +32,12 @@ except ImportError:
### seed random number generator so that unittests are deterministic ###
utt.seed_rng()
def inplace_func(inputs, outputs, mode=get_default_mode()):
return function(inputs, outputs, mode=mode, accept_inplace=True)
def inplace_func(inputs, outputs, mode=get_default_mode(),
allow_input_downcast=False):
return function(inputs, outputs,
mode=mode,
allow_input_downcast=allow_input_downcast,
accept_inplace=True)
def eval_outputs(outputs):
variables = inplace_func([], outputs)()
......@@ -2437,7 +2441,7 @@ class T_op_cache(unittest.TestCase):
fn_py = inplace_func([v], gv)
fn_c_or_py = inplace_func([v], gv)
a = numpy.random.rand(5,2)
a = numpy.random.rand(5,2).astype(config.floatX)
self.failUnless(numpy.all(fn_py(a) == fn_c_or_py(a)))
......@@ -3168,7 +3172,7 @@ def test_default_state():
assert f(3) == 15
f['x'] = None
assert numpy.allclose(f(1), 4.8)
assert numpy.allclose(f(2.2), 7)
assert numpy.allclose(f(numpy.asarray(2.2, dtype=config.floatX)), 7)
def test_autocast():
orig_autocast = autocast_float.dtypes
......
......@@ -314,14 +314,18 @@ class Warning(Exception):
def just_gemm(i, o, ishapes = [(4,3), (3,5), (4,5), (), ()], max_graphlen=0):
try:
f = inplace_func([Param(ii, mutable=True) for ii in i],o, mode='FAST_RUN')
f = inplace_func(
[Param(ii, mutable=True, allow_downcast=True) for ii in i],
o,
mode='FAST_RUN')
at_least_one_gemm = False
for node in f.maker.env.nodes:
if node.op == T.dot: raise Warning('dot not changed to gemm_inplace in graph')
if node.op == _dot22: raise Warning('_dot22 not changed to gemm_inplace in graph')
if node.op == gemm_inplace: at_least_one_gemm = True
assert at_least_one_gemm
g = inplace_func(i, o, mode=compile.Mode(linker='py', optimizer=None))
g = inplace_func(i, o, mode=compile.Mode(linker='py', optimizer=None),
allow_input_downcast=True)
for node in g.maker.env.nodes:
if node.op == gemm_inplace: raise Exception('gemm_inplace in original graph')
......@@ -568,8 +572,8 @@ def test_dot22():
f = theano.function([a,b],T.dot(a,b),mode=mode_blas_opt)
topo = f.maker.env.toposort()
assert _dot22 in [x.op for x in topo]
av=numpy.random.rand(5,5)
bv=numpy.random.rand(5,5)
av=numpy.random.rand(5,5).astype(config.floatX)
bv=numpy.random.rand(5,5).astype(config.floatX)
f(av,bv)
def test_dot22scalar():
......@@ -581,9 +585,9 @@ def test_dot22scalar():
a=T.matrix()
b=T.matrix()
c=T.matrix()
av=numpy.random.rand(5,5)
bv=numpy.random.rand(5,5)
cv=numpy.random.rand(5,5)
av=numpy.random.rand(5,5).astype(config.floatX)
bv=numpy.random.rand(5,5).astype(config.floatX)
cv=numpy.random.rand(5,5).astype(config.floatX)
if True:
f = theano.function([a,b],0.2*T.dot(a,b),mode=mode_blas_opt)
......@@ -651,4 +655,4 @@ def test_dot_w_self():
f = theano.function([B], p, updates = { A : A - grad[0]} )
# tests correctness in debugmode
f(numpy.asarray([[0,1], [2,3]]))
f(numpy.asarray([[0,1], [2,3]], dtype=config.floatX))
......@@ -5,6 +5,7 @@ from theano import tensor as T
from theano.tensor import nnet as NN
from theano.compile import module
from theano.compile.mode import get_default_mode
from theano import config
from theano import tensor as T, sparse as S
import numpy as N
import sys
......@@ -199,17 +200,20 @@ class QuadraticDenoisingAA(module.Module):
sz = (input_size, hidden_size)
inf = 1/N.sqrt(input_size)
hif = 1/N.sqrt(hidden_size)
obj.w1 = R.uniform(size = sz, low = -inf, high = inf)
obj.w1 = N.asarray(R.uniform(size = sz, low = -inf, high = inf),
dtype=config.floatX)
if not self.tie_weights:
obj.w2 = R.uniform(size = list(reversed(sz)), low = -hif, high = hif)
obj.b1 = N.zeros(hidden_size)
obj.b2 = N.zeros(input_size)
obj.w2 = N.asarray(
R.uniform(size=list(reversed(sz)), low=-hif, high=hif),
dtype=config.floatX)
obj.b1 = N.zeros(hidden_size, dtype=config.floatX)
obj.b2 = N.zeros(input_size, dtype=config.floatX)
obj.qfilters = [R.uniform(size = sz, low = -inf, high = inf) * qfilter_relscale \
for qf in self.qfilters]
if seed is not None:
obj.random.seed(seed)
obj.lr = lr
obj.lr = N.asarray(lr, dtype=config.floatX)
obj.__hide__ = ['params']
......@@ -251,7 +255,7 @@ class SigmoidXEQuadraticDenoisingAA(QuadraticDenoisingAA):
def _instance_initialize(self, obj, input_size, hidden_size, noise_level, seed, lr, qfilter_relscale):
# obj.l2_coef = 0.0
obj.noise_level = noise_level
obj.noise_level = N.asarray(noise_level, dtype=config.floatX)
super(SigmoidXEQuadraticDenoisingAA, self)._instance_initialize(obj, input_size, hidden_size, seed, lr, qfilter_relscale)
QDAA = SigmoidXEQuadraticDenoisingAA
......@@ -438,7 +442,7 @@ class ConvolutionalMLP(module.FancyModule):
self.hidden_representation_size = hidden_representation_size
self.output_size = output_size
self.lr = lr
self.lr = N.asarray(lr, dtype=config.floatX)
# for layer in obj.layers:
# if layer.lr is None:
# layer.lr = lr
......
......@@ -9,6 +9,7 @@ from nose.plugins.skip import SkipTest
from numpy.testing.noseclasses import KnownFailureTest
import theano
from theano import config
from theano import gof
from theano.tensor.opt import *
from theano import tensor #do not use, there is an import * below that hides it
......@@ -109,9 +110,14 @@ class test_greedy_distribute(unittest.TestCase):
f = function([s, eps, x,y], r**2)
r0 = f(4,1.e-6, [1.5,2], [2.3,3.1])
r1 = f(4,1.e-6, [1.5,2], [2.3,3.1])
r2 = f(4,1.e-6, [1.5,2], [2.3,3.1])
s_val = numpy.asarray(4, dtype=config.floatX)
eps_val = numpy.asarray(1.e-6, dtype=config.floatX)
x_val = numpy.asarray([1.5,2], dtype=config.floatX)
y_val = numpy.asarray([2.3,3.1], dtype=config.floatX)
r0 = f(s_val, eps_val, x_val, y_val)
r1 = f(s_val, eps_val, x_val, y_val)
r2 = f(s_val, eps_val, x_val, y_val)
assert numpy.all(r0 == r1)
assert numpy.all(r0 == r2)
......@@ -614,9 +620,9 @@ class test_canonize(unittest.TestCase):
def test_local_merge_abs():
x,y,z = T.matrices('xyz')
x_val = numpy.random.rand(5,5)
y_val = numpy.random.rand(5,5)
z_val = numpy.random.rand(5,5)
x_val = numpy.random.rand(5,5).astype(config.floatX)
y_val = numpy.random.rand(5,5).astype(config.floatX)
z_val = numpy.random.rand(5,5).astype(config.floatX)
mode = theano.config.mode
if mode == "FAST_COMPILE":
mode = "FAST_RUN"
......@@ -1211,7 +1217,8 @@ class test_shapeoptimizer(unittest.TestCase):
# Without the optimization
f = theano.function([x], ins_x.shape, mode=mode)
assert numpy.all(f(rng.randn(3,4,7)) == [3,4,7])
xval = rng.randn(3,4,7).astype(config.floatX)
assert numpy.all(f(xval) == [3,4,7])
f_ops = [node.op for node in f.maker.env.toposort()]
assert len(f_ops) == 5
assert identity_noshape in f_ops
......@@ -1224,7 +1231,8 @@ class test_shapeoptimizer(unittest.TestCase):
# The identity_shape op is should not be needed anymore to compute
# the shape
g = theano.function([x], ins_x.shape, mode=mode)
assert numpy.all(g(rng.randn(6,1,2)) == [6,1,2])
xval = rng.randn(6,1,2).astype(config.floatX)
assert numpy.all(g(xval) == [6,1,2])
g_ops = [node.op for node in g.maker.env.toposort()]
assert len(g_ops) == 4
assert identity_noshape not in g_ops
......@@ -1234,7 +1242,8 @@ class test_shapeoptimizer(unittest.TestCase):
###test multiple level of op without infer_shape
ins_x3 = identity_noshape(identity_noshape(identity_noshape(x)))
h = theano.function([x], ins_x3.shape, mode=mode)
assert numpy.all(h(rng.randn(6,1,2)) == [6,1,2])
xval = rng.randn(6,1,2).astype(config.floatX)
assert numpy.all(h(xval) == [6,1,2])
h_ops = [node.op for node in h.maker.env.toposort()]
assert len(h_ops) == 4
assert identity_noshape not in h_ops
......@@ -1660,7 +1669,7 @@ class T_local_erf(unittest.TestCase):
self.mode._optimizer.position_cutoff = 1.50001
def test_local_one_plus_erf(self):
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30])
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30], dtype=config.floatX)
x = T.vector()
f = theano.function([x],1+T.erf(x), mode=self.mode)
......@@ -1683,7 +1692,7 @@ class T_local_erf(unittest.TestCase):
f(val)
def test_local_one_minus_erf(self):
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30])
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30], dtype=config.floatX)
x = T.vector()
f = theano.function([x],1-T.erf(x), mode=self.mode)
......@@ -1711,7 +1720,7 @@ class T_local_erf(unittest.TestCase):
print f(val)
def test_local_erf_minus_one(self):
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30])
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30], dtype=config.floatX)
x = T.vector()
f = theano.function([x],T.erf(x)-1, mode=self.mode)
......@@ -1747,7 +1756,7 @@ class T_local_erfc(unittest.TestCase):
def test_local_one_minus_erfc(self):
""" test opt: 1-erfc(x) => erf(x) and -erfc(x)+1 => erf(x)
"""
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30])
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30], dtype=config.floatX)
x = T.vector('x')
f = theano.function([x],1-T.erfc(x), mode=self.mode)
......@@ -1771,7 +1780,7 @@ class T_local_erfc(unittest.TestCase):
def test_local_erf_neg_minus_one(self):
""" test opt: (-1)+erfc(-x)=>erf(x)"""
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30])
val = numpy.asarray([-30,-3,-2,-1,0,1,2,3,30], dtype=config.floatX)
x = T.vector('x')
f = theano.function([x],-1+T.erfc(-x), mode=self.mode)
......@@ -1794,7 +1803,7 @@ class T_local_erfc(unittest.TestCase):
if theano.config.mode in ["DebugMode", "DEBUG_MODE", "FAST_COMPILE"]:
#python mode don't like the inv(0)
val.remove(0)
val = numpy.asarray(val)
val = numpy.asarray(val, dtype=config.floatX)
x = T.vector('x')
#their is some nan that will happear in the graph for the log of the negatives values
......@@ -1833,7 +1842,7 @@ class T_local_erfc(unittest.TestCase):
# In float32 their is a plage of values close to 10 that we stabilize as it give bigger error then the stabilized version.
# The orig value in float32 -30.0, the stab value -20.1 the orig value in float64 -18.1.
val.remove(10)
val = numpy.asarray(val)
val = numpy.asarray(val, dtype=config.floatX)
x = T.vector('x')
y = T.vector('y')
......@@ -1919,7 +1928,7 @@ class T_local_sum(unittest.TestCase):
def test_local_sum_all_to_none(self):
a = T.tensor3()
input=numpy.arange(3*3*3).reshape(3,3,3)
input=numpy.arange(3*3*3, dtype=config.floatX).reshape(3,3,3)
f = theano.function([a],a.sum(),mode=self.mode)
assert len(f.maker.env.nodes)==1
assert numpy.allclose(f(input),input.sum())
......@@ -1935,7 +1944,7 @@ class T_local_sum(unittest.TestCase):
def test_local_sum_sum(self):
a=T.tensor3()
input=numpy.arange(3*3*3).reshape(3,3,3)
input=numpy.arange(3*3*3, dtype=config.floatX).reshape(3,3,3)
dims=[(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)]
for d,dd in dims:
......@@ -2036,10 +2045,10 @@ class T_local_sum_dimshuffle(unittest.TestCase):
]
rng = numpy.random.RandomState(utt.fetch_seed())
a_val = rng.randn(2,2)
b_val = rng.randn(2)
c_val = rng.randn(2,2,2)
d_val = rng.randn()
a_val = rng.randn(2,2).astype(config.floatX)
b_val = rng.randn(2).astype(config.floatX)
c_val = rng.randn(2,2,2).astype(config.floatX)
d_val = numpy.asarray(rng.randn(), config.floatX)
for i,s in enumerate(sums):
print i
......
......@@ -9,7 +9,7 @@ from theano.compile import Module, Method, Member
from theano.tests import unittest_tools as utt
from theano import tensor
from theano import compile, gof
from theano import compile, config, gof
class T_RandomStreams(unittest.TestCase):
......@@ -402,10 +402,12 @@ class T_RandomStreams(unittest.TestCase):
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
val0 = made.f([-5, .5, 0, 1])
val1 = made.f([.9])
numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=1)
numpy_val1 = numpy_rng.uniform(low=[.9], high=1)
low_val0 = numpy.asarray([-5, .5, 0, 1], dtype=config.floatX)
low_val1 = numpy.asarray([.9], dtype=config.floatX)
val0 = made.f(low_val0)
val1 = made.f(low_val1)
numpy_val0 = numpy_rng.uniform(low=low_val0, high=1)
numpy_val1 = numpy_rng.uniform(low=low_val1, high=1)
assert numpy.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1)
......@@ -447,13 +449,22 @@ class T_RandomStreams(unittest.TestCase):
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
numpy_rng = numpy.random.RandomState(int(rng_seed))
val0 = made.f([-5, .5, 0, 1], [[1.]])
val1 = made.f([.9], [[1.], [1.1], [1.5]])
val2 = made.f([-5, .5, 0, 1], [[1.], [1.1], [1.5]])
numpy_val0 = numpy_rng.uniform(low=[-5, .5, 0, 1], high=[1.])
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]])
low_vals = [
numpy.asarray([-5, .5, 0, 1], dtype=config.floatX),
numpy.asarray([.9], dtype=config.floatX),
numpy.asarray([-5, .5, 0, 1], dtype=config.floatX) ]
high_vals = [
numpy.asarray([[1.]], dtype=config.floatX),
numpy.asarray([[1.], [1.1], [1.5]], dtype=config.floatX),
numpy.asarray([[1.], [1.1], [1.5]], dtype=config.floatX) ]
val0 = made.f(low_vals[0], high_vals[0])
val1 = made.f(low_vals[1], high_vals[1])
val2 = made.f(low_vals[2], high_vals[2])
numpy_val0 = numpy_rng.uniform(low=low_vals[0], high=high_vals[0])
numpy_val1 = numpy_rng.uniform(low=low_vals[1], high=high_vals[1])
numpy_val2 = numpy_rng.uniform(low=low_vals[2], high=high_vals[2])
assert numpy.allclose(val0, numpy_val0)
assert numpy.allclose(val1, numpy_val1)
......@@ -473,8 +484,8 @@ class T_RandomStreams(unittest.TestCase):
made = m.make()
made.random.initialize()
low_val = [.1, .2, .3]
high_val = [1.1, 2.2, 3.3]
low_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
high_val = numpy.asarray([1.1, 2.2, 3.3], dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
......@@ -510,7 +521,7 @@ class T_RandomStreams(unittest.TestCase):
made.random.initialize()
n_val = [1, 2, 3]
prob_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)))
......@@ -546,7 +557,7 @@ class T_RandomStreams(unittest.TestCase):
made.random.initialize()
avg_val = [1, 2, 3]
std_val = [.1, .2, .3]
std_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)))
......@@ -624,7 +635,8 @@ class T_RandomStreams(unittest.TestCase):
made.random.initialize()
n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray([[.1, .9], [.2, .8], [.3, .7]],
dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
......
......@@ -9,7 +9,7 @@ from theano.tensor import raw_random
from theano import tensor
from theano import compile, gof
from theano import compile, config, gof
class T_random_function(unittest.TestCase):
def setUp(self):
......@@ -494,7 +494,7 @@ class T_random_function(unittest.TestCase):
rng_state0 = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(utt.fetch_seed())
post0, val0 = f(rng_state0, [-5, .5, 0, 1])
post1, val1 = f(post0, [.9])
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))
......@@ -597,7 +597,7 @@ class T_random_function(unittest.TestCase):
f = compile.function([rng_R, n, prob], [post_r, out], accept_inplace=True)
n_val = [1, 2, 3]
prob_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())
......@@ -628,14 +628,15 @@ class T_random_function(unittest.TestCase):
assert out.ndim == 1
f = compile.function([rng_R, avg, std], [post_r, out], accept_inplace=True)
def as_floatX(thing):
return numpy.asarray(thing, dtype=theano.config.floatX)
avg_val = [1, 2, 3]
std_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())
# Arguments of size (3,)
def as_floatX(thing):
return numpy.asarray(thing, dtype=theano.config.floatX)
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)))
......@@ -705,6 +706,7 @@ class T_random_function(unittest.TestCase):
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())
......
......@@ -9,7 +9,7 @@ from theano.tensor.shared_randomstreams import RandomStreams
from theano import function
from theano import tensor
from theano import compile, gof
from theano import compile, config, gof
from theano.tests import unittest_tools as utt
......@@ -466,7 +466,7 @@ class T_SharedRandomStreams(unittest.TestCase):
f = function([n, prob], out)
n_val = [1, 2, 3]
prob_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)))
......@@ -566,6 +566,7 @@ 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)))
......
......@@ -2,7 +2,10 @@
import unittest
import theano
import numpy
from theano import config
from theano.tests import unittest_tools as utt
'''
Questions and notes about scan that should be answered :
......@@ -161,7 +164,7 @@ class T_Scan(unittest.TestCase):
my_f = theano.function([state,n_steps], output, updates = updates)
rng = numpy.random.RandomState(utt.fetch_seed())
state = rng.uniform()
state = asarrayX(rng.uniform())
steps = 5
numpy_values = numpy.array([ state*(2**(k+1)) for k in xrange(steps) ])
......@@ -186,10 +189,10 @@ class T_Scan(unittest.TestCase):
f2 = theano.function([u,x0,W_in,W], output, updates = updates)
# get random initial values
rng = numpy.random.RandomState(utt.fetch_seed())
v_u = rng.uniform( size = (4,), low = -5., high = 5.)
v_x0 = rng.uniform()
W = rng.uniform()
W_in = rng.uniform()
v_u = asarrayX(rng.uniform(size = (4,), low = -5., high = 5.))
v_x0 = asarrayX(rng.uniform())
W = asarrayX(rng.uniform())
W_in = asarrayX(rng.uniform())
# compute the output in numpy
v_out = numpy.zeros((4,))
......@@ -218,8 +221,8 @@ class T_Scan(unittest.TestCase):
f3 = theano.function([u,x0], output, updates = updates)
# get random initial values
v_u = rng.uniform( size = (4,), low = -5., high = 5.)
v_x0 = rng.uniform()
v_u = asarrayX(rng.uniform(size = (4,), low = -5., high = 5.))
v_x0 = asarrayX(rng.uniform())
# compute the output i numpy
v_out = numpy.zeros((4,))
v_out[0] = v_u[0]*W_in.value + v_x0*W.value
......@@ -491,7 +494,8 @@ class T_Scan(unittest.TestCase):
#TypeError: ('__array__() takes no arguments (1 given)', <theano.scan.Scan object at 0x3dbbf90>(?_steps, u1, u2, y0, y1, 0.0, W1, W2), 'Sequence id of Apply node=0')
#
# This don't seam to be a theano related bug...
vu1 = rng.rand(3,20)
#vu1 = rng.rand(3,20)
vu1 = asarrayX(rng.rand(3,20))
W1 = theano.shared(vW1,'W1')
W2 = theano.shared(vW2,'W2')
......@@ -632,7 +636,7 @@ class T_Scan(unittest.TestCase):
f2 = theano.function([u], outputs, updates = updates)
rng = numpy.random.RandomState(utt.fetch_seed())
v_u = rng.uniform(size=(5,), low = -5., high = 5.)
v_u = rng.uniform(size=(5,), low = -5., high = 5.).astype(config.floatX)
numpy_result = v_u + 3
theano_result = f2(v_u)
assert numpy.allclose(theano_result , numpy_result)
......@@ -645,7 +649,7 @@ class T_Scan(unittest.TestCase):
f = theano.function([v],abs_expr,updates = abs_updates)
rng = numpy.random.RandomState(utt.fetch_seed())
vals = rng.uniform(size=(10,), low = -5., high = 5.)
vals = rng.uniform(size=(10,), low = -5., high = 5.).astype(config.floatX)
abs_vals = abs(vals)
theano_vals = f(vals)
assert numpy.allclose(abs_vals , theano_vals)
......@@ -665,10 +669,10 @@ class T_Scan(unittest.TestCase):
f2 = theano.function([u,x0,W_in,W], output, updates = updates)
# get random initial values
rng = numpy.random.RandomState(utt.fetch_seed())
v_u = rng.uniform( size = (4,), low = -5., high = 5.)
v_x0 = rng.uniform()
W = rng.uniform()
W_in = rng.uniform()
v_u = asarrayX(rng.uniform(size=(4,), low=-5., high=5.))
v_x0 = asarrayX(rng.uniform())
W = asarrayX(rng.uniform())
W_in = asarrayX(rng.uniform())
# compute the output in numpy
v_out = numpy.zeros((4,))
......@@ -686,7 +690,7 @@ class T_Scan(unittest.TestCase):
f = theano.function([v,s], result, updates = updates)
rng = numpy.random.RandomState(utt.fetch_seed())
v_v = rng.uniform( size = (5,), low = -5., high = 5.)
v_v = rng.uniform(size = (5,), low = -5., high = 5.).astype(config.floatX)
assert abs(numpy.sum(v_v) - f(v_v, 0.)) < 1e-3
......@@ -705,7 +709,7 @@ class T_Scan(unittest.TestCase):
grad_fn = theano.function([u,x0,W_in, W], [gu,gx0,gW_in, gW],
updates = updates, no_default_updates = True)
cost_fn = theano.function([u,x0,W_in, W], cost, updates = updates,
no_default_updates = True)
no_default_updates = True, allow_input_downcast=True)
# get random initial values
rng = numpy.random.RandomState(utt.fetch_seed())
......@@ -754,7 +758,8 @@ class T_Scan(unittest.TestCase):
grad_fn = theano.function([u1,u2,x0,y0,W_in1], gparams,
updates = updates, no_default_updates = True)
cost_fn = theano.function([u1,u2,x0,y0,W_in1], cost,
updates = updates, no_default_updates = True)
updates = updates, no_default_updates = True,
allow_input_downcast=True)
num_grad = multiple_outputs_numeric_grad(cost_fn,[v_u1,v_u2,v_x0,v_y0,vW_in1])
analytic_grad = grad_fn(v_u1,v_u2, v_x0,v_y0, vW_in1)
......@@ -954,7 +959,7 @@ class T_Scan(unittest.TestCase):
f = theano.function([x],[y,z], updates = updates)
rng = numpy.random.RandomState(utt.fetch_seed())
nx = rng.uniform( size = (10,10) )
nx = rng.uniform( size = (10,10) ).astype(config.floatX)
ny1,nz1 = f(nx)
ny2,nz2 = f(nx)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论