Python ProgrammingPython Programming

How to create tensor using "constant" function in TensorFlow?

The "constant" function is most popular function to create tensors. The only required argument is value or list of values used to create tensor.
The default data type is float32 is list of values are of floating type and int32 if values are integer type.
##
# TensorFlow program to create tensor using constant function.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
t1 = tf.constant([1, 2, 3])
t2 = tf.constant([[1.1, 2.2, 3.3], [4, 5, 6]])
t3 = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
t4 = tf.constant(["India", "Australia"])
t5 = tf.constant([15.5, 20.5, 35.5, 40.1, 80.5], tf.float16, [5], 'T5', False)
sess = tf.Session()
 
print(t1)
print(t2)
 
print("\n########################\n")
print(t3)
print(sess.run(t3))
print("\n########################\n")
 
print(t4)
 
print("\n########################\n")
print(t5)
print(sess.run(t5))
print("\n########################\n")

Sample output of above program.
Create tensor using constant function