Issue in Tensor slicing operation

58 views Asked by At

I'm trying to get expected_out from input.

input = [[2],[3],[3]]
expected_out = [2,3,3]

How to get the expected_out from input using TensorFlow.

2

There are 2 answers

0
martianwars On BEST ANSWER

In this case you want to remove single dimensional entries from a matrix. In both TensorFlow and Numpy, this operation is termed as squeeze.

Here is the official documentation for TensorFlow - tf.squeeze. Quoting from the documentation,

Given a tensor input, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don't want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying axis

Hence to solve your issue, you can either pass None to axis, your default case, or pass 1. Here is what the code will look like,

expected_out = tf.squeeze(input)

or,

expected_out = tf.squeeze(input, 1)
0
rvinas On

Use tf.squeeze:

import tensorflow as tf

input = tf.constant([[2], [3], [3]])

with tf.Session() as sess:
    print(sess.run(tf.squeeze(input)))