Reputation: 43
>>> tf.constant([1,2,3])
<tf.Tensor 'Const:0' shape=(3,) dtype=int32>
>>> tf.constant([[1,2,3]])
<tf.Tensor 'Const_1:0' shape=(1, 3) dtype=int32>
Is tf.constant([1,2,3])
creating a scalar and tf.constant([[1,2,3]])
creating an array?
Upvotes: 2
Views: 1307
Reputation: 29720
Not quite. tf.constant([1, 2, 3])
creates a rank 1 constant tensor (a vector). Thus the shape is (3,)
.
>>> sess = tf.InteractiveSession()
>>> tf.constant([1, 2, 3]).eval()
array([1, 2, 3], dtype=int32)
While tf.constant([[1, 2, 3]])
creates a rank 2 constant tensor (a matrix), with 1 row and 3 columns.. so its shape is (1, 3)
.
>>> tf.constant([[1, 2, 3]]).eval()
array([[1, 2, 3]], dtype=int32)
If you really wanted a scalar (rank 0), you wouldn't construct it with a sequence but rather just a scalar value.
>>> tf.constant(3)
<tf.Tensor 'Const_5:0' shape=() dtype=int32>
Notice the empty shape here, making it clear it is rank 0.
See Tensor/Rank in the documentation.
Upvotes: 2