提交 1b6a7f6b authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #4842 from ChihebTrabelsi/ccw3

Addition of DownsampleFactorMaxGradGrad.grad and its test
...@@ -12,7 +12,6 @@ import numpy ...@@ -12,7 +12,6 @@ import numpy
from six import integer_types from six import integer_types
from six.moves import xrange from six.moves import xrange
import six.moves.builtins as builtins import six.moves.builtins as builtins
import theano import theano
from theano import gof, OpenMPOp, tensor, Variable, Apply from theano import gof, OpenMPOp, tensor, Variable, Apply
...@@ -995,6 +994,15 @@ class DownsampleFactorMaxGradGrad(OpenMPOp): ...@@ -995,6 +994,15 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
return [in_shapes[1]] return [in_shapes[1]]
def grad(self, inp, grads):
x, maxout, ggx = inp
gz, = grads
return [theano.tensor.zeros_like(x),
theano.tensor.zeros_like(maxout),
MaxPoolGrad(
self.ds, ignore_border=self.ignore_border,
st=self.st, padding=self.padding)(x, maxout, gz)]
def c_code(self, node, name, inp, out, sub): def c_code(self, node, name, inp, out, sub):
if self.mode != 'max': if self.mode != 'max':
raise theano.gof.utils.MethodNotDefined() raise theano.gof.utils.MethodNotDefined()
......
...@@ -11,7 +11,8 @@ import theano.tensor as tensor ...@@ -11,7 +11,8 @@ import theano.tensor as tensor
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
from theano.tensor.signal.pool import (Pool, pool_2d, from theano.tensor.signal.pool import (Pool, pool_2d,
MaxPoolGrad, AveragePoolGrad, MaxPoolGrad, AveragePoolGrad,
max_pool_2d_same_size) max_pool_2d_same_size,
DownsampleFactorMaxGradGrad)
from theano.tensor.signal.downsample import DownsampleFactorMaxGrad from theano.tensor.signal.downsample import DownsampleFactorMaxGrad
...@@ -652,6 +653,35 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -652,6 +653,35 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
assert numpy.allclose(fn_hess([1, 2]), [[0., 0.], [0., 982.7667]]) assert numpy.allclose(fn_hess([1, 2]), [[0., 0.], [0., 982.7667]])
def test_DownsampleFactorMaxGradGrad_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed())
imgsizes = ((10, 10), (10, 5), (5, 5))
maxpoolsizes = ((5, 3), (3, 5), (3, 3))
stridesizes = ((3, 2), (2, 3), (3, 3))
paddingsizes = ((2, 2), (2, 1), (2, 2))
for i in range(len(imgsizes)):
imgsize = imgsizes[i]
imval1 = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0
imval2 = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0
maxpoolsize = maxpoolsizes[i]
stridesize = stridesizes[i]
paddingsize = paddingsizes[i]
def mp(input1, input2):
pooled_out = Pool(
maxpoolsize, ignore_border=True,
st=stridesize,
padding=paddingsize,
)(input1)
out = DownsampleFactorMaxGradGrad(
ds=maxpoolsize,
ignore_border=True,
st=stridesize,
padding=paddingsize)(input1, pooled_out, input2)
return out
utt.verify_grad(mp, [imval1, imval2], rng=rng)
def test_max_pool_2d_2D(self): def test_max_pool_2d_2D(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
maxpoolshps = ((1, 1), (3, 2)) maxpoolshps = ((1, 1), (3, 2))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论