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

Added log1p Op

上级 072bbb0f
......@@ -1178,6 +1178,18 @@ class Log10(UnaryScalarOp):
return "%(z)s = log10(%(x)s);" % locals()
log10 = Log10(upgrade_to_float, name = 'log10')
class Log1p(UnaryScalarOp):
""" log(1+x) """
def impl(self, x):
return numpy.log1p(x)
def grad(self, (x,), (gz,)):
return [gz / (1+x)]
def c_code(self, node, name, (x, ), (z, ), sub):
if node.inputs[0].type in complex_types:
raise NotImplementedError('type not supported', type)
return "%(z)s = log1p(%(x)s);" % locals()
log1p = Log1p(upgrade_to_float, name = 'log1p')
class Exp(UnaryScalarOp):
def impl(self, x):
return numpy.exp(x)
......
......@@ -1437,6 +1437,10 @@ def log2(a):
def log10(a):
"""base 10 logarithm of a"""
@_scal_elemwise
def log1p(a):
"""log(1+a)"""
@_scal_elemwise
def sgn(a):
"""sign of a"""
......
......@@ -100,6 +100,10 @@ def inv_inplace(a):
def log_inplace(a):
"""base e logarithm of a (inplace on a)"""
@_scal_inplace
def log1p_inplace(a):
"""log(1+a)"""
@_scal_inplace
def log2_inplace(a):
"""base 2 logarithm of a (inplace on a)"""
......
......@@ -444,6 +444,17 @@ Log10InplaceTester = makeBroadcastTester(op = inplace.log10_inplace,
grad = _grad_broadcast_unary_positive,
inplace = True)
Log1pTester = makeBroadcastTester(op = log1p,
expected = numpy.log1p,
good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive)
Log1pInplaceTester = makeBroadcastTester(op = inplace.log1p_inplace,
expected = numpy.log1p,
good = _good_broadcast_unary_positive,
grad = _grad_broadcast_unary_positive,
inplace = True)
SqrtTester = makeBroadcastTester(op = sqrt,
expected = numpy.sqrt,
good = _good_broadcast_unary_positive,
......@@ -1088,9 +1099,7 @@ class test_bitwise(unittest.TestCase):
self.failUnless(numpy.all(v == (~l)), (l, r, v))
class T_add(unittest.TestCase):
def setUp(self):
utt.seed_rng()
......@@ -1118,7 +1127,6 @@ class T_add(unittest.TestCase):
utt.verify_grad(add, [numpy.random.rand(3, 5), numpy.random.rand(3, 1)])
class T_exp(unittest.TestCase):
def test_grad_0(self):
utt.verify_grad(exp, [
numpy.asarray([[ 1.5089518 , 1.48439076, -4.7820262 ],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论