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

TransformationsTransformations

Tensor Transformations

Welcome back! Today, we're going to dive deeper into more advanced operations: transforming tensors.

Tensor transformations are crucial when working with data. As you delve further into deep learning and data science tasks, you'll find that the data you deal with isn't always in the format you need. This lesson will introduce you to methods in TensorFlow that allow you to manipulate the structure and content of tensors to fit your needs.


Reshaping Tensors

When working with tensors, there are times you'll need to change the shape without altering the underlying data. tf.reshape() comes in handy during such times.

How it works:

  • Reshaping changes the tensor's structure, but not its data. The total number of elements before and after the reshape must remain the same;
  • It operates by "filling" the new shape row-wise (from left to right, top to bottom).

Note

When specifying the new shape, one dimension can be -1. TensorFlow will compute the size of that dimension so the total size remains constant.


This is particularly useful when you want to feed tensors into a neural network but the dimensions don't match the network's input shape.


Slicing

Slicing helps you retrieve a portion of a tensor. It's analogous to Python's list slicing but extended to multi-dimensional tensors.

How it works:

  • tf.slice() extracts a slice from a tensor. It requires the starting index for the slice and the size of the slice;
  • If the size is -1, it denotes all elements in that dimension.

Note

Always remember the zero-based indexing of TensorFlow, which is similar to Python's native indexing.


Useful for extracting specific features or data points from a larger dataset.


Modifying Data

There is other way of slicing that also allows you to modify the original data, similar to slicing arrays in NumPy.

How it works:

  • Using [], you can easily slice and index tensors, akin to NumPy slicing. This approach lets you select specific rows, columns, or elements of a tensor;
  • With tf.Variable(), the tensor becomes mutable, allowing direct modifications using slicing;
  • To modify the selected subtensor's values, use the .assign() method with a tensor or list that matches its shape.

Note

  • The slicing syntax in TensorFlow is largely inspired by NumPy, so if you're familiar with NumPy, transitioning to TensorFlow's slicing mechanism is straightforward;
  • Always ensure you're using tf.Variable() for any operations that require tensor mutability.

Slicing combined with tensor modification can be instrumental when preprocessing data, tweaking model weights, or making batch updates during training.


Concatenating

Concatenation allows you to join multiple tensors along a specified axis.

How it works:

  • tf.concat() combines tensors. The method requires a list of tensors you want to concatenate and the axis along which to perform the operation;
  • The axis is zero-based. An axis of 0 refers to rows (vertically) and an axis of 1 refers to columns (horizontally).

Note

  • Ensure the tensors you're joining have matching dimensions on the non-concatenated axes;
  • This operation is similar to numpy.concatenate(), but tailored for TensorFlow tensors.

Useful for combining data from multiple sources or assembling features into a single data structure.

Tarefa

Background

You're working on a dataset consisting of readings from various sensors placed at different geographical locations. These sensors record weather-related data such as temperature, pressure, and normalized geographical coordinates.

However, while compiling the readings, you found that some of the data has been recorded incorrectly.

You also received new readings from other sensors that you need to include.

Dataset Information

  1. main_dataset: A tensor of shape (6, 4) representing 6 readings. Each row is a sample, and the columns represent the following features:
    • Temperature (in Celsius).
    • Pressure (in hPa).
    • Normalized latitude coordinate.
    • Normalized longitude coordinate.
  2. error_correction_data: A tensor of shape (2, 4) representing 2 corrected readings for erroneous data in the main dataset.
  3. additional_data: A tensor of shape (3, 4) representing 3 new readings.

Objective

Prepare a corrected and complete dataset for weather prediction:

  1. Data Correction:
    • You found out that the readings on the 2nd and 5th rows of the main_dataset were inaccurate. Replace these rows in the main_dataset with the rows from error_correction_data.
  2. Incorporate Additional Data:
    • Concatenate the main_dataset with additional_data to incorporate the new readings.
  3. Batch Reshaping:
    • For the purpose of batch training, you want to divide the dataset into batches with 3 readings per batch. Reshape complete_dataset, where the first dimension represents the batch size, and the second dimension represents the number of readings per batch.

Tudo estava claro?

Seção 1. Capítulo 11
toggle bottom row
course content

Conteúdo do Curso

