def grad_conv_T4_TH3(A, B): out_dim = A.shape[2] - B.shape[2] + 1 R = np.zeros((B.shape[1], out_dim, out_dim)) for i in range(A.shape[0]): for f in range(B.shape[1]): R[f] = R[f] + convolveTH23(A[i], rot(B[i, f])) return R from theano.sandbox.linalg import kron m1 = T.tensor4("kron_input_1") m2 = T.tensor4("kron_input_2") kron_out = kron(m1, m2) kron_fun = theano.function([m1, m2], kron_out) def upsample_TH(A, pool_dim): B = np.ones((pool_dim, pool_dim)) R = np.zeros( (A.shape[0], A.shape[1], A.shape[2] * pool_dim, A.shape[3] * pool_dim)) for i in range(R.shape[0]): for j in range(R.shape[1]): R[i, j] = kron_fun(A[i, j], B) R = R / (pool_dim * pool_dim) return R def upsample_NP(A, pool_dim):
""" Ashape = A.shape if (len(A.shape) < 4): A.shape = (1, Ashape[0], Ashape[1], Ashape[2]) B = B.swapaxes(0, 1) R = convolve_T4(A, B) A.shape = Ashape return R from theano.sandbox.linalg import kron upsample_pd = T.scalar("upsample_pool_dim", dtype=floatX1) upsample_inp = T.vector("upsample_input", dtype=floatX1) upsample_f = T.matrix("upsample_filter", dtype=floatX1) kron_out = kron(upsample_inp, upsample_f) * (1.0 / (upsample_pd**2)) kron_fun = theano.function([upsample_inp, upsample_f, upsample_pd], kron_out, name='kron_fun') def upsample_T4(A, pd): """Perform the reverse of average pooling on a 4-dimensioanl tensor by replicating an element in A "pd" times in the last 2 dimensions and then averaging """ B = np.ones((1, pd * pd)) Ashape = A.shape A = A.reshape(A.size) l = Ashape[0] * Ashape[1] * Ashape[2] R = kron_fun(A.astype(floatX1), B.astype(floatX1), pd)
array while A is 3-dimensional. TBD - this needs a clearer description """ Ashape = A.shape if (len(A.shape) < 4): A.shape = (1, Ashape[0], Ashape[1], Ashape[2]) B = B.swapaxes(0, 1) R = convolve_T4(A, B) A.shape = Ashape return R from theano.sandbox.linalg import kron upsample_pd = T.scalar("upsample_pool_dim", dtype = floatX1) upsample_inp = T.vector("upsample_input", dtype = floatX1) upsample_f = T.matrix("upsample_filter", dtype = floatX1) kron_out = kron(upsample_inp, upsample_f) * (1.0/ (upsample_pd ** 2)) kron_fun = theano.function([upsample_inp, upsample_f, upsample_pd], kron_out, name = 'kron_fun') def upsample_T4(A, pd): """Perform the reverse of average pooling on a 4-dimensioanl tensor by replicating an element in A "pd" times in the last 2 dimensions and then averaging """ B = np.ones((1, pd*pd)) Ashape = A.shape A = A.reshape(A.size) l = Ashape[0] * Ashape[1] * Ashape[2] R = kron_fun(A.astype(floatX1), B.astype(floatX1), pd) R = R.reshape(l,Ashape[3],pd,pd).swapaxes(1, 2).reshape(Ashape[0], Ashape[1], Ashape[2] * pd, Ashape[3] * pd) A.shape = Ashape
for f in range(B.shape[1]): R[f] = R[f] + convolveTH2(A[i], rot(B[i, f])) return R def grad_conv_T4_TH3(A, B): out_dim = A.shape[2] - B.shape[2] + 1 R = np.zeros((B.shape[1], out_dim, out_dim)) for i in range(A.shape[0]): for f in range(B.shape[1]): R[f] = R[f] + convolveTH23(A[i], rot(B[i, f])) return R from theano.sandbox.linalg import kron m1 = T.tensor4("kron_input_1") m2 = T.tensor4("kron_input_2") kron_out = kron(m1, m2) kron_fun = theano.function([m1, m2], kron_out) def upsample_TH(A, pool_dim): B = np.ones((pool_dim, pool_dim)) R = np.zeros((A.shape[0], A.shape[1], A.shape[2] * pool_dim, A.shape[3] * pool_dim)) for i in range(R.shape[0]): for j in range(R.shape[1]): R[i, j] = kron_fun(A[i, j], B) R = R / (pool_dim * pool_dim) return R def upsample_NP(A, pool_dim): B = np.ones((pool_dim, pool_dim)) R = np.zeros((A.shape[0], A.shape[1], A.shape[2] * pool_dim,