提交 86613b02 authored 作者: Frederic's avatar Frederic

Reenabled the grad of Images2Neibs. The problem was in the tests, not op.grad()

The problem was that we tested sqr(images2neibs(x)) with too big values. This caused problem when we compute the numerical gradient.
上级 9783a17d
...@@ -84,8 +84,17 @@ class Images2Neibs(Op): ...@@ -84,8 +84,17 @@ class Images2Neibs(Op):
def grad(self, inp, grads): def grad(self, inp, grads):
x, neib_shape, neib_step = inp x, neib_shape, neib_step = inp
gz, = grads gz, = grads
return [ grad_not_implemented(self, i, ip) \
for i, ip in enumerate(inp) ] if self.mode in ['valid', 'ignore_borders']:
if (neib_shape is neib_step or
neib_shape == neib_step or
# Theano Constant == do not compare the data
# the equals function do that.
(hasattr(neib_shape, "equals") and
neib_shape.equals(neib_step))):
return [neibs2images(gz, neib_shape, x.shape, mode=self.mode),
None, None]
return [grad_not_implemented(self, 0, x), None, None]
def c_code_cache_version(self): def c_code_cache_version(self):
return (5,) return (5,)
......
...@@ -323,61 +323,72 @@ def speed_neibs_wrap_centered(): ...@@ -323,61 +323,72 @@ def speed_neibs_wrap_centered():
for i in range(1000): for i in range(1000):
f() f()
# Disable the test as the grad is wrongly implemented
def tes_neibs_grad_verify_grad(): class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_grad_valid(self):
shape = (2, 3, 4, 4) shape = (2, 3, 4, 4)
images = T.dtensor4() images_val = numpy.random.rand(*shape).astype('float32')
images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (2, 2))), axis=[0, 1]) return images2neibs(images, (2, 2))
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu,
eps=0.1)
if cuda.cuda_available: if cuda.cuda_available:
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu,
eps=0.1)
# The grad is not defined when the neib_shape and neib_step
# are not the same.
def fn(images):
return images2neibs(images, (2, 2), (1, 1))
self.assertRaises(NotImplementedError,
unittest_tools.verify_grad, fn, [images_val],
mode=mode_without_gpu)
def test_neibs_grad_verify_grad_warp_centered(): def test_grad_warp_centered(self):
# It is not implemented for now. So test that we raise an error. # It is not implemented for now. So test that we raise an error.
shape = (2, 3, 6, 6) shape = (2, 3, 6, 6)
images = T.dtensor4() images_val = numpy.random.rand(*shape).astype('float32')
images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (3, 3), mode='wrap_centered')), return images2neibs(images, (3, 3), mode='wrap_centered')
axis=[0, 1])
try:
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
raise Exception("Expected an error")
except NotImplementedError:
pass
if cuda.cuda_available: self.assertRaises(NotImplementedError, unittest_tools.verify_grad,
try: fn, [images_val], mode=mode_without_gpu)
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu)
raise Exception("Expected an error")
except NotImplementedError:
pass
if cuda.cuda_available:
self.assertRaises(NotImplementedError, unittest_tools.verify_grad,
fn, [images_val], mode=mode_with_gpu)
def test_neibs_ignore_border(): def test_grad_ignore_border(self):
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), images_val = numpy.random.rand(*shape).astype('float32')
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (2, 2), return images2neibs(images, (2, 2),
mode='ignore_borders')), axis=[0, 1]) mode='ignore_borders')
# Disable the test as the grad is wrongly implemented unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu,
#unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) eps=0.1)
# not implemented for gpu # GPU code not implemented in that case, but is should still
# if cuda.cuda_available: # not crash.
# unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu) if cuda.cuda_available:
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu,
eps=0.1)
def test_neibs2images_crash_on_grad(self):
# say we had images of size (2, 3, 20, 20)
# then we extracted 2x2 neighbors on this, we get (2 * 3 * 10 * 10, 4)
neibs = T.dmatrix()
neibs_val = numpy.random.rand(600, 4)
to_images = T.sum(neibs2images(neibs, (2, 2), (2, 3, 20, 20)))
g = T.grad(to_images, neibs)
fn = theano.function([neibs], to_images, mode=mode_without_gpu)
#print "Compiled"
fn(neibs_val)
def test_neibs_valid_with_inconsistent_borders(): def test_neibs_valid_with_inconsistent_borders():
...@@ -401,18 +412,6 @@ def test_neibs_valid_with_inconsistent_borders(): ...@@ -401,18 +412,6 @@ def test_neibs_valid_with_inconsistent_borders():
pass pass
# Disable the test as the grad is wrongly implemented
def tes_neibs2images_crash_on_grad():
# say we had images of size (2, 3, 20, 20)
# then we extracted 2x2 neighbors on this, we get (2 * 3 * 10 * 10, 4)
neibs = T.dmatrix()
neibs_val = numpy.random.rand(600, 4)
to_images = T.sum(neibs2images(neibs, (2, 2), (2, 3, 20, 20)))
g = T.grad(to_images, neibs)
fn = theano.function([neibs], to_images, mode=mode_without_gpu)
#print "Compiled"
fn(neibs_val)
if __name__ == '__main__': if __name__ == '__main__':
#test_neibs_gpu() #test_neibs_gpu()
#test_neibs() #test_neibs()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论