def test_DownsampleFactorMaxPaddingStride_grad_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] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 maxpoolsize = maxpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] grad_shape = Pool.out_shape(imval.shape, maxpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, )(input) grad_op = MaxPoolGrad(maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad_st(self, example, ignore_border): # checks the gradient of the gradient for # the case that stride is used rng = numpy.random.RandomState(utt.fetch_seed()) (maxpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border, st=stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) imgsizes = ((10, 10), (10, 5), (5, 5)) avgpoolsizes = ((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] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 avgpoolsize = avgpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] # 'average_exc_pad' with non-zero padding is not implemented for mode in ['sum', 'average_inc_pad']: grad_shape = Pool.out_shape(imval.shape, avgpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(avgpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def get_dim(self, name): if name == 'input_': return self.input_dim if name == 'output': return tuple(Pool.out_shape( self.input_dim, self.pooling_size, st=self.step, ignore_border=self.ignore_border, padding=self.padding))
def test_DownsampleFactorMaxGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: for stride in stridesizes: grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool( maxpoolshp, ignore_border=ignore_border, st=stride)(input) grad_op = MaxPoolGrad( maxpoolshp, ignore_border=ignore_border, st=stride) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: for stride in stridesizes: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def get_dim(self, name): if name == 'input_': return self.input_dim if name == 'output': return tuple(Pool.out_shape( self.input_dim, self.pooling_size, st=self.step, ignore_border=self.ignore_border, padding=self.padding))
def test_AveragePoolGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(avgpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] avgpoolshp = avgpoolshps[indx] for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"] ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([images], [output]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: for stride in stridesizes: grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool( maxpoolshp, ignore_border=ignore_border, st=stride)(input) grad_op = MaxPoolGrad( maxpoolshp, ignore_border=ignore_border, st=stride) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border in [True, False]: grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxPaddingStride_grad_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] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 maxpoolsize = maxpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] grad_shape = Pool.out_shape(imval.shape, maxpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, )(input) grad_op = MaxPoolGrad(maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_stride(self, example, ignore_border, mode): # checks the gradient of the gradient for # the case that stride is used rng = np.random.RandomState(utt.fetch_seed()) (avgpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, stride=stride, ) # skip the grad verification when the output is empty if np.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border in [True, False]: grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) imgsizes = ((10, 10), (10, 5), (5, 5)) avgpoolsizes = ((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] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 avgpoolsize = avgpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] # 'average_exc_pad' with non-zero padding is not implemented for mode in ['sum', 'average_inc_pad']: grad_shape = Pool.out_shape(imval.shape, avgpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(avgpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # avgpool, stride, padding, input sizes examples = ( ((3,), (2,), (2,), (10,)), ((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 1, 10,)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (avgpoolshp, stridesize, paddingsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # 'average_exc_pad' with non-zero padding is not implemented for mode in ['sum', 'average_inc_pad']: grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ndim=len(avgpoolshp), st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=True, mode=mode) return grad_op(input, grad, avgpoolshp, stridesize, paddingsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(avgpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] avgpoolshp = avgpoolshps[indx] for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input size examples = ( ((2,), (16,)), ((2,), (4, 16,)), ((2,), (4, 2, 16,)), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border, mode in product(examples, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, inputsize) = example imval = rng.rand(*inputsize) images = theano.shared(imval) # Pure Numpy computation numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) # The pool_2d or pool_3d helper methods if len(maxpoolshp) == 2: output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) elif len(maxpoolshp) == 3: output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxPaddingStride_grad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # maxpool, stride, pad, input sizes examples = ( ((3, ), (2, ), (2, ), (10, )), ( (3, ), (2, ), (2, ), ( 2, 10, ), ), ( (3, ), (2, ), (2, ), ( 2, 1, 10, ), ), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (maxpoolshp, stridesize, padsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), stride=stridesize, ignore_border=True, pad=padsize, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=True, )(input, maxpoolshp, stridesize, padsize) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=True) return grad_op(input, out, grad, maxpoolshp, stridesize, padsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # avgpool, stride, pad, input sizes examples = ( ((3, ), (2, ), (2, ), (10, )), ( (3, ), (2, ), (2, ), ( 2, 10, ), ), ( (3, ), (2, ), (2, ), ( 2, 1, 10, ), ), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((5, 3, 2), (3, 2, 1), (2, 2, 2), (1, 1, 10, 5, 5)), ) for (avgpoolshp, stridesize, padsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # 'average_exc_pad' with non-zero padding is not implemented for mode in ["sum", "average_inc_pad"]: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), stride=stridesize, ignore_border=True, pad=padsize, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=True, mode=mode) return grad_op(input, grad, avgpoolshp, stridesize, padsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(maxpoolshp, ignore_border=ignore_border)(input) grad_op = MaxPoolGrad(maxpoolshp, ignore_border=ignore_border) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(avgpoolshp, ignore_border=ignore_border, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: for stride in stridesizes: grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad(avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([ images, ], [ output, ]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # maxpool, input sizes examples = ( ((2, ), (2, )), ((2, ), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (maxpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_st(self, example, ignore_border, mode): # checks the gradient of the gradient for # the case that stride is used rng = numpy.random.RandomState(utt.fetch_seed()) (avgpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, st=stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input sizes examples = ( ((2,), (2,)), ((2,), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (maxpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad( ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, stride, padding, input sizes examples = ( ((3,), (2,), (2,), (10,)), ((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 1, 10,)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (maxpoolshp, stridesize, paddingsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=True, )(input, maxpoolshp, stridesize, paddingsize) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=True) return grad_op(input, out, grad, maxpoolshp, stridesize, paddingsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # avgpool, input sizes examples = ( ((2, ), (2, )), ((2, ), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ) for (avgpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # avgpool, input sizes examples = ( ((2,), (2,)), ((2,), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (avgpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad( ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input size examples = ( ((2, ), (16, )), ((2, ), ( 4, 16, )), ((2, ), ( 4, 2, 16, )), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border, mode in product( examples, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, inputsize) = example imval = rng.rand(*inputsize) images = theano.shared(imval) # Pure Numpy computation numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) # The pool_2d or pool_3d helper methods if len(maxpoolshp) == 2: output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [ output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) elif len(maxpoolshp) == 3: output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [ output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def infer_shape(self, node, in_shapes): ws, stride, pad = [node.inputs[1], node.inputs[2], node.inputs[3]] shp = Pool.out_shape(in_shapes[0], ws, self.ignore_border, stride, pad, self.ndim) return [shp]
def test_out_shape(self): assert Pool.out_shape((9, 8, 6), (2, 2)) == [9, 4, 3] assert Pool.out_shape((8, 6), (2, 2)) == [4, 3]
def maxpool_2d(z, in_dim, poolsize, poolstride): z = pool_2d(z, ds=poolsize, st=poolstride) output_size = tuple(Pool.out_shape(in_dim, poolsize, st=poolstride)) return z, output_size
def maxpool_2d(z, in_dim, poolsize, poolstride): z = pool_2d(z, ds=poolsize, st=poolstride) output_size = tuple(Pool.out_shape(in_dim, poolsize, st=poolstride)) return z, output_size
def test_out_shape(self): assert Pool.out_shape((9, 8, 6), (2, 2)) == [9, 4, 3] assert Pool.out_shape((8, 6), (2, 2)) == [4, 3]
def infer_shape(self, node, in_shapes): ws, stride, pad = [node.inputs[1], node.inputs[2], node.inputs[3]] shp = Pool.out_shape(in_shapes[0], ws, self.ignore_border, stride, pad, self.ndim) return [shp]