提交 a3d94ccd authored 作者: James Bergstra's avatar James Bergstra

added xlogy0 op

上级 546cf660
from theano.tensor.xlogx import xlogx from theano.tensor.xlogx import xlogx, xlogy0
import unittest import unittest
...@@ -25,6 +25,19 @@ class T_XlogX(unittest.TestCase): ...@@ -25,6 +25,19 @@ class T_XlogX(unittest.TestCase):
# return [xlogx(a)[:,2]] # return [xlogx(a)[:,2]]
utt.verify_grad(xlogx, [numpy.random.rand(3,4)]) utt.verify_grad(xlogx, [numpy.random.rand(3,4)])
class T_XlogY0(unittest.TestCase):
def setUp(self):
utt.seed_rng()
def test2(self):
utt.verify_grad(xlogy0, [numpy.random.rand(3,4), numpy.random.rand(3,4)])
def test3(self):
x = as_tensor_variable([1, 0])
y = as_tensor_variable([1, 0])
z = xlogy0(x, y)
f = theano.function([], z)
self.failUnless(numpy.all(f() == numpy.asarray([0, 0.])))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -26,3 +26,27 @@ class XlogX(scalar.UnaryScalarOp): ...@@ -26,3 +26,27 @@ class XlogX(scalar.UnaryScalarOp):
scalar_xlogx = XlogX(scalar.upgrade_to_float, name='scalar_xlogx') scalar_xlogx = XlogX(scalar.upgrade_to_float, name='scalar_xlogx')
xlogx = tensor.Elemwise(scalar_xlogx, name='xlogx') xlogx = tensor.Elemwise(scalar_xlogx, name='xlogx')
class XlogY0(scalar.BinaryScalarOp):
"""
Compute X * log(Y), with special case 0 log(0) = 0.
"""
@staticmethod
def st_impl(x, y):
if x == 0.0:
return 0.0
return x * numpy.log(y)
def impl(self, x, y):
return XlogY0.st_impl(x, y)
def grad(self, (x, y), (gz,)):
return [gz * scalar.log(y), gz * x / y]
def c_code(self, node, name, (x, y), (z,), sub):
if node.inputs[0].type in [scalar.float32, scalar.float64]:
return """%(z)s =
%(x)s == 0.0
? 0.0
: %(x)s * log(%(y)s);""" % locals()
raise NotImplementedError('only floatingpoint is implemented')
scalar_xlogy0 = XlogY0(scalar.upgrade_to_float, name='scalar_xlogy0')
xlogy0 = tensor.Elemwise(scalar_xlogy0, name='xlogy0')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论