def test_pooling_with_tensor_vars(self): x = ftensor4() window_size = ivector() stride = ivector() padding = ivector() data = np.random.normal(0, 1, (1, 1, 5, 5)).astype("float32") # checking variable params vs fixed params for ignore_border in [True, False]: for mode in ["max", "sum", "average_inc_pad", "average_exc_pad"]: y = pool_2d(x, window_size, ignore_border, stride, padding, mode) dx = aesara.gradient.grad(y.sum(), x) var_fct = aesara.function([x, window_size, stride, padding], [y, dx]) for ws in (4, 2, 5): for st in (2, 3): for pad in (0, 1): if (pad > st or st > ws or (pad != 0 and not ignore_border) or (mode == "average_exc_pad" and pad != 0)): continue y = pool_2d(x, (ws, ws), ignore_border, (st, st), (pad, pad), mode) dx = aesara.gradient.grad(y.sum(), x) fix_fct = aesara.function([x], [y, dx]) var_y, var_dx = var_fct(data, (ws, ws), (st, st), (pad, pad)) fix_y, fix_dx = fix_fct(data) utt.assert_allclose(var_y, fix_y) utt.assert_allclose(var_dx, fix_dx)
def test_max_pool_2d_2D(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 2)) imval = rng.rand(4, 5) images = dmatrix() 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 numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val) def mp(input): return pool_2d(input, maxpoolshp, ignore_border, mode=mode) utt.verify_grad(mp, [imval], rng=rng)
def test_DownsampleFactorMax_hessian(self): # Example provided by Frans Cronje, see # https://groups.google.com/d/msg/theano-users/qpqUy_3glhw/JMwIvlN5wX4J x_vec = vector("x") z = aet.dot(x_vec.dimshuffle(0, "x"), x_vec.dimshuffle("x", 0)) y = pool_2d(input=z, ws=(2, 2), ignore_border=True) C = aet.exp(aet_sum(y)) grad_hess = aesara.gradient.hessian(cost=C, wrt=x_vec) fn_hess = function(inputs=[x_vec], outputs=grad_hess) # The value has been manually computed from the theoretical gradient, # and confirmed by the implementation. assert np.allclose(fn_hess([1, 2]), [[0.0, 0.0], [0.0, 982.7667]])
def test_max_pool_2d_3D(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = [(1, 2)] imval = rng.rand(2, 3, 4) images = dtensor3() 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 numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMax(self): rng = np.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, 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 = aesara.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(np.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def mp(input): return pool_2d(input, maxpoolshp, ignore_border, mode=mode)