How to create a tensorflow.constant tensor with a list?

The tensorflow.constant is one of the functions to create a tensor with known values. Its usage is flexible and may not be what you think. The simplest usage would be:


This will create a 0-d tensor(scalar) x with the value 2.0.


This creates a 1-d tensor with a 1-d list, and the shape of this tensor is (3,)


This creates a 2-d tensor with a 2-d python list. Its shape is (3,2).

Ok, those are straight-forward and self-explanatory. But the tf.constant function has another parameter called shape. What is the purpose of this shape parameter? What if the shape parameter is inconsistent with the shape of the python list provided as the value parameter?

If the shape parameter is the same as the shape of the python list, then it is redundant and useless.


But if the shape parameter is different than the shape of the list, what happens? Well, if the shape parameter specifies more elements than contained in the list, the last element in the list would be expanded to the remaining elements of the resulting tensor:

a=tf.constant([1,2,3,4,5],shape=[6]) #[1,2,3,4,5,5]
a=tf.constant([1,2,3],shape=[6]) #[1,2,3,3,3,3]

If the number of elements specified by the shape parameter is less than the number of elements contained in the list, an error occurs:

#ValueError: Too many elements provided. Needed at most 5, but received 6

The dimensions specified by the shape parameter do not need to be the same as those of the list:

#Tensor("Const_13:0", shape=(2, 3), dtype=int32)
[[1 2 3]
 [4 5 6]]

In the above example, the value parameter is a 1-d list while the shape parameter specifies a 2-d tensor. The resulting tensor is subject to the shape parameter, i.e., a 2-d tensor. The resulting 2-d tensor is formed by splitting the single row of the value list into 2 rows. Here the last element expanding rule also applies:

#Tensor("Const_14:0", shape=(2, 3), dtype=int32)
[[1 2 3]
 [3 3 3]]

When the shape parameter is present, the value parameter does not need to be an 1-d list, it can be a list of any dimension. In other words, the shape of the value list does not matter in the presence of the shape parameter. You just take all the elements in the value list in order and fill them one by one to the resulting array. The shape of the resulting tensor is determined by the shape parameter, not the value parameter.

#Tensor("Const_15:0", shape=(2, 3), dtype=int32)
[[1 2 3]
 [4 5 6]]


If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply