SoFunction
Updated on 2024-10-29

A short summary of example usage of the flatten function in PyTorch

I. Usage

Flatten layer is mainly used to "flatten" the input, i.e., to one-dimensionalize the multi-dimensional input, which is used in the transition from convolutional layer to fully connected layer. It does not affect the size of the batch, which can be understood as followsStretch the high latitude array along the x- or y-axis into a one-dimensional array.

II. Parameters

1. start_dim (optional parameter): specifies the dimension from which the tensor starts to be spread. By default.start_dimis set to 0 to indicate that the spreading starts from the first dimension (usually the batch size). If set to any other integer value, it will start spreading from the specified dimension.

2. end_dim (optional parameter): specifies in which dimension to end the spreading tensor. By default.end_dimis set to -1, indicating that the spreading is done until the last dimension. If set to any other integer value, the flattening will end at the specified dimension.

III. Examples

(1). First define at random a data x that satisfies the normal distribution of (2, 3, 4)

import torch 
x = (2,3,4)
print(x)
x = (0)
print(x)
------------------------------------
tensor([[[ 0.1281,  1.6878,  0.2301, -0.0721],
         [ 1.2374, -0.6929,  1.1186,  0.4372],
         [ 0.5122,  1.4653, -0.1673,  0.7258]],
        [[ 0.2772, -1.9994, -1.2284,  0.2764],
         [-0.0451, -0.9195,  0.5749,  0.1942],
         [ 0.8539, -0.0434, -0.7313,  0.0234]]])
tensor([ 0.1281,  1.6878,  0.2301, -0.0721,  1.2374, -0.6929,  1.1186,  0.4372,
         0.5122,  1.4653, -0.1673,  0.7258,  0.2772, -1.9994, -1.2284,  0.2764,
        -0.0451, -0.9195,  0.5749,  0.1942,  0.8539, -0.0434, -0.7313,  0.0234])
import torch 
x = (2,3,4)
print(x)
x = (0)
print(x)
------------------------------------
tensor([[[ 0.1281,  1.6878,  0.2301, -0.0721],
         [ 1.2374, -0.6929,  1.1186,  0.4372],
         [ 0.5122,  1.4653, -0.1673,  0.7258]],
        [[ 0.2772, -1.9994, -1.2284,  0.2764],
         [-0.0451, -0.9195,  0.5749,  0.1942],
         [ 0.8539, -0.0434, -0.7313,  0.0234]]])
tensor([ 0.1281,  1.6878,  0.2301, -0.0721,  1.2374, -0.6929,  1.1186,  0.4372,
         0.5122,  1.4653, -0.1673,  0.7258,  0.2772, -1.9994, -1.2284,  0.2764,
        -0.0451, -0.9195,  0.5749,  0.1942,  0.8539, -0.0434, -0.7313,  0.0234])

At this point the dimension of x is 2 x 3 x 4 = 24, and x = flatten(0) and x = flatten() give the same result.

 (2).

import torch 
x = (2,3,4)
print(x)
x = (1)
print(x)
===========================================
tensor([[[-0.7137, -0.0859, -1.5284,  0.7284],
         [ 0.8425,  0.3606,  1.7639,  0.1848],
         [ 0.4040, -1.6575,  1.9134, -1.0787]],
        [[ 0.6981,  1.3494, -0.5817, -1.1824],
         [-0.4972,  0.4179,  2.1742, -0.2462],
         [ 0.2429, -1.9315, -0.3497,  0.7190]]])
tensor([[-0.7137, -0.0859, -1.5284,  0.7284,  0.8425,  0.3606,  1.7639,  0.1848,
          0.4040, -1.6575,  1.9134, -1.0787],
        [ 0.6981,  1.3494, -0.5817, -1.1824, -0.4972,  0.4179,  2.1742, -0.2462,
          0.2429, -1.9315, -0.3497,  0.7190]])

At this point x is expanded from dimension 1, and the final x dimension is (2, 3 × 4), which is (2, 12)

Attention:start_dimcap (a poem)end_dimThe value of the parameter should be in the range of-() <= start_dim <= end_dim < () Between.

This article on the use of PyTorch flatten() function is introduced to this article, more related to PyTorch flatten() function content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future!