提交 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(): ...@@ -802,6 +802,8 @@ def test_duplicate_arg_elemwise():
assert numpy.allclose(Bval,f(Aval)) 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__': if __name__ == '__main__':
test_many_arg_elemwise() test_many_arg_elemwise()
......
...@@ -3378,61 +3378,94 @@ def test_dimshuffle_duplicate(): ...@@ -3378,61 +3378,94 @@ def test_dimshuffle_duplicate():
assert success assert success
def test_shared_dont_alias(): def build_test_shared_options(shared_constructor_,
rng = numpy.random.RandomState([3,5,17]) dtype_,
x = rng.uniform(0,1,[2,4]) get_value_borrow_true_alias_,
shared_borrow_true_alias_):
x_shared = theano.shared(x, borrow = False) """
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) rng = numpy.random.RandomState([3,5,17])
x = numpy.asarray(rng.uniform(0,1,[2,4]),dtype=dtype)
total_func = theano.function([],total) 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 total_val_2 = total_func()
assert total_val == total_val_2
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 total_val_3 = total_func()
assert total_val == total_val_3
#in this case we can alias #value returned by access should not alias with internal
x = x_shared.get_value(borrow = True) assert total_val == total_val_3
x += 1
#this is not required by the contract but it is a feature we've implemented #in this case we can alias
assert numpy.allclose(x.sum(), total_func()) 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(): def test_shared_do_alias(self):
rng = numpy.random.RandomState([2,4,16]) dtype = self.dtype
x = rng.uniform(1,2,[4,2]) 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 test_shared_options=build_test_shared_options(tensor.shared, 'float64', True, True)
assert numpy.allclose(x.sum(), total_func())
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论