Elena' s AI Blog

Tensors in TensorFlow

13 Jan 2022 (updated: 29 Dec 2025) / 11 minutes to read

Elena Daehnhardt


Coding robot illustration

What Are Tensors in TensorFlow?

A tensor is an N-dimensional array that stores numerical data for machine learning computations. TensorFlow is an open-source machine learning library, created by Google Brain, that uses tensors as its core data structure for building and training deep neural networks. TensorFlow is robust, efficient, and works natively with Python, which is why this series uses it (TensorFlow documentation).

This post shows how to create tensors with tf.constant() and tf.Variable(), shuffle them with tf.random.shuffle(), index them, and read tensor metadata (dtype, shape, rank, and size) with concrete code examples.

# Import tensorflow
import tensorflow as tf
print(tf.__version__)
    2.7.0

Tensor Dimensions: Scalars, Vectors, and Matrices

A tensor stores numerical data in N dimensions, and its rank (ndim) names the structure:

Rank (ndim) Name Example
0 Scalar (zero-order tensor) tf.constant(7)
1 Vector tf.constant([5, 7])
2 Matrix tf.constant([[5, 7], [3, 10]])
N N-dimensional tensor tf.constant(array, shape=(5, 3, 2))

The examples below create these tensors as immutable constants with tf.constant(). TensorFlow reports the number of dimensions through the .ndim attribute.

# Creating a scalar tensor
scalar = tf.constant(7)
scalar
    <tf.Tensor: shape=(), dtype=int32, numpy=7>
# Check the number of tensor dimensions
scalar.ndim 
0
# Create a vector
vector = tf.constant([5, 7])
vector
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([5, 7], dtype=int32)>
vector.ndim
1

We create a matrix structure, with data type defined as int16.

# Create a matrix
matrix = tf.constant([[5, 7],
                      [3, 10]], dtype=tf.int16)
matrix
<tf.Tensor: shape=(2, 2), dtype=int16, numpy=
array([[ 5,  7],
       [ 3, 10]], dtype=int16)>
matrix.ndim
2

Creating Mutable Tensors with tf.Variable

A tf.constant tensor is immutable — its elements cannot be reassigned. A tf.Variable tensor is mutable: its elements can be changed in place with the .assign() method, which is how trainable weights are updated during model training.

# Create a variable tensor
tensor = tf.Variable([[[1, 2, 3],
                       [4, 5, 6]],
                      [[7, 8, 9],
                       [10, 11, 12]],
                      [[13, 14, 15],
                       [16, 17, 18]]])

In the code below, we use assign method to change the first element (which is a matrice) in tensor. We fillled its values with zeros.

# Change elements of the first tensor element
tensor[0].assign([[0, 0, 0], [0, 0, 0]])
<tf.Variable 'UnreadVariable' shape=(3, 2, 3) dtype=int32, numpy=
array([[[ 0,  0,  0],
        [ 0,  0,  0]],

       [[ 7,  8,  9],
        [10, 11, 12]],

       [[13, 14, 15],
        [16, 17, 18]]], dtype=int32)>

If we have a NumPy array, we can easily convert it into a tensor. In the example below, we have a NumPy array of 30 integers. We create a tensor consisting of 5 matrices with three rows and two columns using this array. This is useful because tensors can be run faster on GPU, while a native NumPy array cannot. However, please note that we can use libraries such as CuPy to speed up NumPy processing with GPU.

It is essential to ensure that the shape of tensor elements must be correctly defined and fit into array size. We can check it by multiplying shape dimensions 5x3x2=30.

# Turn a NumPy array into tensor
import numpy as np
numpy_array = np.arange(1, 31, dtype=np.int32)
numpy_array

X = tf.constant(numpy_array, shape=(5, 3, 2))
X
<tf.Tensor: shape=(5, 3, 2), dtype=int32, numpy=
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16],
        [17, 18]],

       [[19, 20],
        [21, 22],
        [23, 24]],

       [[25, 26],
        [27, 28],
        [29, 30]]], dtype=int32)>

