Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reduction Operations | Tensors
Introduction to TensorFlow
course content

Course Content

Introduction to TensorFlow

Introduction to TensorFlow

1. Tensors
2. Basics of TensorFlow

book
Reduction Operations

Reduction Operations

In the world of tensor operations, there are numerous tasks where we need to reduce the dimensions of our data, either by summarizing it across one axis or more. For instance, if we have a 2D tensor (a matrix), a reduction operation might compute a value for each row or each column, resulting in a 1D tensor (a vector). TensorFlow offers a set of operations to accomplish this, and in this chapter, we'll explore the most commonly used reduction operations.

Sum, Mean, Maximum, and Minimum

TensorFlow offers the following methods for these calculations:

  • tf.reduce_sum(): Computes the sum of all elements in the tensor or along a specific axis;
  • tf.reduce_mean(): Calculates the mean of the tensor elements;
  • tf.reduce_max(): Determines the maximum value in the tensor;
  • tf.reduce_min(): Finds the minimum value in the tensor.
12345678910111213141516171819
import tensorflow as tf tensor = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]) # Calculate sum of all elements total_sum = tf.reduce_sum(tensor) print("Total Sum:", total_sum.numpy()) # Calculate mean of all elements mean_val = tf.reduce_mean(tensor) print("Mean Value:", mean_val.numpy()) # Determine the maximum value max_val = tf.reduce_max(tensor) print("Maximum Value:", max_val.numpy()) # Find the minimum value min_val = tf.reduce_min(tensor) print("Minimum Value:", min_val.numpy())
copy

Note

The .numpy() method was used to convert the tensors into NumPy arrays, offering a clearer visual presentation of the numbers when displayed.

Operations along specific axes

Tensors can have multiple dimensions, and sometimes we want to perform reductions along a specific axis. The axis parameter allows us to specify which axis or axes we want to reduce.

  • axis=0: Perform the operation along the rows (resulting in a column vector);
  • axis=1: Perform the operation along the columns (resulting in a row vector);
  • It's possible to reduce along multiple axes simultaneously by providing a list to the axis parameter;
  • When the tensor's rank is reduced, you can use keepdims=True to retain the reduced dimension as 1.

For a 2D tensor (matrix):

1234567891011121314151617181920
import tensorflow as tf tensor = tf.constant([[1., 2.], [3., 4.], [5., 6.]]) # Calculate the sum of each column col_sum = tf.reduce_sum(tensor, axis=0) print("Column-wise Sum:", col_sum.numpy()) # Calculate the maximum of each row col_max = tf.reduce_max(tensor, axis=1) print("Row-wise Max:", col_max.numpy()) # Calculate the mean of the whole tensor (reduce along both directions) # Equivalent to not specifying the axis at all total_mean = tf.reduce_mean(tensor, axis=(0, 1)) print("Total Mean:", total_mean.numpy()) # Calculate the mean of the whole tensor (keeping reduced dimensions) total_mean_dim = tf.reduce_mean(tensor, axis=(0, 1), keepdims=True) print("Total Mean (saving dimensions):", total_mean_dim.numpy())
copy

Note

When you execute a reduction operation along a specific axis, you essentially eliminate that axis from the tensor, aggregating all the tensors within that axis element-wise. The same effect will remain for any number of dimensions.

Here's how it looks for a 3D tensor:

12345678910111213141516171819
import tensorflow as tf tensor = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]] ]) # Calculate the sum along axis 0 sum_0 = tf.reduce_sum(tensor, axis=0) print("Sum axis 0:\n", sum_0.numpy()) # Calculate the sum along axis 1 sum_1 = tf.reduce_sum(tensor, axis=1) print("Sum axis 1:\n", sum_1.numpy()) # Calculate the sum along axes 0 and 1 sum_0_1 = tf.reduce_sum(tensor, axis=(0, 1)) print("Sum axes 0 and 1:\n", sum_0_1.numpy())
copy

Note

Many other reduction operations exist in TensorFlow, but they operate on the same principles.

Task
test

Swipe to show code editor

Background

You are a data scientist at a weather research agency. You've been given a tensor containing weather readings from various cities over several days. The tensor has the following structure:

  • Dimension 1: Represents different cities.
  • Dimension 2: Represents different days.
  • Each entry in the tensor is a tuple of (temperature, humidity).

Objective

  1. Calculate the average temperature for each city over all the days.
  2. Calculate the maximum humidity reading across all cities for each day.

Note

