Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Reshaping Tensors | PyTorch Introduction
PyTorch Essentials

book
Challenge: Reshaping Tensors

Завдання

Swipe to start coding

  1. Reshape the given tensor (with an initial shape of 3x4) into a 2x6 tensor.
  2. Create a view of the reshaped tensor as a 4x3 tensor.
  3. Use the appropriate method to add a new dimension at the third position (index 2) of the tensor.
  4. Use squeeze() to remove the added dimension.

Рішення

import torch

tensor = torch.randint(1, 10, (3, 4))
# Reshape the tensor into a 2x6 tensor
reshaped_tensor = tensor.reshape(2, 6)
# Create a view of the reshaped tensor as a 4x3 tensor
view_tensor = reshaped_tensor.view(4, 3)
# Add a new dimension at the third position
unsqueezed_tensor = reshaped_tensor.unsqueeze(2)
# Remove the added dimension
squeezed_tensor = unsqueezed_tensor.squeeze(2)
# Print results
print("Original tensor:\n", tensor)
print("Reshaped tensor:\n", reshaped_tensor)
print("View tensor:\n", view_tensor)
print("Unsqueezed tensor shape:\n", unsqueezed_tensor.shape)
print("Squeezed tensor shape:\n", squeezed_tensor.shape)

Все було зрозуміло?

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

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

Секція 1. Розділ 10
import torch

tensor = torch.randint(1, 10, (3, 4))
# Reshape the tensor into a 2x6 tensor
reshaped_tensor = tensor.___
# Create a view of the reshaped tensor as a 4x3 tensor
view_tensor = reshaped_tensor.___
# Add a new dimension at the third position
unsqueezed_tensor = reshaped_tensor.___
# Remove the added dimension
squeezed_tensor = unsqueezed_tensor.___
# Print results
print("Original tensor:\n", tensor)
print("Reshaped tensor:\n", reshaped_tensor)
print("View tensor:\n", view_tensor)
print("Unsqueezed tensor shape:\n", unsqueezed_tensor.shape)
print("Squeezed tensor shape:\n", squeezed_tensor.shape)
toggle bottom row
some-alt