提交 15522a96 authored 作者: Kyung Hyun Cho's avatar Kyung Hyun Cho

added a unit test for strict mode

上级 c07dcdb5
...@@ -46,11 +46,11 @@ else: ...@@ -46,11 +46,11 @@ else:
mode_with_gpu = mode_with_opt.including('gpu', 'scan') mode_with_gpu = mode_with_opt.including('gpu', 'scan')
type_eps = {'float64': 1e-7,
'float32': 3e-3}
class multiple_outputs_numeric_grad: class multiple_outputs_numeric_grad:
"""WRITEME""" """WRITEME"""
type_eps = {'float64': 1e-7,
'float32': 3e-3}
def __init__(self, f, pt, ndarray_mask=None, eps=None): def __init__(self, f, pt, ndarray_mask=None, eps=None):
"""Return the gradient of f at pt. """Return the gradient of f at pt.
...@@ -3897,6 +3897,42 @@ class T_Scan(unittest.TestCase): ...@@ -3897,6 +3897,42 @@ class T_Scan(unittest.TestCase):
f = theano.function([W, n_steps], H) f = theano.function([W, n_steps], H)
f(numpy.ones((8,), dtype='float32'), 1) f(numpy.ones((8,), dtype='float32'), 1)
def test_strict_mode(self):
n = 10
w = numpy.array([[-1,2],[3,-4]]).astype(theano.config.floatX)
w_ = theano.shared(w)
x0 = numpy.array([1,2]).astype(theano.config.floatX)
x0_ = tensor.vector(name='x0', dtype=theano.config.floatX)
def _scan_loose(x):
return tensor.dot(x, w_)
def _scan_strict(x, w_ns):
return tensor.dot(x, w_ns)
ret_loose = theano.scan(_scan_loose,
sequences=[],
outputs_info=[x0_],
n_steps=n,
strict=False)
f_loose = theano.function([x0_], ret_loose[0][-1])
ret_strict = theano.scan(_scan_strict,
sequences=[],
outputs_info=[x0_],
non_sequences=[w_],
n_steps=n,
strict=True)
f_strict = theano.function([x0_], ret_strict[0][-1])
result_loose = f_loose(x0)
result_strict = f_strict(x0)
diff = (abs(result_loose - result_strict)).mean()
assert diff <= type_eps[theano.config.floatX]
def test_speed(): def test_speed():
# #
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论