Shuffling Tensors with tf.random.shuffle

tf.random.shuffle() randomly reorders a tensor along its first dimension. Shuffling removes ordering biases in the data before training, which prevents the model from learning the sequence of samples instead of their patterns.

In the following example, we create a tensor-based on NumPy array of 30 elements. We rearrange data into a shape of five matrices with three rows and two columns.

# Turn a NumPy array into tensor
import numpy as np
numpy_array = np.arange(1, 31, dtype=np.int32)
numpy_array

X = tf.constant(numpy_array, shape=(5, 3, 2))
X
<tf.Tensor: shape=(5, 3, 2), dtype=int32, numpy=
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16],
        [17, 18]],

       [[19, 20],
        [21, 22],
        [23, 24]],

       [[25, 26],
        [27, 28],
        [29, 30]]], dtype=int32)>

Finally, we shuffle the data with operation-level randomness seed.

# Shuffling tensor X with operation-level seed=24
tf.random.shuffle(X, seed=57)
<tf.Tensor: shape=(5, 3, 2), dtype=int32, numpy=
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[13, 14],
        [15, 16],
        [17, 18]],

       [[25, 26],
        [27, 28],
        [29, 30]],

       [[19, 20],
        [21, 22],
        [23, 24]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]]], dtype=int32)>

In my next post, I went more in-depth about how we can ensure the same order when shuffling data, which is essential for reproducing results in cases of model training or cross-validation. Please check my post on global and operation-level seeds.

Creating Random Tensors for Weight Initialization

A random tensor is a tensor populated with values drawn from a probability distribution, used to initialize neural network weights before training. The example below draws values from a normal (Gaussian) distribution using tf.random.Generator.from_seed().

Normal distribution, also known as the Gaussian distribution, is a probability distribution that is symmetric about the mean, showing that data near the mean are more frequent in occurrence than data far from the mean. In graph form, normal distribution will appear as a bell curve (see investopedia).

random1 = tf.random.Generator.from_seed(57) # set seed for reproducubility
random1 = random1.normal(shape=(3, 2))
random1
<tf.Tensor: shape=(3, 2), dtype=float32, numpy=
array([[ 0.49689844,  0.8259971 ],
       [ 1.0340209 , -0.24918637],
       [-1.5780283 , -0.92161775]], dtype=float32)>

Getting Tensor Information: dtype, shape, rank, and size

TensorFlow exposes a tensor’s metadata through attributes and functions: data type with tensor.dtype, shape with tensor.shape, rank (number of dimensions) with tensor.ndim, a specific axis with tensor.shape[axis], and total element count with tf.size(tensor), as in the following example:

tensor=tf.zeros(shape=[2, 3, 5])
print("Datatype of tensor elements: ", tensor.dtype)
print("Number of dimensions or rank: ", tensor.ndim)
print("Shape of tensor: ", tensor.shape)
print("Total number of elements in the tensor: ", tf.size(tensor).numpy())
print("Tensor elements along the first axis: ", tensor.shape[0])
print("Tensor elements along the last axis: ", tensor.shape[-1])
Datatype of tensor elements:  <dtype: 'float32'>
Number of dimensions or rank:  3
Shape of tensor:  (2, 3, 5)
Total number of elements in the tensor:  30
Tensor elements along the first axis:  2
Tensor elements along the last axis:  5

Summary: Working with Tensors in TensorFlow

Tensors are the core data structure of TensorFlow: N-dimensional arrays that hold the numerical data a model trains on. This post created tensors with tf.constant() (immutable) and tf.Variable() (mutable), generated random tensors for weight initialization, shuffled tensors with tf.random.shuffle(), and inspected tensor metadata with .dtype, .shape, .ndim, and tf.size(). For the official API reference, see the TensorFlow tensor guide.

For writing this post, I have used the tutorials at Udemy, TensorFlow Developer Certificate in 2022: Zero to Mastery.

Did you like this post? Please let me know if you have any comments or suggestions.

Posts that might be interesting for you


All Posts