Introduction to TensorFlow

TransformationsTransformations

Tensor Transformations

Welcome back! Today, we're going to dive deeper into more advanced operations: transforming tensors.

Tensor transformations are crucial when working with data. As you delve further into deep learning and data science tasks, you'll find that the data you deal with isn't always in the format you need. This lesson will introduce you to methods in TensorFlow that allow you to manipulate the structure and content of tensors to fit your needs.


Reshaping Tensors

When working with tensors, there are times you'll need to change the shape without altering the underlying data. tf.reshape() comes in handy during such times.

How it works:

  • Reshaping changes the tensor's structure, but not its data. The total number of elements before and after the reshape must remain the same;
  • It operates by "filling" the new shape row-wise (from left to right, top to bottom).

Note

When specifying the new shape, one dimension can be -1. TensorFlow will compute the size of that dimension so the total size remains constant.


This is particularly useful when you want to feed tensors into a neural network but the dimensions don't match the network's input shape.


Slicing

Slicing helps you retrieve a portion of a tensor. It's analogous to Python's list slicing but extended to multi-dimensional tensors.

How it works:

  • tf.slice() extracts a slice from a tensor. It requires the starting index for the slice and the size of the slice;
  • If the size is -1, it denotes all elements in that dimension.

Note

Always remember the zero-based indexing of TensorFlow, which is similar to Python's native indexing.


Useful for extracting specific features or data points from a larger dataset.


Modifying Data

There is other way of slicing that also allows you to modify the original data, similar to slicing arrays in NumPy.

How it works:

  • Using [], you can easily slice and index tensors, akin to NumPy slicing. This approach lets you select specific rows, columns, or elements of a tensor;
  • With tf.Variable(), the tensor becomes mutable, allowing direct modifications using slicing;
  • To modify the selected subtensor's values, use the .assign() method with a tensor or list that matches its shape.

Note

  • The slicing syntax in TensorFlow is largely inspired by NumPy, so if you're familiar with NumPy, transitioning to TensorFlow's slicing mechanism is straightforward;
  • Always ensure you're using tf.Variable() for any operations that require tensor mutability.

Slicing combined with tensor modification can be instrumental when preprocessing data, tweaking model weights, or making batch updates during training.


Concatenating

Concatenation allows you to join multiple tensors along a specified axis.

How it works:

  • tf.concat() combines tensors. The method requires a list of tensors you want to concatenate and the axis along which to perform the operation;
  • The axis is zero-based. An axis of 0 refers to rows (vertically) and an axis of 1 refers to columns (horizontally).

Note

  • Ensure the tensors you're joining have matching dimensions on the non-concatenated axes;
  • This operation is similar to numpy.concatenate(), but tailored for TensorFlow tensors.

Useful for combining data from multiple sources or assembling features into a single data structure.

Tarefa

Background

You're working on a dataset consisting of readings from various sensors placed at different geographical locations. These sensors record weather-related data such as temperature, pressure, and normalized geographical coordinates.

However, while compiling the readings, you found that some of the data has been recorded incorrectly.

You also received new readings from other sensors that you need to include.

Dataset Information

  1. main_dataset: A tensor of shape (6, 4) representing 6 readings. Each row is a sample, and the columns represent the following features:
    • Temperature (in Celsius).
    • Pressure (in hPa).
    • Normalized latitude coordinate.
    • Normalized longitude coordinate.
  2. error_correction_data: A tensor of shape (2, 4) representing 2 corrected readings for erroneous data in the main dataset.
  3. additional_data: A tensor of shape (3, 4) representing 3 new readings.

Objective

Prepare a corrected and complete dataset for weather prediction:

  1. Data Correction:
    • You found out that the readings on the 2nd and 5th rows of the main_dataset were inaccurate. Replace these rows in the main_dataset with the rows from error_correction_data.
  2. Incorporate Additional Data:
    • Concatenate the main_dataset with additional_data to incorporate the new readings.
  3. Batch Reshaping:
    • For the purpose of batch training, you want to divide the dataset into batches with 3 readings per batch. Reshape complete_dataset, where the first dimension represents the batch size, and the second dimension represents the number of readings per batch.

Tudo estava claro?

Seção 1. Capítulo 11
toggle bottom row
some-alt