Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `tf.strided_slice()` do?

Tags:

I am wondering what tf.strided_slice() operator actually does.
The doc says,

To a first order, this operation extracts a slice of size end - begin from a tensor input starting at the location specified by begin. The slice continues by adding stride to the begin index until all dimensions are not less than end. Note that components of stride can be negative, which causes a reverse slice.

And in the sample,

# 'input' is [[[1, 1, 1], [2, 2, 2]], #             [[3, 3, 3], [4, 4, 4]], #             [[5, 5, 5], [6, 6, 6]]] tf.slice(input, [1, 0, 0], [2, 1, 3], [1, 1, 1]) ==> [[[3, 3, 3]]] tf.slice(input, [1, 0, 0], [2, 2, 3], [1, 1, 1]) ==> [[[3, 3, 3],                                                        [4, 4, 4]]] tf.slice(input, [1, 1, 0], [2, -1, 3], [1, -1, 1]) ==>[[[4, 4, 4],                                                         [3, 3, 3]]] 

So in my understanding of the doc, the first sample (tf.slice(input, begin=[1, 0, 0], end=[2, 1, 3], strides=[1, 1, 1])),

  • resulting size is end - begin = [1, 1, 3]. The sample result shows [[[3, 3, 3,]]], that shape is [1, 1, 3], it seems OK.
  • the first element of the result is at begin = [1, 0, 0]. The first element of the sample result is 3, which is input[1,0,0], it seems OK.
  • the slice continues by adding stride to the begin index. So the second element of the result should be input[begin + strides] = input[2, 1, 1] = 6, but the sample shows the second element is 3.

What strided_slice() does?

(Note: method names in the samples and the last example is incorrect.)

like image 897
keisuke Avatar asked Dec 29 '16 12:12

keisuke


People also ask

What is Strided slice?

tf.strided_slice() is used to do numpy style slicing of a tensor variable. It has 4 parameters in general: input, begin, end, strides.The slice continues by adding stride to the begin index until all dimensions are not less than the end.

What is TF stack?

tf.stack( values, axis=0, name='stack' ) Defined in tensorflow/python/ops/array_ops.py. Stacks a list of rank- R tensors into one rank- (R+1) Packs the list of tensors in values into a tensor with rank one higher than each tensor in values , by packing them along the dimension.


1 Answers

I experimented a bit with this method, which gave me some insights, which I think might be of some use. let's say we have a tensor.

a = np.array([[[1, 1.2, 1.3], [2, 2.2, 2.3], [7, 7.2, 7.3]],               [[3, 3.2, 3.3], [4, 4.2, 4.3], [8, 8.2, 8.3]],               [[5, 5.2, 5.3], [6, 6.2, 6.3], [9, 9.2, 9.3]]])  # a.shape = (3, 3, 3) 

strided_slice() requires 4 required arguments input_, begin, end, strides in which we are giving our a as input_ argument. As the case with tf.slice() method, the begin argument is zero-based and rest of args shape-based. However in the docs begin and end both are zero-based.

The functionality of method is quite simple:
It works like iterating over a loop, where begin is the location of element in the tensor from where the loop initiates and end is where it stops.

tf.strided_slice(a, [0, 0, 0], [3, 3, 3], [1, 1, 1])  # output =  the tensor itself  tf.strided_slice(a, [0, 0, 0], [3, 3, 3], [2, 2, 2])  # output = [[[ 1.   1.3] #            [ 7.   7.3]] #           [[ 5.   5.3] #            [ 9.   9.3]]] 

strides are like steps over which the loop iterates, here the [2,2,2] makes method to produce values starting at (0,0,0), (0,0,2), (0,2,0), (0,2,2), (2,0,0), (2,0,2) ..... in the a tensor.

tf.strided_slice(input3, [1, 1, 0], [2, -1, 3], [1, 1, 1])  

will produce output similar to tf.strided_slice(input3, [1, 1, 0], [2, 2, 3], [1, 1, 1]) as the tensora has shape = (3,3,3).

like image 191
Deepank Verma Avatar answered Oct 22 '22 14:10

Deepank Verma