提交 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,12 +3378,29 @@ def test_dimshuffle_duplicate(): ...@@ -3378,12 +3378,29 @@ 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_):
"""
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_
x_shared = theano.shared(x, borrow = False) def test_shared_dont_alias(self):
dtype = self.dtype
if dtype is None:
dtype = theano.config.floatX
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 = theano.tensor.sum(x_shared)
total_func = theano.function([],total) total_func = theano.function([],total)
...@@ -3412,14 +3429,23 @@ def test_shared_dont_alias(): ...@@ -3412,14 +3429,23 @@ def test_shared_dont_alias():
x = x_shared.get_value(borrow = True) x = x_shared.get_value(borrow = True)
x += 1 x += 1
#this is not required by the contract but it is a feature we've implemented #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()) assert numpy.allclose(x.sum(), total_func())
else:
assert numpy.allclose(x_sum, total_func())
def test_shared_do_alias(self):
dtype = self.dtype
if dtype is None:
dtype = theano.config.floatX
def test_shared_do_alias():
rng = numpy.random.RandomState([2,4,16]) rng = numpy.random.RandomState([2,4,16])
x = rng.uniform(1,2,[4,2]) x = numpy.asarray(rng.uniform(1,2,[4,2]),dtype=dtype)
x_sum = x.sum()
x_shared = theano.shared(x, borrow = True) x_shared = self.shared_constructor(x, borrow = True)
total = theano.tensor.sum(x_shared) total = theano.tensor.sum(x_shared)
...@@ -3432,7 +3458,14 @@ def test_shared_do_alias(): ...@@ -3432,7 +3458,14 @@ def test_shared_do_alias():
x += 1 x += 1
#not required by the contract but it is a feature we've implemented #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()) assert numpy.allclose(x.sum(), total_func())
else:
assert numpy.allclose(x_sum, total_func())
return SharedTester
test_shared_options=build_test_shared_options(tensor.shared, 'float64', True, True)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论