提交 af0fbb52 authored 作者: Frederic Bastien's avatar Frederic Bastien

refactored TensorSharedVariable test of shared options to be able to reused them…

refactored TensorSharedVariable test of shared options to be able to reused them for CudaNdarrayShared.
上级 3949f88b
......@@ -802,6 +802,8 @@ def test_duplicate_arg_elemwise():
assert numpy.allclose(Bval,f(Aval))
import theano.tensor.tests.test_basic
test_shared_options = theano.tensor.tests.test_basic.build_test_shared_options(tcn.shared_constructor, 'float32', False, False)
if __name__ == '__main__':
test_many_arg_elemwise()
......
......@@ -3378,61 +3378,94 @@ def test_dimshuffle_duplicate():
assert success
def test_shared_dont_alias():
rng = numpy.random.RandomState([3,5,17])
x = rng.uniform(0,1,[2,4])
x_shared = theano.shared(x, borrow = False)
def build_test_shared_options(shared_constructor_,
dtype_,
get_value_borrow_true_alias_,
shared_borrow_true_alias_):
"""
This is a generic fct to allow reusing the same test function
for many shared variable of many types.
"""
class SharedTester(unittest.TestCase):
shared_constructor = staticmethod(shared_constructor_)
dtype = dtype_
get_value_borrow_true_alias = get_value_borrow_true_alias_
shared_borrow_true_alias = shared_borrow_true_alias_
def test_shared_dont_alias(self):
dtype = self.dtype
if dtype is None:
dtype = theano.config.floatX
total = theano.tensor.sum(x_shared)
total_func = theano.function([],total)
rng = numpy.random.RandomState([3,5,17])
x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype)
x_sum = x.sum()
x_shared = self.shared_constructor(x, borrow = False)
total = theano.tensor.sum(x_shared)
total_val = total_func()
total_func = theano.function([],total)
assert numpy.allclose(x.sum(), total_val)
total_val = total_func()
x += 1
assert numpy.allclose(x.sum(), total_val)
total_val_2 = total_func()
x += 1
#value used to construct should not alias with internal
assert total_val == total_val_2
total_val_2 = total_func()
x = x_shared.get_value(borrow = False)
#value used to construct should not alias with internal
assert total_val == total_val_2
x += 1
x = x_shared.get_value(borrow = False)
total_val_3 = total_func()
x += 1
#value returned by access should not alias with internal
assert total_val == total_val_3
total_val_3 = total_func()
#in this case we can alias
x = x_shared.get_value(borrow = True)
x += 1
#value returned by access should not alias with internal
assert total_val == total_val_3
#this is not required by the contract but it is a feature we've implemented
assert numpy.allclose(x.sum(), total_func())
#in this case we can alias
x = x_shared.get_value(borrow = True)
x += 1
#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(x.sum(), total_func())
else:
assert numpy.allclose(x_sum, total_func())
def test_shared_do_alias():
rng = numpy.random.RandomState([2,4,16])
x = rng.uniform(1,2,[4,2])
def test_shared_do_alias(self):
dtype = self.dtype
if dtype is None:
dtype = theano.config.floatX
x_shared = theano.shared(x, borrow = True)
rng = numpy.random.RandomState([2,4,16])
x = numpy.asarray(rng.uniform(1,2,[4,2]),dtype=dtype)
x_sum = x.sum()
total = theano.tensor.sum(x_shared)
x_shared = self.shared_constructor(x, borrow = True)
total_func = theano.function([],total)
total = theano.tensor.sum(x_shared)
total_val = total_func()
total_func = theano.function([],total)
assert numpy.allclose(x.sum(), total_val)
total_val = total_func()
assert numpy.allclose(x.sum(), total_val)
x += 1
#not required by the contract but it is a feature we've implemented
if self.shared_borrow_true_alias:
assert numpy.allclose(x.sum(), total_func())
else:
assert numpy.allclose(x_sum, total_func())
x += 1
return SharedTester
#not required by the contract but it is a feature we've implemented
assert numpy.allclose(x.sum(), total_func())
test_shared_options=build_test_shared_options(tensor.shared, 'float64', True, True)
if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论