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

Зміст курсу

Introduction to TensorFlow

Introduction to TensorFlow

1. Tensors
2. Basics of TensorFlow

book
Transformations

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).
1234567891011121314
import tensorflow as tf # Create a tensor with shape (3, 2) tensor = tf.constant([[1, 2], [3, 4], [5, 6]]) # Reshape the tensor to shape (2, 3) reshaped_tensor = tf.reshape(tensor, (2, 3)) print(reshaped_tensor) print('-' * 50) # Reshape the tensor to shape (6, 1); # The size of the first dimention is determined automatically reshaped_tensor = tf.reshape(tensor, (-1, 1)) print(reshaped_tensor)
copy

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.
12345678910111213
import tensorflow as tf # Create a tensor tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Slice tensor to extract sub-tensor from index (0, 1) of size (1, 2) sliced_tensor = tf.slice(tensor, begin=(0, 1), size=(1, 2)) print(sliced_tensor) print('-' * 50) # Slice tensor to extract sub-tensor from index (1, 0) of size (2, 2) sliced_tensor = tf.slice(tensor, (1, 0), (2, 2)) print(sliced_tensor)
copy

Note

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

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.
12345678910111213
import tensorflow as tf # Create a mutable tensor tensor = tf.Variable([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Change the entire first row tensor[0, :].assign([0, 0, 0]) print(tensor) print('-' * 80) # Modify the second and the third columns tensor[:, 1:3].assign(tf.fill((3,2), 1)) print(tensor)
copy

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.

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).
123456789101112131415161718
import tensorflow as tf # Create two tensors tensor1 = tf.constant([[1, 2, 3], [4, 5, 6]]) tensor2 = tf.constant([[7, 8, 9]]) # Concatenate tensors vertically (along rows) concatenated_tensor = tf.concat([tensor1, tensor2], axis=0) print(concatenated_tensor) print('-' * 50) # Create another set of tensors tensor3 = tf.constant([[1, 2], [4, 5]]) tensor4 = tf.constant([[3], [6]]) # Concatenate tensors horizontally (along columns) concatenated_tensor = tf.concat([tensor3, tensor4], axis=1) print(concatenated_tensor)
copy

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.
Завдання
test

Swipe to show code editor

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 11
toggle bottom row

book
Transformations

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).
1234567891011121314
import tensorflow as tf # Create a tensor with shape (3, 2) tensor = tf.constant([[1, 2], [3, 4], [5, 6]]) # Reshape the tensor to shape (2, 3) reshaped_tensor = tf.reshape(tensor, (2, 3)) print(reshaped_tensor) print('-' * 50) # Reshape the tensor to shape (6, 1); # The size of the first dimention is determined automatically reshaped_tensor = tf.reshape(tensor, (-1, 1)) print(reshaped_tensor)
copy

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.
12345678910111213
import tensorflow as tf # Create a tensor tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Slice tensor to extract sub-tensor from index (0, 1) of size (1, 2) sliced_tensor = tf.slice(tensor, begin=(0, 1), size=(1, 2)) print(sliced_tensor) print('-' * 50) # Slice tensor to extract sub-tensor from index (1, 0) of size (2, 2) sliced_tensor = tf.slice(tensor, (1, 0), (2, 2)) print(sliced_tensor)
copy

Note

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

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.
12345678910111213
import tensorflow as tf # Create a mutable tensor tensor = tf.Variable([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Change the entire first row tensor[0, :].assign([0, 0, 0]) print(tensor) print('-' * 80) # Modify the second and the third columns tensor[:, 1:3].assign(tf.fill((3,2), 1)) print(tensor)
copy

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.

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).
123456789101112131415161718
import tensorflow as tf # Create two tensors tensor1 = tf.constant([[1, 2, 3], [4, 5, 6]]) tensor2 = tf.constant([[7, 8, 9]]) # Concatenate tensors vertically (along rows) concatenated_tensor = tf.concat([tensor1, tensor2], axis=0) print(concatenated_tensor) print('-' * 50) # Create another set of tensors tensor3 = tf.constant([[1, 2], [4, 5]]) tensor4 = tf.constant([[3], [6]]) # Concatenate tensors horizontally (along columns) concatenated_tensor = tf.concat([tensor3, tensor4], axis=1) print(concatenated_tensor)
copy

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.
Завдання
test

Swipe to show code editor

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 11
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt