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

Reduction OperationsReduction 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.

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):

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:

Note

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

Tarea

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.

¿Todo estuvo claro?

Sección 1. Capítulo 12
toggle bottom row
course content

Contenido del Curso

Introduction to TensorFlow

Reduction OperationsReduction 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.

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):

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:

Note

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

Tarea

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.

¿Todo estuvo claro?

Sección 1. Capítulo 12
toggle bottom row
some-alt