提交 8ee3b6d6 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

merge

......@@ -14,6 +14,7 @@ import gof, gof.graph
def verify_grad(testcase, op_cls, pt, n_tests=1, rng=numpy.random, eps=0.0000001, tol=0.0001):
"""testcase.failUnless( analytic gradient matches finite-diff gradient) """
pt = [numpy.asarray(p) for p in pt]
for test_num in xrange(n_tests):
tensor_pt = [tinit(p,name='input %i'%i) for i,p in enumerate(pt)]
......@@ -165,7 +166,7 @@ class T_mul(unittest.TestCase):
[numpy.ones(3), numpy.ones(4)], 1.0)
self.fail()
except ValueError, e:
self.failUnless(e is tensor._assert_same_shapes.E_shape)
self.failUnless(e[0] is tensor._assert_same_shapes.E_shape)
class T_div(unittest.TestCase):
def setUp(self):
......
......@@ -105,8 +105,8 @@ def _assert_same_shapes(x, *rest):
shape = x.shape
for other in rest:
if other.shape != shape:
raise _assert_same_shapes.E_shape
_assert_same_shapes.E_shape = ValueError("The dimensions of the inputs do not match.")
raise ValueError(_assert_same_shapes.E_shape)
_assert_same_shapes.E_shape = "The dimensions of the inputs do not match."
def _assert_tensor_scalar(x, a):
"""ensure that the second input is a scalar"""
......@@ -585,8 +585,8 @@ class PowElemwise(_Elemwise):
return x ** y
def grad(self, (x, y), gz):
gx = gz * y * (pow_elemwise(x, y-1.0))
gs = gz * log(x) * pow_elemwise(x, y)
return gx, gs
gy = gz * log(x) * pow_elemwise(x, y)
return gx, gy
def c_foreach(self, (x_i, y_i), (z_i, )):
return "%(z)s_i = pow(%(x)s_i, %(y)s_i);"
pow_elemwise = _constructor(PowElemwise)
......@@ -600,13 +600,13 @@ pow_elemwise_inplace = _constructor(PowElemwiseInplace)
# Scalar #
class PowScalarL(TensorScalarOp):
def impl(self, x, a):
_assert_tensor_scalar(x, a)
return a ** x
def grad(self, (x, s), gz):
gx = sum(gz * s * pow_scalar_l(add_scalar(s,-1.0), x))
gs = scale(mul(gz, pow_scalar_l(s, x)), log(x))
return gx, gs
def impl(self, y, x):
_assert_tensor_scalar(y, x)
return x ** y
def grad(self, (y, x), gz):
gx = sum(gz * y * x ** (y-1.0))
gy = gz * log(x) * x ** y
return gy, gx
c_expr = "pow(%(a)s, %(x)s_i)"
pow_scalar_l = _constructor(PowScalarL)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论