Example #1
0
def test_tensor_array_strided_slice_grad_grad():
    shape = (3, 2, 2)
    from tensorflow.python.ops.gen_array_ops import strided_slice_grad
    x = tf.reshape(tf.range(0, numpy.prod(shape), dtype=tf.float32), shape)
    x_ = tf.TensorArray(dtype=tf.float32,
                        size=shape[0],
                        element_shape=shape[1:])
    for t in range(shape[0]):
        x_ = x_.write(index=t, value=x[t])
    x__ = x_.stack()
    y = strided_slice_grad(shape=tf.convert_to_tensor(shape),
                           begin=tf.constant([0]),
                           end=tf.constant([0], dtype=tf.int32),
                           end_mask=1,
                           strides=tf.constant([1]),
                           dy=x__)
    y.set_shape(x.get_shape())
    vx, vx__, vy = session.run([x, x__, y])
    print("vx, vx__, vy:", vx, vx__, vy)
    assert_almost_equal(vx, vx__)
    assert_almost_equal(vx, vy)
    dy = tf.reshape(
        tf.range(0, numpy.prod(shape), dtype=tf.float32) * 0.1 - 5.135, shape)
    dx, = tf.gradients(ys=[y], xs=[x], grad_ys=[dy])
    vdx, vdy = session.run([dx, dy])
    print("vdx, vdy:", vdx, vdy)
    assert_almost_equal(vdx, vdy)
Example #2
0
 def reverse_strided_slice(shape, out):
     #if start == 0 and step == 1:
     #  return out
     from tensorflow.python.ops.gen_array_ops import strided_slice_grad
     out = strided_slice_grad(shape=tf.convert_to_tensor(shape),
                              begin=tf.constant([start_]),
                              end=tf.constant([0], dtype=tf.int32),
                              end_mask=1,
                              strides=tf.constant([step]),
                              dy=out)
     assert isinstance(out, tf.Tensor)
     out.set_shape(tf.TensorShape(shape))
     return out
Example #3
0
def test_strided_slice_grad_grad():
    shape = (3, 2, 2)
    from tensorflow.python.ops.gen_array_ops import strided_slice_grad
    x = tf.reshape(tf.range(0, numpy.prod(shape), dtype=tf.float32), shape)
    y = strided_slice_grad(shape=tf.convert_to_tensor(shape),
                           begin=tf.constant([0]),
                           end=tf.constant([0], dtype=tf.int32),
                           end_mask=1,
                           strides=tf.constant([1]),
                           dy=x)
    y.set_shape(x.get_shape())
    vx, vy = session.run([x, y])
    print("vx, vy:", vx, vy)
    assert_almost_equal(vx, vy)
    dy = tf.reshape(
        tf.range(0, numpy.prod(shape), dtype=tf.float32) * 0.1 - 5.135, shape)
    dx, = tf.gradients(ys=[y], xs=[x], grad_ys=[dy])
    vdx, vdy = session.run([dx, dy])
    print("vdx, vdy:", vdx, vdy)
    assert_almost_equal(vdx, vdy)