Skip to main content

How to Create a Tensor in TensorFlow with tf.constant

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor is TensorFlow's grid of numbers: a single number, a row of numbers, a table or a grid with more sides. tf.constant turns ordinary Python values into one. This guide creates four tensors with it in TensorFlow 2.22.0, and reads what TensorFlow prints about each.

Syntax​

Syntax
tf.constant(<value>, dtype=<data type>)
PartWhat it means
tfTensorFlow, imported with import tensorflow as tf
<value>A number, a list, or a list of lists of the same length
dtype=<data type>Optional. The type of every value in the tensor, such as tf.float32. Leave it out and TensorFlow picks one from the values

The tf.constant API page says what it does, "Creates a constant tensor from a tensor-like object", and how the type is chosen: "If the argument dtype is not specified, then the type is inferred from the type of value."

The Example​

Four tensors about the weather and a game: one temperature, a week of temperatures, the scores of two players in three games, and three counts stored as decimal numbers.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

make_tensors.py
import tensorflow as tf

temperature = tf.constant(21.5)
week = tf.constant([21.5, 23.0, 19.8, 22.1])
scores = tf.constant([[7, 9, 8], [6, 10, 9]])
counts = tf.constant([1, 2, 3], dtype=tf.float32)

print(temperature)
print(week)
print(scores)
print(counts)

The make_tensors.py script in a macOS-style code window: tf.constant makes a temperature of 21.5, a week of four temperatures, a two-row table of scores, and counts 1, 2, 3 with dtype tf.float32, then prints all four.

Each call turns one Python value into a tensor. The list of lists becomes a table with two rows, one per player. dtype=tf.float32 asks for decimal numbers even though 1, 2, 3 are whole numbers.

Step 2: Run It​

Terminal
uv run python make_tensors.py
Output
tf.Tensor(21.5, shape=(), dtype=float32)
tf.Tensor([21.5 23. 19.8 22.1], shape=(4,), dtype=float32)
tf.Tensor(
[[ 7 9 8]
[ 6 10 9]], shape=(2, 3), dtype=int32)
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32)

The terminal after uv run python make_tensors.py: the temperature tensor 21.5 with shape () and dtype float32, the week with shape (4,), the scores table with shape (2, 3) and dtype int32, and the counts 1. 2. 3. with dtype float32.

Each tensor prints its values, its shape and its dtype:

TensorshapedtypeWhy
temperature()float32A single number has no sides, so its shape is empty
week(4,)float32One row of 4 values
scores(2, 3)int322 rows of 3 values each, all whole numbers
counts(3,)float32dtype asked for it, so 1, 2, 3 became 1. 2. 3.

The tensor guide explains the two types TensorFlow picked by itself: "TensorFlow converts Python integers to tf.int32 and Python floating point numbers to tf.float32". The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Rows of different lengths: tf.constant([[1, 2, 3], [4, 5]])ValueError: Can't convert non-rectangular Python sequence to Tensor. A tensor is a full grid, so every row needs the same number of valuesGive every row the same length
Expecting to change a value laterA tensor made with tf.constant can't be changed: the tensor guide says "All tensors are immutable like Python numbers and strings"Build a new tensor from the old one

FAQ​

Is a tensor the same as a NumPy array?​

Close. The tensor guide says: "If you're familiar with NumPy, tensors are (kind of) like np.arrays." Both hold a grid of values of one type, and a tensor's .numpy() gives you the same values as a NumPy array.

Why does print show tf.Tensor(...) and not just the numbers?​

That's how a tensor describes itself: its values, then its shape and its data type, so one print tells you all three.

Conclusion​

tf.constant turned a number, a list and a list of lists into tensors with the shapes (), (4,) and (2, 3). TensorFlow picked float32 for decimals and int32 for whole numbers, and dtype overrode that choice.

Additional Resources​