Challenge: Reshaping Tensors
Завдання
Swipe to start coding
- Reshape the given tensor (with an initial shape of
3x4
) into a2x6
tensor. - Create a view of the reshaped tensor as a
4x3
tensor. - Use the appropriate method to add a new dimension at the third position (index
2
) of the tensor. - 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)