Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Understanding Tensor Operations

In TensorFlow, you can perform a wide range of operations using its basic data unit, the Tensor. From basic arithmetic operations like addition, subtraction, and multiplication to more complex operations like matrix multiplication and dimension alterations, tensors are highly versatile.

In this lesson, we will explore how to perform basic tensor operations.


1. Basic Operations (Addition, Subtraction, Multiplication, Division)

Basic arithmetic operations can be performed on tensors.

Tensor Operations Example
import tensorflow as tf

# Create two 1D tensors
tensor_a = tf.constant([1, 2, 3])
tensor_b = tf.constant([4, 5, 6])

# Perform basic operations
add_result = tf.add(tensor_a, tensor_b) # Addition

diff_result = tf.subtract(tensor_a, tensor_b) # Subtraction

mul_result = tf.multiply(tensor_a, tensor_b) # Multiplication

div_result = tf.divide(tensor_a, tensor_b) # Division

print("Addition result:", add_result)
# Addition result: tf.Tensor([5 7 9], shape=(3,), dtype=int32)

print("Subtraction result:", diff_result)
# Subtraction result: tf.Tensor([-3 -3 -3], shape=(3,), dtype=int32)

print("Multiplication result:", mul_result)
# Multiplication result: tf.Tensor([ 4 10 18], shape=(3,), dtype=int32)

print("Division result:", div_result)
# Division result: tf.Tensor([0.25 0.4 0.5], shape=(3,), dtype=float64)

2. Matrix Multiplication

In neural networks, operations between input data and weights are carried out via matrix multiplication.

For example, in each layer of a neural network, output is generated by multiplying an input vector with a weight matrix followed by applying an activation function, which utilizes matrix multiplication.

tf.matmul() is used for matrix multiplication.

Matrix Multiplication Example
# Create a 2x3 matrix and a 3x2 matrix
matrix_a = tf.constant([[1, 2, 3], [4, 5, 6]])
matrix_b = tf.constant([[7, 8], [9, 10], [11, 12]])

# Perform matrix multiplication
matmul_result = tf.matmul(matrix_a, matrix_b)
print("Matrix multiplication result:", matmul_result)
# Matrix multiplication result: tf.Tensor(
# [[ 58 64]
# [139 154]], shape=(2, 2), dtype=int32)

3. Dimension Alterations (Reshape & Transpose)

In deep learning models, data often needs to be transformed into specific shapes.

You can alter the shape of a tensor using tf.reshape() and tf.transpose().

# Convert a 1D tensor into a 2x3 matrix
reshaped_tensor = tf.reshape(tf.constant([1, 2, 3, 4, 5, 6]), (2, 3))
print("Reshaped tensor:", reshaped_tensor)
# Reshaped tensor: tf.Tensor(
# [[1 2 3]
# [4 5 6]], shape=(2, 3), dtype=int32)

# Transpose a matrix
transposed_tensor = tf.transpose(reshaped_tensor)
print("Transposed tensor:", transposed_tensor)
# Transposed tensor: tf.Tensor(
# [[1 4]
# [2 5]
# [3 6]], shape=(3, 2), dtype=int32)

In addition, you can perform various other tensor operations such as:

  • Aggregation Operations: Calculate sum, mean, maximum, minimum along specific dimensions

  • Broadcasting: Automatically expands a smaller tensor to a larger tensor for operations

  • tf.where(): Select values based on specified conditions

In the next lesson, we will test your understanding with a simple quiz based on what you have learned so far.

Want to learn more?

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