tensor dimension transformation

① How to check the tensor dimension:
You can use the shape attribute or the size() method to check the shape of the tensor.

import torch

# 创建一个形状为(2, 3, 4)的张量
x = torch.randn(2, 3, 4)
print(x.shape)  # 输出: torch.Size([2, 3, 4])
print(x.size())  # 输出: torch.Size([2, 3, 4])

In the output result, the torch.Size object represents the shape of the tensor, and the size of each dimension can be obtained through the index. For example, in the example above, x.shape[0] returns a value of 2.

②How to extend two-dimensional to four-dimensional:
You can use the unsqueeze function in PyTorch to expand a two-dimensional tensor to a four-dimensional tensor. The unsqueeze function accepts an integer as a parameter, specifying in which dimension to expand, for example:

import torch

x = torch.randn(3, 4) # 二维张量
print(x.shape) # 输出: torch.Size([3, 4])

x = x.unsqueeze(0).unsqueeze(-1) # 在第0维和最后一维上进行扩展
print(x.shape) # 输出: torch.Size([1, 3, 4, 1])

In this example, unsqueeze(0) expands the 0th dimension of the original tensor into a new dimension of length 1, so the original number of rows of 3 will become the current length of the first dimension of 3. unsqueeze(-1) expands the new last dimension to become a new dimension with a length of 1, so the original number of columns 4 will become the current second dimension with a length of 4, and the position of the expanded dimension can also be represented by a negative number , such as unsqueeze(-1) means to expand on the penultimate dimension.

In this way, a two-dimensional tensor can be expanded into a four-dimensional tensor.

③ How to change the four-dimensional tensor into three-dimensional?
You can use the view method in PyTorch to convert a 4D tensor into a 3D tensor. Suppose we have a four-dimensional tensor with shape (batch_size, channels, height, width), we can use the following code to turn it into a three-dimensional tensor:

new_tensor = tensor.view(batch_size, channels * height, width)

Here we splice the original height and width dimensions into a new dimension channels * height. If you want to splice width and height, you can also use the following code:

new_tensor = tensor.view(batch_size, channels, height * width)

It should be noted that the view method does not change the data of the original tensor, but returns a new tensor, so the result needs to be saved in a new variable.

④ How to become 2-dimensional?
You can use the reshape function to change a four-dimensional tensor into a two-dimensional tensor. The sample code is as follows:

import torch

# 创建一个4维张量
x = torch.randn(2, 3, 4, 5)

# 将4维张量变为2维张量
x_2d = x.reshape(x.size(0), -1)

print(x.shape)    # torch.Size([2, 3, 4, 5])
print(x_2d.shape) # torch.Size([2, 60])

Here -1 is used as the second parameter of the reshape function, which means that all remaining elements are compressed into one dimension. In the above example, x.size(0) indicates the size of the first dimension of the tensor, which is 2, and all remaining elements are compressed into the second dimension, which is 3 4 5= 60 .

⑤ Can the view set height*width?
view() can be used to reshape tensors. If a tensor has shape (batch_size, channels, height, width) and you want to change it to (batch_size, height * width, channels), you can use the following code:

tensor = tensor.view(tensor.size(0), -1, tensor.size(1))

Where -1 means that the size of this dimension should be automatically inferred by other dimensions, here the value of height * width will be automatically calculated as the size of this dimension.

⑥Then can view change four-dimensional into two-dimensional?
Using the view function can transform a tensor of any dimension into a tensor of a specified shape, so a four-dimensional tensor can be changed into a two-dimensional tensor. For a 4D tensor, if you want to turn it into a 2D tensor, you can use the following code:

new_tensor = tensor.view(tensor.size(0), -1)

Among them, tensor.size(0) indicates the size of the first dimension of the tensor, and -1 indicates that PyTorch automatically infers the size of the second dimension. In this way, a four-dimensional tensor of any size can be converted into a two-dimensional tensor.

⑦ You can see that both view and reshape can change the shape of a tensor, so what is the relationship between view and reshape?

Both reshape and view can be used to change the shape of tensors. The main difference between them is how to deal with when the size of the dimension that needs to be changed is not divisible.

Specifically, reshape can use a -1 parameter when changing the shape, indicating that PyTorch should infer its size from other dimensions to ensure that the total size of the shape does not change. The view function cannot use the -1 parameter, and needs to manually calculate the size of all dimensions in the target shape.

Also, the view function already determines the size of the tensor, so the new shape must be the same size as the original shape. The reshape function can change the size of the tensor, including increasing or decreasing the number of elements of the tensor.

In general, the functions of reshape and view are very similar, but reshape is more flexible and can handle more shape transformation requirements, while view is more efficient and suitable for situations where the shape size is known.

Guess you like

Origin blog.csdn.net/qq_45104603/article/details/130072831