提交 2be47437 authored 作者: Gijs van Tulder's avatar Gijs van Tulder

Add tests for DownsampleFactorMaxGrad average+sum

上级 a38a44a8
...@@ -316,20 +316,24 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -316,20 +316,24 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
maxpoolsizes = ((5, 3), (3, 5), (3, 3)) maxpoolsizes = ((5, 3), (3, 5), (3, 3))
stridesizes = ((3, 2), (2, 3), (3, 3)) stridesizes = ((3, 2), (2, 3), (3, 3))
paddingsizes = ((2, 2), (2, 1), (2, 2)) paddingsizes = ((2, 2), (2, 1), (2, 2))
for i in range(len(imgsizes)): # average_inc_pad and average_exc_pad do not
imgsize = imgsizes[i] # support grad with padding
imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 for mode in ['max', 'sum']:
maxpoolsize = maxpoolsizes[i] for i in range(len(imgsizes)):
stridesize = stridesizes[i] imgsize = imgsizes[i]
paddingsize = paddingsizes[i] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0
maxpoolsize = maxpoolsizes[i]
stridesize = stridesizes[i]
paddingsize = paddingsizes[i]
def mp(input): def mp(input):
return DownsampleFactorMax( return DownsampleFactorMax(
maxpoolsize, ignore_border=True, maxpoolsize, ignore_border=True,
st=stridesize, st=stridesize,
padding=paddingsize, padding=paddingsize,
)(input) mode=mode,
utt.verify_grad(mp, [imval], rng=rng) )(input)
utt.verify_grad(mp, [imval], rng=rng)
def test_DownsampleFactorMax_grad(self): def test_DownsampleFactorMax_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
...@@ -337,14 +341,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -337,14 +341,17 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
imval = rng.rand(2, 3, 3, 4) * 10.0 imval = rng.rand(2, 3, 3, 4) * 10.0
# more variance means numeric gradient will be more accurate # more variance means numeric gradient will be more accurate
for maxpoolshp in maxpoolshps: for maxpoolshp, ignore_border, mode in product(maxpoolshps,
for ignore_border in [True, False]: [True, False],
# print 'maxpoolshp =', maxpoolshp ['max',
# print 'ignore_border =', ignore_border 'sum',
def mp(input): 'average_inc_pad',
return DownsampleFactorMax(maxpoolshp, 'average_exc_pad']):
ignore_border=ignore_border)(input) def mp(input):
utt.verify_grad(mp, [imval], rng=rng) return DownsampleFactorMax(maxpoolshp,
ignore_border=ignore_border,
mode=mode)(input)
utt.verify_grad(mp, [imval], rng=rng)
def test_DownsampleFactorMax_grad_st(self): def test_DownsampleFactorMax_grad_st(self):
"""checks the gradient for the case that stride is used""" """checks the gradient for the case that stride is used"""
...@@ -353,14 +360,18 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -353,14 +360,18 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
stridesizes = ((1, 1), (3, 3), (5, 7)) stridesizes = ((1, 1), (3, 3), (5, 7))
imval = rng.rand(1, 2, 16, 16) imval = rng.rand(1, 2, 16, 16)
for maxpoolshp in maxpoolshps: for maxpoolshp, ignore_border, mode, stride in product(maxpoolshps,
for ignore_border in [True, False]: [True, False],
for stride in stridesizes: ['max',
def mp(input): 'sum',
return DownsampleFactorMax(maxpoolshp, 'average_inc_pad',
ignore_border=ignore_border, 'average_exc_pad'],
st=stride)(input) stridesizes):
utt.verify_grad(mp, [imval], rng=rng) def mp(input):
return DownsampleFactorMax(maxpoolshp,
ignore_border=ignore_border,
st=stride, mode=mode)(input)
utt.verify_grad(mp, [imval], rng=rng)
def test_DownsampleFactorMax_grad_st_extra(self): def test_DownsampleFactorMax_grad_st_extra(self):
"""checks the gradient for the case """checks the gradient for the case
...@@ -372,17 +383,19 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -372,17 +383,19 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5),
(8, 5), (8, 5), (8, 5)) (8, 5), (8, 5), (8, 5))
for indx in numpy.arange(len(maxpoolshps)): for mode in ['max', 'sum', 'average_inc_pad', 'average_exc_pad']:
imvsize = imvsizs[indx] for indx in numpy.arange(len(maxpoolshps)):
imval = rng.rand(1, 2, imvsize[0], imvsize[1]) imvsize = imvsizs[indx]
stride = stridesizes[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1])
maxpoolshp = maxpoolshps[indx] stride = stridesizes[indx]
for ignore_border in [True, False]: maxpoolshp = maxpoolshps[indx]
def mp(input): for ignore_border in [True, False]:
return DownsampleFactorMax(maxpoolshp, def mp(input):
ignore_border=ignore_border, return DownsampleFactorMax(maxpoolshp,
st=stride)(input) ignore_border=ignore_border,
utt.verify_grad(mp, [imval], rng=rng) st=stride,
mode=mode)(input)
utt.verify_grad(mp, [imval], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): def test_DownsampleFactorMaxGrad_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论