Rachel Jennifer
Rachel Jennifer

Reputation: 503

How do I use strided_slice to select all the element in tensorflow?

I read the examples in document:

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

It seems like that I can not simply use input[:,:] to select all the element, instead I have to use the syntax like input[:-1, :-1]. However in this way input[:-1, :-1] , I will miss the last row or last column. What should I do?

I take an example:

ph = tf.placeholder(shape=[None, 3], dtype=tf.int32)
x = tf.strided_slice(ph, [0,0],[-1,-1],[1,1])
input_ = np.array([[1,2,3],
                  [3,4,5],
                  [7,8,9]])
sess = tf.InteractiveSession()
sess.run(x,feed_dict={ph:input_})

output:

array([[1, 2],
       [3, 4]])

I read a lot of material and I found that I can use tf.shape(ph),let see:

ph = tf.placeholder(shape=[None, 3], dtype=tf.int32)
x = tf.strided_slice(ph, [0,0],tf.shape(ph),[1,1])
input_ = np.array([[1,2,3],
                  [3,4,5],
                  [7,8,9]])
sess = tf.InteractiveSession()
sess.run(x,feed_dict={ph:input_})

out:

array([[1, 2, 3],
       [3, 4, 5],
       [7, 8, 9]])

However, if I want to get the result like this:

[[1, 2],
 [3, 4],
 [7, 8]]

What can I do?

Upvotes: 2

Views: 869

Answers (2)

Arsene Lupin
Arsene Lupin

Reputation: 343

The following would work as well:

ph = tf.placeholder(shape=[None, 3], dtype=tf.int32)
x = tf.strided_slice(ph, [0,0],[tf.shape(ph)[0],-1],[1,1])
input_ = np.array([[1,2,3],
                  [3,4,5],
                  [7,8,9]])
sess = tf.InteractiveSession()
sess.run(x,feed_dict={ph:input_})

ipython notebook screenshot at https://i.sstatic.net/Eilzi.jpg

Upvotes: 0

Peter Hawkins
Peter Hawkins

Reputation: 3211

I having trouble understanding your question, but here's my attempt at answering it:

You can use the x[:, :, :] syntax to select all elements of an array:

sess = tf.Session()
inp = tf.constant([[[1, 1, 1], [2, 2, 2]],
                   [[3, 3, 3], [4, 4, 4]],
                   [[5, 5, 5], [6, 6, 6]]])
print(inp.shape)

x = inp[:, :, :]
print(sess.run(x))

To get the last output you wanted, it's certainly possible with some manual dimension calculations:

sess = tf.Session()
x = tf.constant([[1,2,3],
                 [3,4,5],
                 [7,8,9]])
y = tf.shape(x)
bounds = tf.concat([y[:-1], [-1]], axis=0)
out = tf.strided_slice(x, [0,0], bounds, [1,1])
print(sess.run(out))

In general the Tensorflow slicing syntax follows numpy's slicing syntax, which is documented here: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

Hope that helps!

Upvotes: 2

Related Questions