提交 6a3595f2 authored 作者: Frederic Bastien's avatar Frederic Bastien

added an option to verify_grad(cast_to_output_type) to force the casting of the…

added an option to verify_grad(cast_to_output_type) to force the casting of the output to the same type as the op expect. This is needed to test the ConvOp grad in float32 type. Otherwise it upcast it to float64.
上级 bd63b854
...@@ -2486,7 +2486,7 @@ class numeric_grad: ...@@ -2486,7 +2486,7 @@ class numeric_grad:
return numpy.max(errs), numpy.argmax(errs) return numpy.max(errs), numpy.argmax(errs)
def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None): def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None, cast_to_output_type=False):
""" WRITEME """ WRITEME
Raises an Exception if the difference between the analytic gradient and Raises an Exception if the difference between the analytic gradient and
...@@ -2537,8 +2537,12 @@ def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None): ...@@ -2537,8 +2537,12 @@ def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None):
o_fn = function(tensor_pt, o_output) o_fn = function(tensor_pt, o_output)
o_fn_out = o_fn(*[p.copy() for p in pt]) o_fn_out = o_fn(*[p.copy() for p in pt])
random_projection = rng.rand(*o_fn_out.shape) random_projection = rng.rand(*o_fn_out.shape)
if cast_to_output_type:
random_projection = numpy.array(random_projection,
dtype=o_output.dtype)
t_r = as_tensor_variable(random_projection) t_r = as_tensor_variable(random_projection)
#random projection of o onto t_r #random projection of o onto t_r
...@@ -2547,6 +2551,10 @@ def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None): ...@@ -2547,6 +2551,10 @@ def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None):
num_grad = numeric_grad(cost_fn, [p.copy() for p in pt], eps) num_grad = numeric_grad(cost_fn, [p.copy() for p in pt], eps)
g_cost = as_tensor_variable(1.0,name='g_cost')
if cast_to_output_type:
g_cost = cast(g_cost, o_output.dtype)
symbolic_grad = grad(cost, tensor_pt,as_tensor_variable(1.0,name='g_cost')) symbolic_grad = grad(cost, tensor_pt,as_tensor_variable(1.0,name='g_cost'))
grad_fn = function(tensor_pt, symbolic_grad) grad_fn = function(tensor_pt, symbolic_grad)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论