📅  最后修改于: 2023-12-03 15:35:21.753000             🧑  作者: Mango
Torch View is a PyTorch function that returns a tensor with the same data as the input tensor but with a different size or shape. It is a powerful tool that allows us to manipulate tensors and create new tensors with different sizes and shapes, without changing the data stored in the original tensor.
torch.view(input, shape)
The function takes two arguments:
input
: the input tensor that we want to reshape.shape
: the new shape that we want to apply to the input tensor.import torch
# create a 2D tensor with shape (3, 4)
x = torch.randn(3, 4)
print(x)
# reshape the tensor to have shape (2, 6)
y = torch.view(x, (2, 6))
print(y)
Output:
tensor([[ 0.1254, 1.0163, -1.5829, 0.1746],
[ 0.7520, -0.1972, -1.4101, 0.7017],
[-0.4865, -1.0701, 0.3225, -0.1152]])
tensor([[ 0.1254, 1.0163, -1.5829, 0.1746, 0.7520, -0.1972],
[-1.4101, 0.7017, -0.4865, -1.0701, 0.3225, -0.1152]])
In this example, we create a 2D tensor x
with shape (3, 4)
. We then use torch.view
to reshape the tensor to have shape (2, 6)
. The resulting tensor y
has the same data as x
, but with a different shape.
Torch View is a powerful PyTorch function that allows us to manipulate tensors and create new tensors with different sizes and shapes, without changing the data stored in the original tensor. It is a useful tool for deep learning engineers who work with tensors on a regular basis.