comparison xlogx.py @ 450:117e5b09cf31

Added an XlogX op.
author Joseph Turian <turian@gmail.com>
date Thu, 04 Sep 2008 14:46:17 -0400
parents
children
comparison
equal deleted inserted replaced
449:2bb67e978c28 450:117e5b09cf31
1
2 import theano
3 from theano import tensor, scalar
4 import numpy
5
6 class XlogX(scalar.UnaryScalarOp):
7 """
8 Compute X * log(X), with special case 0 log(0) = 0.
9 """
10 @staticmethod
11 def st_impl(x):
12 if x == 0.0:
13 return 0.0
14 return x * numpy.log(x)
15 def impl(self, x):
16 return XlogX.st_impl(x)
17 def grad(self, (x,), (gz,)):
18 return [gz * (1 + scalar.log(x))]
19 def c_code(self, node, name, (x,), (z,), sub):
20 if node.inputs[0].type in [scalar.float32, scalar.float64]:
21 return """%(z)s =
22 %(x)s == 0.0
23 ? 0.0
24 : %(x)s * log(%(x)s);""" % locals()
25 raise NotImplementedError('only floatingpoint is implemented')
26 scalar_xlogx = XlogX(scalar.upgrade_to_float, name='scalar_xlogx')
27 xlogx = tensor.Elemwise(scalar_xlogx, name='xlogx')
28