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)
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)
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_infer_shape(self): image = tensor.dtensor4() maxout = tensor.dtensor4() gz = tensor.dtensor4() rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3), (3, 2)) image_val = rng.rand(4, 6, 7, 9) out_shapes = [[[[4, 6, 7, 9], [4, 6, 7, 9]], [[4, 6, 3, 4], [4, 6, 4, 5]], [[4, 6, 2, 3], [4, 6, 3, 3]], [[4, 6, 3, 3], [4, 6, 4, 3]], [[4, 6, 2, 4], [4, 6, 3, 5]]], [[None, None], [[4, 6, 4, 5], None], [[4, 6, 3, 3], None], [[4, 6, 4, 3], None], [[4, 6, 3, 5], None]], [[None, None], [None, None], [[4, 6, 3, 4], None], [[4, 6, 4, 4], None], [None, None]]] for i, maxpoolshp in enumerate(maxpoolshps): for j, ignore_border in enumerate([True, False]): for k, padding in enumerate([(0, 0), (1, 1), (1, 2)]): if out_shapes[k][i][j] is None: continue # checking shapes generated by Pool self._compile_and_check([image], [Pool(maxpoolshp, ignore_border=ignore_border, padding=padding)(image)], [image_val], Pool) # checking shapes generated by MaxPoolGrad maxout_val = rng.rand(*out_shapes[k][i][j]) gz_val = rng.rand(*out_shapes[k][i][j]) self._compile_and_check([image, maxout, gz], [MaxPoolGrad(maxpoolshp, ignore_border=ignore_border, padding=padding) (image, maxout, gz)], [image_val, maxout_val, gz_val], MaxPoolGrad, warn=False) # checking with broadcastable input image = tensor.tensor(dtype='float64', broadcastable=(False, False, True, True)) image_val = rng.rand(4, 6, 1, 1) self._compile_and_check( [image], [Pool((2, 2), ignore_border=True, padding=(0, 0))(image)], [image_val], Pool)
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)
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)
def mp(input, grad): out = Pool(ignore_border=True)(input, maxpoolsize, stridesize, paddingsize) grad_op = MaxPoolGrad(ignore_border=True) return grad_op(input, out, grad, maxpoolsize, stridesize, paddingsize)
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)
import theano import numpy as np import theano.tensor as T from theano.tensor.signal.pool import max_pool_2d_same_size, pool_2d, MaxPoolGrad input = T.tensor4() pool_out = pool_2d(input, ds=(2, 2), ignore_border=True, mode='max') method = 1 if method == 1: #indices = T.grad(None, wrt=input, known_grads={pool_out: T.ones_like(pool_out)}) indices = MaxPoolGrad((2, 2), True)(input, pool_out, T.ones_like(pool_out)) unpool = indices * pool_out.repeat(2, axis=2).repeat(2, axis=3) elif method == 2: indices, pool_out = pool_2d( input, ds=(2, 2), ignore_border=True, mode='max') #modifiy the code of max_pool_2d, to be completed elif method == 3: pool_same_size = max_pool_2d_same_size(input, (2, 2)) #indices = pool_same_size>0 #get 0/1 indicating non/argmax #unpool = T.set_subtensor(T.flatten(T.zeros_like(input))[T.flatten(indices)],T.flatten(pool_out)).reshape(input.shape) #indices = pool_same_size.nonzero() #get an array of the cordinates of argmax #unpool = T.set_subtensor(T.zeros_like(input)[indices],T.flatten(pool_out)) f_pool = theano.function([input], pool_out) f_pool_out_same_size = theano.function([input], pool_out_same_size) f_index = theano.function([input], indices) f_unpool = theano.function([input], unpool) a = np.arange(16).reshape(1, 1, 4, 4)