What does a tensor t[..., 1, tf.newaxis] ...stands for?

Viewed 252

I am beginning to work with Python and TensorFlow machine learning.

I am working through an example where I create a simple tensor representing a matrix with two rows and three columns of floats:

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])

The tutorial then provides the following code:

t[..., 1, tf.newaxis]

I am wondering what the ellipsis ... stands for?

I cannot find any reference to this syntax in the Tensorflow API.

1 Answers

"..." means "all dimensions prior to" the specified dimension.

So, in this example:

t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
t.shape
# OP: TensorShape([2, 3])

Now add newaxis

t[..., 1, tf.newaxis]
# OP: <tf.Tensor: shape=(2, 1), dtype=float32, numpy=
# array([[2.],
#       [5.]], dtype=float32)>
t[..., 1, tf.newaxis].shape
# OP: TensorShape([2, 1])

Without the newaxis, it basically selects all the rows (...) and the first column. The shape would be (2,). Now, with the newaxis, the shape is (2,1).

Related