Home > Software engineering >  Does tf.one create a constant tensor?
Does tf.one create a constant tensor?

Time:11-12

I would like to understand the reason for using tf.constant command to create a tensor. Why does it contain “constant” word in it?

If we had 2 tensors:

x0 = tf.constant(np.random.randn(3,1))

and

x1 = tf.ones(3)

would there be any particular operation permitted for x1 but not for x0?

Would x1 be a constant tensor too?

I hope I am not making confusion with the way the question is asked but I am new to TensorFlow and would really like to understand the basics.

Henrikh

CodePudding user response:

Both are tf.Tensors(). The difference is that:

tf.ones() creates a tensor with all values equal to 1. For example, tf.ones((2, 2)) has as output:

<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>

By the other hand, tf.constant() creates a tensor with the values you want. For example, tf.constant([[1, 2], [3, 4]]) creates:

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

You can also change their types (with param dtype).

Both are equal in terms of doing same operations, because they are tensors. But how you use them and what for is up to your project!

Hope I've answered your question.

  • Related