Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

The Basic Unit of TensorFlow, Tensor

Tensor is the fundamental unit for representing data in TensorFlow.

In machine learning and deep learning models, you will work with numerous numerical data, primarily used to represent input data (features), weights, and loss.

Loss is the value that represents the difference between the predicted value by the model and the actual value, which the model strives to minimize during training.


Creating a Tensor

You can create immutable tensors using tf.constant().

Example of Creating a Tensor
import tensorflow as tf

# Create a 1-D tensor
tensor = tf.constant([1, 2, 3, 4, 5])

In the code above, tf.constant([1, 2, 3, 4, 5]) creates a tensor in the form of a 1-dimensional array.


In deep learning models, values like weights and biases change during the training process. To represent such mutable tensors, use tf.Variable().

Creating a Trainable Variable
# Create a trainable variable
weight = tf.Variable([0.5, -0.3, 0.8], dtype=tf.float32)

This function is used to store values that update as the model trains, and is essential when using the automatic differentiation feature with tf.GradientTape().


Shape of Tensors

For example, in an image classification model, each pixel value of a specific image can be represented as a tensor.

Example of Representing Image Data as a Tensor
# Represent pixel values of an image as a tensor
image_tensor = tf.constant([
[0, 128, 255, 64, 32],
[12, 200, 30, 90, 255],
[255, 180, 75, 40, 0],
[80, 190, 250, 140, 30],
[50, 100, 150, 200, 250]
], dtype=tf.float32)

In natural language processing (NLP), word embedding vectors are converted into tensors.

Word embedding is a technique for representing words as numerical vectors, capturing semantic similarity between words.

Words with similar meanings are represented as similar vectors.

Example of Representing Word Embeddings as Tensors
# Dog embedding vector
dog = tf.constant([0.2, 0.4, 0.6, 0.8, 1.0], dtype=tf.float32)

# Cat embedding vector (similar vector to dog)
cat = tf.constant([0.25, 0.38, 0.58, 0.85, 0.95], dtype=tf.float32)

# Car embedding vector (different vector from dog)
car = tf.constant([-0.8, 0.1, -0.5, 0.3, -0.6], dtype=tf.float32)

The tensor examples above illustrate image data in the form of a 2-dimensional array and word embeddings in the form of 1-dimensional vectors.


In the next lesson, we will explore dimensions and shapes of tensors in greater detail.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.