Does create a constant tensor?



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))


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.



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.

Answered By – noober

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More