passing current tests

上级 688ad973
...@@ -14,6 +14,7 @@ import gof, gof.graph ...@@ -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): 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) """ """testcase.failUnless( analytic gradient matches finite-diff gradient) """
pt = [numpy.asarray(p) for p in pt]
for test_num in xrange(n_tests): for test_num in xrange(n_tests):
tensor_pt = [tinit(p,name='input %i'%i) for i,p in enumerate(pt)] tensor_pt = [tinit(p,name='input %i'%i) for i,p in enumerate(pt)]
...@@ -165,7 +166,8 @@ class T_mul(unittest.TestCase): ...@@ -165,7 +166,8 @@ class T_mul(unittest.TestCase):
[numpy.ones(3), numpy.ones(4)], 1.0) [numpy.ones(3), numpy.ones(4)], 1.0)
self.fail() self.fail()
except ValueError, e: except ValueError, e:
self.failUnless(e is tensor._assert_same_shapes.E_shape) s = str(e[0])
self.failUnless(s.startswith(tensor._assert_same_shapes.E_shape))
class T_div(unittest.TestCase): class T_div(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -105,8 +105,8 @@ def _assert_same_shapes(x, *rest): ...@@ -105,8 +105,8 @@ def _assert_same_shapes(x, *rest):
shape = x.shape shape = x.shape
for other in rest: for other in rest:
if other.shape != shape: if other.shape != shape:
raise _assert_same_shapes.E_shape raise ValueError(_assert_same_shapes.E_shape)
_assert_same_shapes.E_shape = ValueError("The dimensions of the inputs do not match.") _assert_same_shapes.E_shape = "The dimensions of the inputs do not match."
def _assert_tensor_scalar(x, a): def _assert_tensor_scalar(x, a):
"""ensure that the second input is a scalar""" """ensure that the second input is a scalar"""
...@@ -148,11 +148,8 @@ class _Op(Op): ...@@ -148,11 +148,8 @@ class _Op(Op):
def propagate_dtype(self, *i_dtypes): def propagate_dtype(self, *i_dtypes):
def upcast(dtype, *dtypes): def upcast(dtype, *dtypes):
z = numpy.zeros((), dtype = dtype) z = numpy.zeros((), dtype = dtype)
#print '----', self.__class__
#print type(z), dtype
for dtype in dtypes: for dtype in dtypes:
z = z + numpy.zeros((), dtype = dtype) z = z + numpy.zeros((), dtype = dtype)
#print type(z), type(dtype), dtype
return str(z.dtype) return str(z.dtype)
for dtype in i_dtypes: for dtype in i_dtypes:
if dtype is None: if dtype is None:
...@@ -572,8 +569,8 @@ class PowElemwise(_Elemwise): ...@@ -572,8 +569,8 @@ class PowElemwise(_Elemwise):
return x ** y return x ** y
def grad(self, (x, y), gz): def grad(self, (x, y), gz):
gx = gz * y * (pow_elemwise(x, y-1.0)) gx = gz * y * (pow_elemwise(x, y-1.0))
gs = gz * log(x) * pow_elemwise(x, y) gy = gz * log(x) * pow_elemwise(x, y)
return gx, gs return gx, gy
def c_foreach(self, (x_i, y_i), (z_i, )): def c_foreach(self, (x_i, y_i), (z_i, )):
return "%(z)s_i = pow(%(x)s_i, %(y)s_i);" return "%(z)s_i = pow(%(x)s_i, %(y)s_i);"
pow_elemwise = _constructor(PowElemwise) pow_elemwise = _constructor(PowElemwise)
...@@ -587,13 +584,13 @@ pow_elemwise_inplace = _constructor(PowElemwiseInplace) ...@@ -587,13 +584,13 @@ pow_elemwise_inplace = _constructor(PowElemwiseInplace)
# Scalar # # Scalar #
class PowScalarL(TensorScalarOp): class PowScalarL(TensorScalarOp):
def impl(self, x, a): def impl(self, y, x):
_assert_tensor_scalar(x, a) _assert_tensor_scalar(y, x)
return a ** x return x ** y
def grad(self, (x, s), gz): def grad(self, (y, x), gz):
gx = sum(gz * s * pow_scalar_l(add_scalar(s,-1.0), x)) gx = sum(gz * y * x ** (y-1.0))
gs = scale(mul(gz, pow_scalar_l(s, x)), log(x)) gy = gz * log(x) * x ** y
return gx, gs return gy, gx
c_expr = "pow(%(a)s, %(x)s_i)" c_expr = "pow(%(a)s, %(x)s_i)"
pow_scalar_l = _constructor(PowScalarL) pow_scalar_l = _constructor(PowScalarL)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论