In this tensor, the first number in each tuple represents the temperature (in Celsius) and the second number represents the humidity (in percentage) for that day and city.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 12
toggle bottom row

book
Reduction Operations

Reduction Operations

In the world of tensor operations, there are numerous tasks where we need to reduce the dimensions of our data, either by summarizing it across one axis or more. For instance, if we have a 2D tensor (a matrix), a reduction operation might compute a value for each row or each column, resulting in a 1D tensor (a vector). TensorFlow offers a set of operations to accomplish this, and in this chapter, we'll explore the most commonly used reduction operations.

Sum, Mean, Maximum, and Minimum

TensorFlow offers the following methods for these calculations:

  • tf.reduce_sum(): Computes the sum of all elements in the tensor or along a specific axis;
  • tf.reduce_mean(): Calculates the mean of the tensor elements;
  • tf.reduce_max(): Determines the maximum value in the tensor;
  • tf.reduce_min(): Finds the minimum value in the tensor.
12345678910111213141516171819
import tensorflow as tf tensor = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]) # Calculate sum of all elements total_sum = tf.reduce_sum(tensor) print("Total Sum:", total_sum.numpy()) # Calculate mean of all elements mean_val = tf.reduce_mean(tensor) print("Mean Value:", mean_val.numpy()) # Determine the maximum value max_val = tf.reduce_max(tensor) print("Maximum Value:", max_val.numpy()) # Find the minimum value min_val = tf.reduce_min(tensor) print("Minimum Value:", min_val.numpy())
copy

Note

The .numpy() method was used to convert the tensors into NumPy arrays, offering a clearer visual presentation of the numbers when displayed.

Operations along specific axes

Tensors can have multiple dimensions, and sometimes we want to perform reductions along a specific axis. The axis parameter allows us to specify which axis or axes we want to reduce.

  • axis=0: Perform the operation along the rows (resulting in a column vector);
  • axis=1: Perform the operation along the columns (resulting in a row vector);
  • It's possible to reduce along multiple axes simultaneously by providing a list to the axis parameter;
  • When the tensor's rank is reduced, you can use keepdims=True to retain the reduced dimension as 1.

For a 2D tensor (matrix):

1234567891011121314151617181920
import tensorflow as tf tensor = tf.constant([[1., 2.], [3., 4.], [5., 6.]]) # Calculate the sum of each column col_sum = tf.reduce_sum(tensor, axis=0) print("Column-wise Sum:", col_sum.numpy()) # Calculate the maximum of each row col_max = tf.reduce_max(tensor, axis=1) print("Row-wise Max:", col_max.numpy()) # Calculate the mean of the whole tensor (reduce along both directions) # Equivalent to not specifying the axis at all total_mean = tf.reduce_mean(tensor, axis=(0, 1)) print("Total Mean:", total_mean.numpy()) # Calculate the mean of the whole tensor (keeping reduced dimensions) total_mean_dim = tf.reduce_mean(tensor, axis=(0, 1), keepdims=True) print("Total Mean (saving dimensions):", total_mean_dim.numpy())
copy

Note

When you execute a reduction operation along a specific axis, you essentially eliminate that axis from the tensor, aggregating all the tensors within that axis element-wise. The same effect will remain for any number of dimensions.

Here's how it looks for a 3D tensor:

12345678910111213141516171819
import tensorflow as tf tensor = tf.constant([ [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]] ]) # Calculate the sum along axis 0 sum_0 = tf.reduce_sum(tensor, axis=0) print("Sum axis 0:\n", sum_0.numpy()) # Calculate the sum along axis 1 sum_1 = tf.reduce_sum(tensor, axis=1) print("Sum axis 1:\n", sum_1.numpy()) # Calculate the sum along axes 0 and 1 sum_0_1 = tf.reduce_sum(tensor, axis=(0, 1)) print("Sum axes 0 and 1:\n", sum_0_1.numpy())
copy

Note

Many other reduction operations exist in TensorFlow, but they operate on the same principles.

Task
test

Swipe to show code editor

Background

You are a data scientist at a weather research agency. You've been given a tensor containing weather readings from various cities over several days. The tensor has the following structure:

  • Dimension 1: Represents different cities.
  • Dimension 2: Represents different days.
  • Each entry in the tensor is a tuple of (temperature, humidity).

Objective

  1. Calculate the average temperature for each city over all the days.
  2. Calculate the maximum humidity reading across all cities for each day.

Note

In this tensor, the first number in each tuple represents the temperature (in Celsius) and the second number represents the humidity (in percentage) for that day and city.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 12
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt