提交 7e6a3dc5 authored 作者: Frédéric Bastien's avatar Frédéric Bastien

Merge pull request #4126 from olimastro/master

flake8 compile/tests/*.py
......@@ -4,7 +4,6 @@ from theano import config, shared
from theano.compile import function
from theano import tensor
from theano import tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
......@@ -24,8 +23,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
fn = function([x, y, z], f)
xv = numpy.ones((2, 2), dtype=config.floatX)
yv = numpy.ones((2, 2), dtype=config.floatX)*3
zv = numpy.ones((2, 2), dtype=config.floatX)*5
yv = numpy.ones((2, 2), dtype=config.floatX) * 3
zv = numpy.ones((2, 2), dtype=config.floatX) * 5
# print function, function.__module__
# print fn.maker.fgraph.toposort()
fn(xv, yv, zv)
......@@ -39,8 +38,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f = op(x, op(y, z))
fn = function([x, y, z], f)
xv = numpy.ones((2, 3), dtype=config.floatX)
yv = numpy.ones((3, 4), dtype=config.floatX)*3
zv = numpy.ones((4, 5), dtype=config.floatX)*5
yv = numpy.ones((3, 4), dtype=config.floatX) * 3
zv = numpy.ones((4, 5), dtype=config.floatX) * 5
res = fn(xv, yv, zv)
assert res.shape == (2, 5)
assert numpy.all(180.0 == res)
......@@ -56,8 +55,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f = f - T.grad(T.sum(f), y)
fn = function([x, y, z], f)
xv = numpy.ones((2, 2), dtype=config.floatX)
yv = numpy.ones((2, 2), dtype=config.floatX)*3
zv = numpy.ones((2, 2), dtype=config.floatX)*5
yv = numpy.ones((2, 2), dtype=config.floatX) * 3
zv = numpy.ones((2, 2), dtype=config.floatX) * 5
assert numpy.all(11.0 == fn(xv, yv, zv))
def test_grad_grad(self):
......@@ -69,8 +68,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
f = f - T.grad(T.sum(f), y)
fn = function([x, y, z], f)
xv = numpy.ones((2, 2), dtype=config.floatX)
yv = numpy.ones((2, 2), dtype=config.floatX)*3
zv = numpy.ones((2, 2), dtype=config.floatX)*5
yv = numpy.ones((2, 2), dtype=config.floatX) * 3
zv = numpy.ones((2, 2), dtype=config.floatX) * 5
assert numpy.allclose(6.0, fn(xv, yv, zv))
def test_shared(self):
......@@ -83,8 +82,8 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
fn = function([x, y, z], f)
xv = numpy.ones((2, 2), dtype=config.floatX)
yv = numpy.ones((2, 2), dtype=config.floatX)*3
zv = numpy.ones((2, 2), dtype=config.floatX)*5
yv = numpy.ones((2, 2), dtype=config.floatX) * 3
zv = numpy.ones((2, 2), dtype=config.floatX) * 5
# print function, function.__module__
# print fn.maker.fgraph.toposort()
assert numpy.allclose(8.0, fn(xv, yv, zv))
......@@ -109,14 +108,14 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
fn = function([x, y, z], f)
assert numpy.allclose(15.0 + s.get_value(),
fn(xv, yv, zv))
def test_connection_pattern(self):
# Basic case
# Basic case
x, y, z = T.matrices('xyz')
out1 = x * y
out2 = y * z
op1 = OpFromGraph([x ,y, z], [out1, out2])
op1 = OpFromGraph([x, y, z], [out1, out2])
results = op1.connection_pattern(None)
expect_result = [[True, False],
[True, True],
......@@ -124,7 +123,7 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
assert results == expect_result
# Graph with ops that don't have a 'full' connection pattern
# and with ops that have multiple outputs
# and with ops that have multiple outputs
m, n, p, q = T.matrices('mnpq')
o1, o2 = op1(m, n, p)
out1, out2 = op1(o1, q, o2)
......@@ -139,7 +138,7 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
# Inner graph where some computation doesn't rely on explicit inputs
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2,2))
rv_u = srng.uniform((2, 2))
x, y = T.matrices('xy')
out1 = x + rv_u
out2 = y + 3
......@@ -155,14 +154,14 @@ class T_OpFromGraph(unittest_tools.InferShapeTester):
def test_infer_shape(self):
x = T.matrix('x')
y = T.matrix('y')
o1 = x+y
o2 = x*y
op_graph = OpFromGraph([x,y], [o1,o2])
o1 = x + y
o2 = x * y
op_graph = OpFromGraph([x, y], [o1, o2])
q = T.matrix('q')
p = T.matrix('p')
self._compile_and_check([q,p],
op_graph(q,p),
[numpy.ones([3,4], dtype=config.floatX),
numpy.ones([3,4], dtype=config.floatX)],
self._compile_and_check([q, p],
op_graph(q, p),
[numpy.ones([3, 4], dtype=config.floatX),
numpy.ones([3, 4], dtype=config.floatX)],
OpFromGraph)
......@@ -22,7 +22,7 @@ def test0():
class BROKEN_ON_PURPOSE_Add(gof.Op):
__props__ = ("py_offset",)
def __init__(self, py_offset):
gof.Op.__init__(self)
self.py_offset = py_offset
......@@ -102,7 +102,7 @@ class WeirdBrokenOp(gof.Op):
it should raise an error in DebugMode.
"""
__props__ = ("behaviour", )
def __init__(self, behaviour):
gof.Op.__init__(self)
self.behaviour = behaviour
......@@ -160,16 +160,16 @@ class WeirdBrokenOp(gof.Op):
if self.behaviour == 'times2':
behaviour = " Dz[m * Sz] = 2 * Da[m * Sa]; "
#out[0] = a * 2
# out[0] = a * 2
elif self.behaviour == 'times2_inplace':
#out[0] = a
#out[0] *= 2
# out[0] = a
# out[0] *= 2
behaviour = " Dz[m * Sz] = 2 * Da[m * Sa]; "
elif self.behaviour == 'times1':
#out[0] = a * 1
# out[0] = a * 1
behaviour = " Dz[m * Sz] = Da[m * Sa]; "
elif self.behaviour == 'times1_inplace':
#out[0] = a
# out[0] = a
behaviour = ""
else:
raise ValueError(self.behaviour)
......@@ -179,7 +179,7 @@ class WeirdBrokenOp(gof.Op):
"""
total = ((z_code + prep_vars + behaviour + prep_vars2)
% dict(locals(), **sub))
% dict(locals(), **sub))
return total
wb2i = WeirdBrokenOp('times2_inplace')
......@@ -189,16 +189,16 @@ wb1 = WeirdBrokenOp('times1')
def test_badthunkoutput():
# Check if the c and python code is consistent.
# Check if the c and python code is consistent.
a = theano.tensor.dvector()
b = theano.tensor.dvector()
f_good = theano.function([a, b],
off_by_half(a, b),
mode=debugmode.DebugMode(check_c_code=theano.config.cxx))
off_by_half(a, b),
mode=debugmode.DebugMode(check_c_code=theano.config.cxx))
f_inconsistent = theano.function([a, b],
inconsistent(a, b),
mode=debugmode.DebugMode(check_c_code=theano.config.cxx))
inconsistent(a, b),
mode=debugmode.DebugMode(check_c_code=theano.config.cxx))
# this should evaluate with no error
f_good([1.0, 2.0, 3.0], [2, 3, 4])
......@@ -229,7 +229,7 @@ def test_badoptimization():
b = theano.tensor.dvector()
f = theano.function([a, b], a + b,
mode=debugmode.DebugMode(optimizer=opt))
mode=debugmode.DebugMode(optimizer=opt))
try:
f([1.0, 2.0, 3.0], [2, 3, 4],)
......@@ -289,9 +289,9 @@ def test_stochasticoptimization():
edb = gof.EquilibriumDB()
edb.register(
'insert_broken_add_sometimes',
insert_broken_add_sometimes,
'all')
'insert_broken_add_sometimes',
insert_broken_add_sometimes,
'all')
opt = edb.query('+all')
a = theano.tensor.dvector()
......@@ -299,11 +299,11 @@ def test_stochasticoptimization():
try:
theano.function([a, b],
theano.tensor.add(a, b),
mode=debugmode.DebugMode(
optimizer=opt,
check_c_code=True,
stability_patience=max(2, config.DebugMode.patience)))
theano.tensor.add(a, b),
mode=debugmode.DebugMode(
optimizer=opt,
check_c_code=True,
stability_patience=max(2, config.DebugMode.patience)))
except debugmode.StochasticOrder:
return # TEST PASS
assert False
......@@ -314,7 +314,7 @@ def test_just_c_code():
raise SkipTest("G++ not available, so we need to skip this test.")
x = theano.tensor.dvector()
f = theano.function([x], wb2(x),
mode=debugmode.DebugMode(check_py_code=False))
mode=debugmode.DebugMode(check_py_code=False))
assert numpy.all(f([1, 2]) == [2, 4])
......@@ -346,7 +346,7 @@ def test_baddestroymap_c():
raise SkipTest("G++ not available, so we need to skip this test.")
x = theano.tensor.dvector()
f = theano.function([x], wb2i(x),
mode=debugmode.DebugMode(check_py_code=False))
mode=debugmode.DebugMode(check_py_code=False))
try:
assert numpy.all(f([1, 2]) == [2, 4])
assert False # failed to raise error
......@@ -390,7 +390,7 @@ class Test_ViewMap(unittest.TestCase):
x = theano.tensor.dvector()
y = theano.tensor.dvector()
f = theano.function([x, y], self.BadAddSlice()(x, y),
mode='DEBUG_MODE')
mode='DEBUG_MODE')
try:
f([1, 2], [3, 4])
assert False # failed to raise error
......@@ -414,7 +414,7 @@ class Test_ViewMap(unittest.TestCase):
raise SkipTest("G++ not available, so we need to skip this test.")
x = theano.tensor.dvector()
f = theano.function([x], wb1i(x),
mode=debugmode.DebugMode(check_py_code=False))
mode=debugmode.DebugMode(check_py_code=False))
try:
f([1, 2])
assert False # failed to raise error
......@@ -525,7 +525,7 @@ class Test_ViewMap(unittest.TestCase):
try:
f([1, 2, 3, 4], [5, 6, 7, 8])
assert False # DebugMode should have caught the error
except debugmode.BadViewMap as e:
except debugmode.BadViewMap:
# print e
pass
......@@ -533,7 +533,7 @@ class Test_ViewMap(unittest.TestCase):
# pretending that it is aliased to both the outputs.
# This unfairly disables any destructive operations on the
# input, but guarantees correctness.
#custom_op.view_map = {0:[0], 1:[1]}
# custom_op.view_map = {0:[0], 1:[1]}
# f([1,2,3,4],[5,6,7,8])
......@@ -541,12 +541,12 @@ class Test_check_isfinite(unittest.TestCase):
def setUp(self):
self.old_ts = theano.tensor.TensorType.filter_checks_isfinite
self.old_dm = theano.compile.mode.predefined_modes[
'DEBUG_MODE'].check_isfinite
'DEBUG_MODE'].check_isfinite
def tearDown(self):
theano.tensor.TensorType.filter_checks_isfinite = self.old_ts
theano.compile.mode.predefined_modes[
'DEBUG_MODE'].check_isfinite = self.old_dm
'DEBUG_MODE'].check_isfinite = self.old_dm
def test_check_isfinite(self):
x = theano.tensor.vector()
......@@ -561,29 +561,29 @@ class Test_check_isfinite(unittest.TestCase):
# if not, DebugMode will check internally, and raise InvalidValueError
# passing an invalid value as an input should trigger ValueError
self.assertRaises(debugmode.InvalidValueError, f,
numpy.log([3, -4, 5]).astype(config.floatX))
numpy.log([3, -4, 5]).astype(config.floatX))
self.assertRaises(debugmode.InvalidValueError, f,
(numpy.asarray([0, 1.0, 0]) / 0).astype(config.floatX))
(numpy.asarray([0, 1.0, 0]) / 0).astype(config.floatX))
self.assertRaises(debugmode.InvalidValueError, f,
(numpy.asarray([1.0, 1.0, 1.0]) / 0).astype(config.floatX))
(numpy.asarray([1.0, 1.0, 1.0]) / 0).astype(config.floatX))
# generating an invalid value internally should trigger
# InvalidValueError
self.assertRaises(debugmode.InvalidValueError, g,
numpy.asarray([3, -4, 5], dtype=config.floatX))
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
'DEBUG_MODE'].check_isfinite = False
# insert several Inf
f(numpy.asarray(numpy.asarray([1.0, 1.0, 1.0]) / 0,
dtype=config.floatX))
dtype=config.floatX))
def test_check_isfinite_disabled(self):
x = theano.tensor.dvector()
f = theano.function([x], (x + 2) * 5,
mode=debugmode.DebugMode(check_isfinite=False))
mode=debugmode.DebugMode(check_isfinite=False))
# nan should go through
f(numpy.log([3, -4, 5]))
......@@ -734,17 +734,17 @@ class Test_preallocated_output(unittest.TestCase):
# Should work
mode = debugmode.DebugMode(
check_preallocated_output=['c_contiguous'])
check_preallocated_output=['c_contiguous'])
f = theano.function([a, b], out, mode=mode)
out_val = f(a_val, b_val)
f(a_val, b_val)
# print 'out_val =', out_val
# print out_val.strides
# Should raise an Exception, since the output buffer is
# used incorrectly.
mode = debugmode.DebugMode(
check_preallocated_output=['f_contiguous'])
check_preallocated_output=['f_contiguous'])
f = theano.function([a, b], out, mode=mode)
......@@ -766,17 +766,17 @@ class Test_preallocated_output(unittest.TestCase):
# Should work
mode = debugmode.DebugMode(
check_preallocated_output=['c_contiguous'])
check_preallocated_output=['c_contiguous'])
f = theano.function([a, b], out, mode=mode)
out_val = f(a_val, b_val)
f(a_val, b_val)
# print 'out_val =', out_val
# print out_val.strides
# Should raise an Exception, since the output buffer is
# used incorrectly.
mode = debugmode.DebugMode(
check_preallocated_output=['f_contiguous'])
check_preallocated_output=['f_contiguous'])
f = theano.function([a, b], out, mode=mode)
......
import numpy, theano, unittest
import numpy
import unittest
from theano.compile.pfunc import pfunc
from theano.compile.sharedvalue import shared
......@@ -9,9 +10,9 @@ from theano.tensor.nnet import sigmoid
class NNet(object):
def __init__(self,
input=tensor.dvector('input'),
target=tensor.dvector('target'),
n_input=1, n_hidden=1, n_output=1, lr=1e-3, **kw):
input=tensor.dvector('input'),
target=tensor.dvector('target'),
n_input=1, n_hidden=1, n_output=1, lr=1e-3, **kw):
super(NNet, self).__init__(**kw)
self.input = input
......@@ -26,15 +27,15 @@ class NNet(object):
self.cost = tensor.sum((self.output - self.target)**2)
self.sgd_updates = {
self.w1: self.w1 - self.lr * tensor.grad(self.cost, self.w1),
self.w2: self.w2 - self.lr * tensor.grad(self.cost, self.w2)}
self.w1: self.w1 - self.lr * tensor.grad(self.cost, self.w1),
self.w2: self.w2 - self.lr * tensor.grad(self.cost, self.w2)}
self.sgd_step = pfunc(
params=[self.input, self.target],
outputs=[self.output, self.cost],
updates=self.sgd_updates)
params=[self.input, self.target],
outputs=[self.output, self.cost],
updates=self.sgd_updates)
self.compute_output = pfunc([self.input], self.output)
self.compute_output = pfunc([self.input], self.output)
self.output_from_hidden = pfunc([self.hidden], self.output)
......@@ -56,5 +57,5 @@ class TestNnet(unittest.TestCase):
# print 'Mean cost at epoch %s: %s' % (epoch, mean_cost)
self.assertTrue(abs(mean_cost - 0.20588975452) < 1e-6)
# Just call functions to make sure they do not crash.
out = nnet.compute_output(input)
out = nnet.output_from_hidden(numpy.ones(10))
nnet.compute_output(input)
nnet.output_from_hidden(numpy.ones(10))
......@@ -75,7 +75,7 @@ def test_not_inplace():
x = theano.tensor.vector('x')
mode = theano.compile.MonitorMode(post_func=detect_nan)
#mode = mode.excluding('fusion', 'inplace')
# mode = mode.excluding('fusion', 'inplace')
mode = mode.excluding('local_elemwise_fusion',
'inplace_elemwise_optimizer')
o = theano.tensor.outer(x, x)
......
......@@ -8,8 +8,9 @@ from theano.tensor import dmatrix, iscalar, lscalar, dmatrices
from theano import tensor
from theano.compile import In
from theano.compile.sharedvalue import *
from theano.compile.pfunc import *
from theano.compile import pfunc
from theano.compile import shared
from theano.compile import config
def data_of(s):
......@@ -196,7 +197,7 @@ class Test_pfunc(unittest.TestCase):
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self.assertRaises(TypeError, f,
[3], numpy.array([6], dtype='int16'), 1)
[3], numpy.array([6], dtype='int16'), 1)
# Value too big for a, silently ignored
assert numpy.all(f([2 ** 20], numpy.ones(1, dtype='int8'), 1) == 2)
......@@ -275,7 +276,7 @@ class Test_pfunc(unittest.TestCase):
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self.assertRaises(TypeError, g,
[3], numpy.array([6], dtype='int16'), 0)
[3], numpy.array([6], dtype='int16'), 0)
# Value too big for b, raises TypeError
self.assertRaises(TypeError, g, [3], [312], 0)
......@@ -284,7 +285,7 @@ class Test_pfunc(unittest.TestCase):
# Everything here should behave like with False
assert numpy.all(h([3], [6], 0) == 9)
self.assertRaises(TypeError, h,
[3], numpy.array([6], dtype='int16'), 0)
[3], numpy.array([6], dtype='int16'), 0)
self.assertRaises(TypeError, h, [3], [312], 0)
def test_allow_downcast_floatX(self):
......@@ -344,13 +345,13 @@ class Test_pfunc(unittest.TestCase):
# the update_var has type matrix, and the update expression
# is a broadcasted scalar, and that should be allowed.
self.assertRaises(TypeError, theano.function, inputs=[], outputs=[],
updates={output_var: output_var.sum().dimshuffle('x', 'x')})
updates={output_var: output_var.sum().dimshuffle('x', 'x')})
def test_duplicate_updates(self):
x, y = dmatrices('x', 'y')
z = shared(numpy.ones((2, 3)))
self.assertRaises(ValueError, theano.function, [x, y], [z],
updates=[(z, (z + x + y)), (z, (z - x))])
updates=[(z, (z + x + y)), (z, (z - x))])
def test_givens(self):
x = shared(0)
......@@ -366,7 +367,7 @@ class Test_pfunc(unittest.TestCase):
z = tensor.ivector()
c = z * y
f = pfunc([y], (c + 7),
givens={z: theano._asarray([4, 4, 4], dtype='int32')})
givens={z: theano._asarray([4, 4, 4], dtype='int32')})
assert numpy.all(f([1, 1, 1]) == [11, 11, 11])
assert x.get_value() == 0
......@@ -433,7 +434,7 @@ class Test_pfunc(unittest.TestCase):
self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=(x))
self.assertRaises(TypeError, pfunc, [], [x], no_default_updates=x)
self.assertRaises(TypeError, pfunc, [], [x],
no_default_updates='canard')
no_default_updates='canard')
# Mix explicit updates and no_default_updates
g1 = pfunc([], [x], updates=[(x, (x - 1))], no_default_updates=True)
......@@ -617,7 +618,7 @@ class Test_pfunc(unittest.TestCase):
def test_duplicate_inputs(self):
x = theano.tensor.lscalar('x')
self.assertRaises(theano.compile.UnusedInputError,
theano.function, [x, x, x], x)
theano.function, [x, x, x], x)
def test_update_same(self):
# There was a bug in CVM, triggered when a shared variable
......@@ -699,7 +700,7 @@ class Test_aliasing_rules(unittest.TestCase):
# rule #2 reading back from theano-managed memory
assert not numpy.may_share_memory(A.get_value(borrow=False),
data_of(A))
data_of(A))
def test_sparse_input_aliasing_affecting_inplace_operations(self):
##
......@@ -712,7 +713,7 @@ class Test_aliasing_rules(unittest.TestCase):
pass
from theano.sparse import enable_sparse
if enable_sparse == False:
if not enable_sparse:
raise SkipTest('Optional package sparse disabled')
from theano import sparse
......@@ -761,8 +762,8 @@ class Test_aliasing_rules(unittest.TestCase):
y = theano.tensor.dvector()
m1 = theano.tensor.dmatrix()
m2 = theano.tensor.dmatrix()
f = theano.function([theano.In(x, mutable=True),
theano.In(y, mutable=True),
f = theano.function([theano.In(x, mutable=True),
theano.In(y, mutable=True),
theano.In(m1, mutable=True),
theano.In(m2, mutable=True)],
theano.dot((x * 2), m1) + theano.dot((y * 3), m2))
......@@ -810,15 +811,14 @@ class Test_aliasing_rules(unittest.TestCase):
# c does not share memory with a
f = theano.function(
[theano.In(x, mutable=True),
theano.In(y, mutable=True),
theano.In(z, mutable=True),
theano.In(m1, mutable=True),
theano.In(m2, mutable=True),
theano.In(m3, mutable=True)],
(theano.dot((x * 2), m1)
+ theano.dot((y * 3), m2)
+ theano.dot((z * 4), m3)))
[theano.In(x, mutable=True),
theano.In(y, mutable=True),
theano.In(z, mutable=True),
theano.In(m1, mutable=True),
theano.In(m2, mutable=True),
theano.In(m3, mutable=True)],
(theano.dot((x * 2), m1) + theano.dot((y * 3), m2) +
theano.dot((z * 4), m3)))
# Compute bogus values
v = numpy.asarray([1, 2, 3, 4, 5], dtype='float64')
......@@ -875,14 +875,14 @@ class Test_aliasing_rules(unittest.TestCase):
assert not numpy.may_share_memory(R, data_of(A))
f = pfunc([D], (DD * 4),
updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 2))])
updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 2))])
R = f(C)
assert not numpy.may_share_memory(data_of(A), data_of(B))
assert not numpy.may_share_memory(R, data_of(B))
assert not numpy.may_share_memory(R, data_of(A))
f = pfunc([D], (DD * 4),
updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 3))])
updates=[(A, (DD[:1] * 3)), (B, (DD[:1] * 3))])
R = f(C)
assert not numpy.may_share_memory(data_of(A), data_of(B))
assert not numpy.may_share_memory(R, data_of(B))
......
......@@ -44,7 +44,7 @@ class Test_profiling(unittest.TestCase):
mode=m)
inp = [numpy.arange(1024, dtype='float32') + 1 for i in range(len(x))]
output = f(*inp)
f(*inp)
buf = StringIO()
f.profile.summary(buf)
......@@ -72,7 +72,6 @@ class Test_profiling(unittest.TestCase):
theano.config.profile_memory = config2
theano.config.profiling.min_peak_memory = config3
def test_ifelse(self):
config1 = theano.config.profile
config2 = theano.config.profile_memory
......@@ -101,7 +100,7 @@ class Test_profiling(unittest.TestCase):
big_mat1 = 10
big_mat2 = 11
out = f_ifelse(val1, val2, big_mat1, big_mat2)
f_ifelse(val1, val2, big_mat1, big_mat2)
finally:
theano.config.profile = config1
......
......@@ -3,27 +3,21 @@ import unittest
import theano
from theano.tensor import Tensor, TensorType
from theano.compile.sharedvalue import *
from theano.compile.sharedvalue import shared
from theano.compile.sharedvalue import SharedVariable
from theano.compile.sharedvalue import generic
class Test_SharedVariable(unittest.TestCase):
def test_ctors(self):
if 0:
# when using an implementation that handles scalars with
# Scalar type
assert shared(7).type == Scalar('int64')
assert shared(7.0).type == Scalar('float64')
assert shared(7, dtype='float64').type == Scalar('float64')
if theano.configdefaults.python_int_bitwidth() == 32:
assert shared(7).type == theano.tensor.iscalar, shared(7).type
else:
if theano.configdefaults.python_int_bitwidth() == 32:
assert shared(7).type == theano.tensor.iscalar, shared(7).type
else:
assert shared(7).type == theano.tensor.lscalar, shared(7).type
assert shared(7.0).type == theano.tensor.dscalar
assert shared(numpy.float32(7)).type == theano.tensor.fscalar
assert shared(7).type == theano.tensor.lscalar, shared(7).type
assert shared(7.0).type == theano.tensor.dscalar
assert shared(numpy.float32(7)).type == theano.tensor.fscalar
# test tensor constructor
b = shared(numpy.zeros((5, 5), dtype='int32'))
......@@ -31,8 +25,7 @@ class Test_SharedVariable(unittest.TestCase):
b = shared(numpy.random.rand(4, 5))
assert b.type == TensorType('float64', broadcastable=[False, False])
b = shared(numpy.random.rand(5, 1, 2))
assert b.type == TensorType('float64',
broadcastable=[False, False, False])
assert b.type == TensorType('float64', broadcastable=[False, False, False])
assert shared([]).type == generic
......@@ -56,35 +49,35 @@ class Test_SharedVariable(unittest.TestCase):
# here the value is perfect, and we're not strict about it,
# so creation should work
SharedVariable(
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=numpy.asarray([1., 2.]),
strict=False)
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=numpy.asarray([1., 2.]),
strict=False)
# here the value is castable, and we're not strict about it,
# so creation should work
SharedVariable(
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=[1., 2.],
strict=False)
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=[1., 2.],
strict=False)
# here the value is castable, and we're not strict about it,
# so creation should work
SharedVariable(
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=[1, 2], # different dtype and not a numpy array
strict=False)
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=[1, 2], # different dtype and not a numpy array
strict=False)
# here the value is not castable, and we're not strict about it,
# this is beyond strictness, it must fail
try:
SharedVariable(
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=dict(), # not an array by any stretch
strict=False)
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=dict(), # not an array by any stretch
strict=False)
assert 0
except TypeError:
pass
......@@ -94,10 +87,10 @@ class Test_SharedVariable(unittest.TestCase):
# here the value is perfect, and we're not strict about it,
# so creation should work
u = SharedVariable(
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=numpy.asarray([1., 2.]),
strict=False)
name='u',
type=Tensor(broadcastable=[False], dtype='float64'),
value=numpy.asarray([1., 2.]),
strict=False)
# check that assignments to value are cast properly
u.set_value([3, 4])
......@@ -298,7 +291,7 @@ class Test_SharedVariable(unittest.TestCase):
# f(b,[8])
b = shared(numpy.asarray([7.234], dtype=theano.config.floatX),
allow_downcast=True)
allow_downcast=True)
assert b.dtype == theano.config.floatX
f(b, [8])
assert b.get_value() == 8
......
......@@ -30,15 +30,6 @@ whitelist_flake8 = [
"tests/__init__.py",
"compile/__init__.py",
"compile/profiling.py",
"compile/tests/test_builders.py",
"compile/tests/test_misc.py",
"compile/tests/test_monitormode.py",
"compile/tests/test_function_module.py",
"compile/tests/test_shared.py",
"compile/tests/test_ops.py",
"compile/tests/test_pfunc.py",
"compile/tests/test_debugmode.py",
"compile/tests/test_profiling.py",
"typed_list/__init__.py",
"tensor/__init__.py",
"tensor/tests/test_subtensor.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论