How tf.cast makes a matrix?

Let me ask (possibly) a very basic question.
See the code below.

import tensorflow as tf

i = tf.range(3)[:, tf.newaxis]
j = tf.range(3)
tf.cast(i>=j, dtype="int32")

When I run this code, I get the below.
I mean a lower triangular 3x3 matrix(tensor).

<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 0, 0],
       [1, 1, 0],
       [1, 1, 1]], dtype=int32)>

What I want to know is

  1. How tf.newaxis works in i = tf.range(3)[:, tf.newaxis]
    As far as I understand, i is basicly a column vector but to make it a matrix, it makes a room for more columns.
    Is this right?

  2. In the code above, I didn’t make a 3x3 matrix anywhere but finally I get a 3x3 matrix.
    tf.cast is originally a function to convert a type from one to another, not a function to make a matrix.
    But actually I get a matrix.
    Why?

  1. tf.range(3) has shape (3, ).
    [:, tf.newaxis] adds a new dimension, hence you get a tensor of shape (3, 1) (notice the square brackets got doubled [] )

  2. The matrix of shape (3, 3) you get result from your use of logical operators (>=) over 2 tensors, one of shape (3, ) and the other one of shape (3, 1). TF gets through these 2 tensors element-wise.
    You can get rid of tf.cast() and see the following:

i>=j

<tf.Tensor: shape=(3, 3), dtype=bool, numpy=
array([[ True, False, False],
[ True, True, False],
[ True, True, True]])>

The resulting tensor is of shape (3, 3).

1 Like

I see, I see.
Thank you ,tagma!

1 Like