1

trying to comprehend tensorflow strided_slice and slice

x = tf.constant(np.array(   [[[111, 112, 113], [121, 122, 123]],
                            [[211, 212, 213], [221, 222, 223]],
                            [[311, 312, 313], [321, 322, 323]]]))
with tf.Session() as sess:
    print("tf.shape ------------------")
    print(sess.run(tf.shape(x)))
    print("tf.slice ------------------------")
    print(sess.run((tf.slice(x, [1, 0, 0], [2, 1, 3]) )))
    print("tf.strided_slice ------------------------")
    print(sess.run(tf.strided_slice(x, [1, 0, 0], [2, 1, 3], [1, 1, 1])))
    print(sess.run(tf.strided_slice(x, [1, -1, 0], [2, -3, 3], [1, -1, 1])))
    print(sess.run(x[1,-1,0]))
    print(sess.run(x[2,-3,3]))

output

tf.shape ------------------
[3 2 3]
tf.slice ------------------------
[[[211 212 213]]

 [[311 312 313]]]
tf.strided_slice ------------------------
[[[211 212 213]]]
[[[221 222 223]
  [211 212 213]]]
221
ValueError: slice index -1 of dimension 1 out of bounds. for 'strided_slice_8' (op: 'StridedSlice') with input shapes: [3,2,3], [3], [3], [3] and with computed input tensors: input[1] = <2 -3 3>, input[2] = <3 -2 4>, input[3] = <1 1 1>.

for tf.slice i understand we have to mentions slice sizes in each dimension and hence out of range values makes sense. but in strided slice the end is a tensor index in the tensor itself, how come out of size value is valid.

Example is taken from https://www.tensorflow.org/api_docs/python/tf/strided_slice

Trying to implement folding layer part from paper A Convolutional Neural Network for Modelling Sentences

In the formulation of the network so far, feature detectors applied to an individual row of the sentence matrix s can have many orders and create complex dependencies across the same rows in multiple feature maps. Feature detectors in different rows, however, are independent of each other until the top fully connected layer. Full dependence between different rows could be achieved by making M in Eq. 5 a full matrix instead of a sparse matrix of diagonals. Here we explore a simpler method called folding that does not introduce any additional parameters. After a convolutional layer and before (dynamic) k-max pooling, one just sums every two rows in a feature map component-wise. For a map of d rows, folding returns a map of d/2 rows, thus halving the size of the representation. With a folding layer, a feature detector of the i-th order depends now on two rows of feature values in the lower maps of order i − 1. This ends the description of the DCNN.

bicepjai
  • 1,615
  • 3
  • 17
  • 35
  • used tf.split and tf.reduce_sum to achieve the sum of 2 rows, still my question about tf.strided_slice is valid. – bicepjai Aug 26 '17 at 23:16

1 Answers1

1

The ValueError produced doesn't have anything to do with the first 2 calls to Strided_Slice. Your use of the Strided_Slice operation is correct in

print(sess.run(tf.strided_slice(x, [1, 0, 0], [2, 1, 3], [1, 1, 1])))

print(sess.run(tf.strided_slice(x, [1, -1, 0], [2, -3, 3], [1, -1, 1])))

Your issue is with the call to

print(sess.run(x[2,-3,3]))

A negative index in a python array iterates in reverse through the array.

For example, given an array like this

arr = ['a', 'b', 'c', 'd', 'e', 'f']

A call to arr[-1] would yield 'f'. Similarly, a call to arr[-4] would yield 'c'. What would happen if we tried calling arr[-7]? This would be trying to access the index -1, which would throw an error.

Keep in mind that arrays in Python have 0-based indexing. Your call to x[2,-3, 3] initially accesses the element at index 2 (3rd element) in the outer array, which is

[[311, 312, 313], [321, 322, 323]]

Now, in this outer array, there are 2 elements. However, your call x[2, -3, 3] attempts to access the element at index -1 as it iterates from the end of the array. This is what yields the error

slice index -1 of dimension 1 out of bounds

NOTE: The last index you try to access in x[2, -3, 3] will also yield a ValueError because it is attempting to access an index not in the array. To fix this, your call could be x[2, -2, 2].

Here are a few links about Strided Slicing, Slicing, and array indexing in Python: https://www.tensorflow.org/api_docs/python/tf/strided_slice

https://www.tensorflow.org/api_docs/python/tf/slice

Negative list index?

Community
  • 1
  • 1
arvindvs
  • 11
  • 1