Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Reshaping Tensors | PyTorch Introduction
PyTorch Essentials

book
Challenge: Reshaping Tensors

Compito

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.

Soluzione

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)

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 10
single

single

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)

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt