def test_tile_grad():
def grad_tile(x, reps, np_x):
y = tile(x, reps)
z = y.sum()
g = theano.function([x], grad(z, x))
grad_res = g(np_x)
# The gradient should be the product of the tiling dimensions
# (since the gradients are additive through the tiling operation)
assert numpy.all(grad_res == numpy.prod(reps))
rng = numpy.random.RandomState(utt.fetch_seed())
# test vector
grad_tile(vector('x'), [3], rng.randn(5).astype(config.floatX))
# test matrix
grad_tile(matrix('x'), [3, 4], rng.randn(2, 3).astype(config.floatX))
# test tensor3
grad_tile(tensor3('x'), [3, 4, 5],
rng.randn(2, 4, 3).astype(config.floatX))
# test tensor4
grad_tile(tensor4('x'), [3, 4, 5, 6],
rng.randn(2, 4, 3, 5).astype(config.floatX))
评论列表
文章目录