R.joe
R.joe

Reputation: 49

How to slice some specific value in tensorflow by fixing step just like in numpy arrays?

In the following code I want to slice [[[3,30]]]. Can I perform numpy like slicing in tensorflow?

graph = tf.Graph()
with graph.as_default():
    t = tf.constant([[[1, 1, 1], [2, 2, 2]],[[3, 31, 30], [4, 40, 4]],[[5, 5, 5], [6, 6, 6]]])
    s =tf.slice(t, begin=[1,0,0],size=[1,1,3])
    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)
    print(t.shape)
    print(sess.run(s))

Upvotes: 0

Views: 30

Answers (1)

Andreas
Andreas

Reputation: 2521

You can modified it slightly using tf.strided_slice() instead of tf.slice()

s = tf.strided_slice(t, begin=[1,0,0],end=[2,1,3],strides=[1,1,2])

The end is the value of size parameter + begin parameter. While strides describes whether to skip any element in every iteration while slicing (1 means not skipping anything, 2 means skip 1 element, etc). Use 1, 1, 2 to skip 1 element in every iteration, which will skip the 2th, 4th, etc. This will give you [[[3, 30]]] instead of [[[3, 31, 30]]]

Upvotes: 1

Related Questions