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.