提交 531b3ecd authored 作者: Frederic's avatar Frederic

Disabled the grad on Images2Neib as it was doing the inverse and not the grad!

When tring to make the grad raise an Exception with an error that the old version returned wrong answer.
上级 ba1d49f1
......@@ -64,8 +64,13 @@ class Images2Neibs(Op):
def grad(self, inp, grads):
x, neib_shape, neib_step = inp
gz, = grads
if self.mode in ['valid','ignore_borders']:
return [neibs2images(gz, neib_shape, x.shape, mode=self.mode), None, None]
if self.mode in ['valid', 'ignore_borders']:
raise Exception("The Images2Neibs grad is not implemented."
" It was in the past, but returned the wrong"
" answer!")
# This is the reverse of the op, not the grad!
return [neibs2images(gz, neib_shape, x.shape, mode=self.mode),
None, None]
else:
raise NotImplementedError()
......
......@@ -361,56 +361,8 @@ def speed_neibs_wrap_centered():
for i in range(1000):
f()
def test_neibs_grad():
shape = (2, 3, 4, 4)
images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape))
cost = T.sum(T.sqr(images2neibs(images, (2, 2))), axis=[0, 1])
grad = T.grad(cost, images)
f = theano.function([], [cost, grad], mode=mode_without_gpu)
got = f()
should_get = [numpy.asarray(290320.0, dtype=numpy.float32),
numpy.asarray([[[[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[ 16., 18., 20., 22.],
[ 24., 26., 28., 30.]],
[[ 32., 34., 36., 38.],
[ 40., 42., 44., 46.],
[ 48., 50., 52., 54.],
[ 56., 58., 60., 62.]],
[[ 64., 66., 68., 70.],
[ 72., 74., 76., 78.],
[ 80., 82., 84., 86.],
[ 88., 90., 92., 94.]]],
[[[ 96., 98., 100., 102.],
[104., 106., 108., 110.],
[112., 114., 116., 118.],
[120., 122., 124., 126.]],
[[128., 130., 132., 134.],
[136., 138., 140., 142.],
[144., 146., 148., 150.],
[152., 154., 156., 158.]],
[[160., 162., 164., 166.],
[168., 170., 172., 174.],
[176., 178., 180., 182.],
[184., 186., 188., 190.]]]], dtype=numpy.float32)]
assert numpy.allclose(got[0], should_get[0])
assert numpy.allclose(got[1], should_get[1])
def test_neibs_grad_verify_grad():
# Disable the test as the grad is wronly implemented
def tes_neibs_grad_verify_grad():
shape = (2, 3, 4, 4)
images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape),
......@@ -458,7 +410,8 @@ def test_neibs_ignore_border():
return T.sum(T.sqr(images2neibs(images, (2, 2),
mode='ignore_borders')), axis=[0, 1])
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
# Disable the test as the grad is wronly implemented
#unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
# not implemented for gpu
# if cuda.cuda_available:
......@@ -475,15 +428,18 @@ def test_neibs_valid_with_inconsistent_borders():
return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
axis=[0, 1])
f = theano.function([images],
T.sqr(images2neibs(images, (2, 2), mode='valid')))
try:
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
f(images_val)
assert False, "An error was expected"
except TypeError:
except TypeError, e:
# This is expected if the assert is there
pass
def test_neibs2images_crash_on_grad():
# Disable the test as the grad is wronly 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()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论