SoFunction
Updated on 2025-04-14

Understanding of functions and x= functions

1. x = ()

x = ((0), -1)

In PyTorch,((0), -1)is a commonly used operation to change the shape of a tensor without changing its data.

Herexis a multidimensional tensor, and.view()Functions are used to reshape this tensor while keeping the total number of elements unchanged.

Specifically,((0), -1)The meaning is:

  • (0): This part obtains tensorxThe size of the first dimension (i.e.xis a shape of(a, b, c)tensor, then(0)It's equivalent toa). In most cases, this represents the number of samples in the batch or the length of the sequence, depending on the context.
  • -1:exist.view()In the function,-1is a special value that indicates that the size of the dimension is automatically calculated to keep the total number of elements unchanged. In other words, PyTorch infers the size of other dimensions and the total number of elements-1The specific value that should represent.

therefore,((0), -1)The function is to turn the tensorxReshape as a two-dimensional tensor, where the size of the first dimension remains unchanged (i.e. the size of the first dimension of the original tensor), while the size of the second dimension is automatically adjusted to contain all remaining elements.

This operation is useful when it is necessary to "flatten" multidimensional data into two-dimensional data for certain operations such as fully connected layers.

For example, ifxis a shape of(64, 3, 28, 28)tensor (usually represents a dataset containing 64 images, each with 3 color channels, each with a size of 28x28 pixels), then((0), -1)WillxReshape into a shape(64, 3*28*28)tensors, where each sample is flattened into a long vector.

2. () function

x = (x, start_dim=0, end_dim=2)

x = (x, 0)

When you usex = (x, 0)When, here0yesstart_dimthe value of the parameter, andend_dimThe parameters are still defaulted to-1. This means that the flattening operation will be from the tensorxThe first dimension of the tensor (the dimension with index 0) starts and proceeds all the way to the last dimension of the tensor.

However, sincestart_dimis set to 0, andend_dimDefault is-1, which actually will turn the entire tensorxCompletely flattened into a one-dimensional tensor. In other words, no matter the original tensorxWhat is the shape, call(x, 0)back,xWill become a one-dimensional tensor whose length is equal to the total number of all elements in the original tensor.

For example, if the original tensorxThe shape is(a, b, c, d), then callx = (x, 0)back,xThe new shape will be(a*b*c*d,), i.e. a containsa*b*c*dOne-dimensional tensor of each element.

This fully flattened operation is useful when it is necessary to convert multidimensional data into one-dimensional forms suitable for certain specific operations such as feed-forward propagation of fully connected layers. However, it also means that you lose shape information from the original data unless you have recorded this information elsewhere or your operation does not need to retain it.

x = (x, 1)

In PyTorch,(x, start_dim=0, end_dim=-1)Functions are used to convert tensorsxFlatten (or flattened) within the specified dimension without changing its data. Herestart_dimis the dimension that starts flattening (including this dimension),end_dimis the end flattening dimension (not including this dimension), by defaultend_dimis -1, that is, the last dimension.

When you usex = (x, 1)When you tell PyTorch to start from the second dimension (index is 1 because the index starts from 0) and to the last dimension, flattening all of these dimensions into one dimension. This means that ifxis a multi-dimensional tensor, then all dimensions except the first dimension will be merged into one dimension.

For example, ifxThe shape is(64, 3, 28, 28)(Represents 64 images, each image has 3 color channels, each channel has a size of 28x28 pixels), thenx = (x, 1)WillxFlatten into a shape(64, 3*28*28)tensor of . Here, the first dimension (sample number 64) remains unchanged, while the remaining three dimensions (3, 28, 28) are merged into one dimension.

This operation is particularly useful when processing image data, especially before the need to pass image data to a fully connected layer, since the fully connected layer generally expects the input to be two-dimensional (although in practice, the image data will usually be processed first through one or more convolutional layers). Through the flattening operation, you can convert multi-dimensional image data into two-dimensional forms for subsequent processing.

x = (x, 2)

In PyTorch,(input, start_dim=0, end_dim=-1)Functions are used to flatten multidimensional tensors into one-dimensional tensors, but you can specifystart_dimandend_dimParameters to control which dimension starts flattening and where the dimension ends (excluding that dimension). This means you can keep some dimensions of the tensor unchanged and flatten others.

For your codex = (x, 2),here:

  • xis the original tensor you want to flatten.
  • 2yesstart_dimthe value of the parameter, andend_dimThe default parameters are-1, means that the flattening operation will continue until the last dimension of the tensor.

therefore,(x, 2)It means from tensorxThe third dimension of the index starts from 0, flattening all subsequent dimensions into one dimension. ifxThe shape is for example(a, b, c, d, e),So(x, 2)after,xThe shape will change to(a, b, c*d*e). here,aandbThe dimension remains unchanged, andcdandeThe three dimensions are merged into a new dimension.

This operation is very useful when processing multidimensional data, especially when you need to keep the dimensions of a part of the data unchanged and "flatten" other parts of the data for subsequent processing (such as full connection layer processing).

III. Example

import torch

A = ([[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]])
# print()
print()

B = (A,1)
print()
# print(B)

C = (A,0,1)
print()
# print(C)

D = (A,2)
print()
# print(D)

E = (A,0)
print()
# print(E)

F = ((0), -1)
print()
# print(F)

G = ((0), -1, 1)
print()

Output:

([2, 3, 4])
([2, 12])
([6, 4])
([2, 3, 4])
([24])
([2, 12])
([2, 12, 1])

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.