提交 c73dd4d6 authored 作者: lamblin's avatar lamblin

Merge pull request #1010 from goodfeli/fix_clip_grad

Fix clip grad
...@@ -1605,13 +1605,21 @@ class Clip(ScalarOp): ...@@ -1605,13 +1605,21 @@ class Clip(ScalarOp):
def c_code(self, node, name, (x, min, max), (z, ), sub): def c_code(self, node, name, (x, min, max), (z, ), sub):
return "%(z)s = %(x)s < %(min)s ? %(min)s : %(x)s > %(max)s ? %(max)s : %(x)s;" % locals() return "%(z)s = %(x)s < %(min)s ? %(min)s : %(x)s > %(max)s ? %(max)s : %(x)s;" % locals()
def grad(self, (x, min, max), (gz, )): def grad(self, (x, mn, mx), (gz, )):
assert gz.type not in complex_types assert gz.type not in complex_types
gx = ((x > min) & (x < max)) * gz gx = ((x > mn) & (x < mx)) * gz
if x.type in float_types: gmn = (x < mn) * gz
return gx, None, None gmx = (x > mx) * gz
else:
return None, None, None out = self(x, mn, mx)
def handle_int(v):
if out.type in int_types:
return v.zeros_like().astype(config.floatX)
return v
return map(handle_int, [gx, gmn, gmx])
# Don't allow complex even if numpy do # Don't allow complex even if numpy do
# As there is no mathematical reason for this function on complex # As there is no mathematical reason for this function on complex
clip = Clip(upcast_out_no_complex, name='clip') clip = Clip(upcast_out_no_complex, name='clip')
......
...@@ -883,6 +883,25 @@ if __name__ == '__main__': ...@@ -883,6 +883,25 @@ if __name__ == '__main__':
unittest.TextTestRunner().run(suite) unittest.TextTestRunner().run(suite)
""" """
def test_clip_grad():
# test the gradient of clip
def func(x,y,z):
return theano.tensor.clip(x,y,z)
# use an x value less than y, an x value between y and z, and an x value
# greater than z
unittest_tools.verify_grad(func,
[ numpy.asarray([-1.,0.5,2.]), 0., 1.])
def test_clip_grad_int():
# test that integers don't crash clip gradient
x = tensor.iscalar()
y = tensor.iscalar()
z = tensor.iscalar()
c = tensor.clip(x,y,z)
tensor.grad(c, [x, y, z])
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论