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:

x=tf.constant(2.0)

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

x=tf.constant([1,2,3])

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

x=tf.constant([[1,2],[3,4],[5,6]])

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.

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

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
a=tf.constant([1,2,3,4,5,6],shape=[5])

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

a=tf.constant([1,2,3,4,5,6],shape=[2,3])
#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:

a=tf.constant([1,2,3],shape=[2,3])
#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.

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

 

Leave a Reply