The ** PyTorch Flatten** method carries both real and composite valued input tensors. The

**method is used to flatten the tensor into a one-dimensional tensor by reshaping them. In detail, we will discuss flatten() method using PyTorch in python.**

*torch.flatten()*And additionally, we will also cover different examples related to ** PyTorch flatten()** function. And we will cover these topics.

- What is PyTorch Flatten
- PyTorch Flatten example
- PyTorch flatten layer
- PyTorch Flatten list of tensors
- PyTorch Flatten parameters
- How to create a tensor with 2D elements and flatten this vector
- How to create a tensor with 3D elements and flatten this vector

Table of Contents

## What is PyTorch Flatten

In this section, we will learn about the **PyTorch flatten in python**.

The ** torch.flatten()** method is used to flatten the tensor into a one-dimensional tensor by reshaping them.

The PyTorch Flatten method carries both real and composite valued input tensors. It grips a torch tensor as an input and returns a torch tensor flattened into one dimension.

**Syntax:**

The Syntax of the PyTorch flatten:

`torch.flatten(input, start_dim=0, end_dim=-1)`

**Parameters:**

The following are the parameters of PyTorch Flatten

**input:**It is used as an input tensor.**start_dim:**It is used as the first dim to be flattened.**end_dim:**It is used as the last dim to be flattened.

So, with this, we understood about the PyTorch flatten in detail.

Read: PyTorch Pretrained Model

## PyTorch Flatten example

In this section, we will learn how to implement the PyTorch flatten with the help of an example in python.

The PyTorch Flatten method carries both real and composite valued input tensors. It grips a torch tensor as an input and returns a torch tensor flattened into one dimension. It contains the two parameters start_dim and end_dim.

**Code:**

In the following code firstly we will import the torch library such as import torch.

f = torch.tensor([[[2, 4], [6, 8]],[[10, 12],[14, 16]]]) is used to describe the variable by using torch.tensor() function.

torch.flatten(f): Here we are using the torch.flatten() function.

torch.flatten(f, start_dim=1) is use as a flatten() function and within this function we are using some parameters.

```
# Import library
import torch
# Describe the variable by using torch.tensor() function
f = torch.tensor([[[2, 4],
[6, 8]],
[[10, 12],
[14, 16]]])
# Using the torch.flatten() method
torch.flatten(f)
torch.tensor([2, 4, 6, 8, 10, 12, 14, 16])
torch.flatten(f, start_dim=1)
```

**Output:**

After running the above code we get the following output in which we can see that the PyTorch Flatten values are printed on the screen.

This is how we can understand about the PyTorch flatten with the help of an example.

Read: PyTorch MSELoss – Detailed Guide

## PyTorch flatten layer

In this section, we will learn about the **PyTorch flatten layer** in python.

PyTorch Flatten is used to reshape any of the tensor layers with dissimilar dimensions to a single dimension.

The ** torch.flatten()** function is used to flatten the tensor into a one-dimensional tensor by reshaping them.

**Code:**

In the following code firstly we will import the torch library such as import torch.

**f = torch.tensor([[[2, 4, 6], [8, 10, 12]], [[14, 16, 18], [20, 22, 24]]])**Here we are describing the f variable by using torch.tensor() function.**flatten_tens = torch.flatten(f)**Here we are describing flattening the above tensor by using end_dims.**print(“Flatten tensor:\n”, flatten_tens)**is used to print the flatten tensor.

```
# Import Library
import torch
f = torch.tensor([[[2, 4, 6],
[8, 10, 12]],
[[14, 16, 18],
[20, 22, 24]]])
print("Tensor:\n", f)
print("Size of Tensor:", f.size())
# Describe the flatten the above tensor by using end_dims
flatten_tens = torch.flatten(f)
flatten_tens1 = torch.flatten(f, end_dim=0)
flatten_tens2 = torch.flatten(f, end_dim=1)
flatten_tens3 = torch.flatten(f, end_dim=2)
# print the flatten tensors
print("Flatten tensor:\n", flatten_tens)
print("Flatten tensor (end_dim=0):\n", flatten_tens1)
print("Flatten tensor (end_dim=1):\n", flatten_tens2)
print("Flatten tensor (end_dim=2):\n", flatten_tens3)
```

**Output:**

In the below code you can see that PyTorch flatten layer is reshaped with the help of the * torch.flatten()* function.

So, with this, we understood how to reshape the tensor layer with the help of a ** torch.flatten()**.

Read: PyTorch nn linear + Examples

## PyTorch Flatten list of tensors

In this section, we will learn about the **PyTorch Flatten list of tensors** in python.

The PyTorch Flatten List of tensors inputs by reshaped it into a one-dimensional tensor. The input, start_dim, and end_dims are passed inside the torch.flatten() function. The dimensions starting with start_dim and ending with end_dim are flattened.

**Code:**

In the following code firstly we will import the torch library such as import torch.

**f = torch.tensor([[[31, 30],[29, 28]], [[27, 26],[25, 24]]])**is used to describe the variable by using torch.tensor() function.**torch.flatten(f)**Here we are using a torch.flatten() function.**i = torch.randn(4, 17, 18, 18)**is used to describe the variable by using torch.randn() function.**print(res)**is used to print the result by using the

```
# Import Library
impo# Import library
import torch
import torch.nn as nn
f = torch.tensor([[[31, 30],
[29, 28]],
[[27, 26],
[25, 24]]])
# Using torch.flatten()
torch.flatten(f)
torch.tensor([31, 30, 29, 28, 27, 26, 25, 24])
torch.flatten(f, start_dim=1)
# Describing the variable by using torch.randn()
i = torch.randn(4, 17, 18, 18)
j = nn.Sequential(
nn.Conv2d(17, 4, 18, 56, 4),
nn.Flatten()
)
res = j(i)
print(res)
res.size()
```

**Output:**

After running the above code, we get the following output in which we can see that the Pytorch Flatten list of tensors values is printed on the screen.

This is how we can understand about the PyTorch flatten a list of tensors in python.

Read: Cross Entropy Loss PyTorch

## PyTorch Flatten parameters

In this section, we will learn about the **PyTorch Flatten parameters **in python.

Here we are using the torch.flatten() function and within this function, we are using some parameters. The first parameter is input, the second parameter is start_sim and the last parameter is end_dim.

The dimensions starting with start_dim and ending with end_dim are flattened.

**Code:**

In the following code firstly we will import the torch library such as import torch.

**f = torch.empty(4,4,5,5).random_(30)**Here we are describing the f variable by using the torch.empty() function.**print(“Size of tensor:”, f.size())**is used to print the size of the tensor with the help of the print() function.- ftens = torch.flatten(f, start_dim=2, end_dim=3): Here we are flatten the above tensor using strat_dim and end_dim.
**print(“Flatten tensor (start_dim=2,end_dim=3):\n”, ftens)**is used to print the flatten tensor.

```
# Importing Library
import torch
# Describing the variable
f = torch.empty(4,4,5,5).random_(30)
# Printing the size of the tensor
print("Tensor:\n", f)
print("Size of tensor:", f.size())
# Here we are flatten the above tensor using start_dim and end_dim
ftens = torch.flatten(f, start_dim=2, end_dim=3)
# printing the flatten tensors
print("Flatten tensor (start_dim=2,end_dim=3):\n", ftens)
```

**Output:**

After running the above code, we get the following output in which we can see that the PyTorch Flatten parameters values are printed on the screen.

This is how we understand about the PyTorch Flatten parameters used in the torch.flatten() function.

Read: PyTorch Numpy to Tensor

## How to create a tensor with 2D elements and flatten this vector

In this section, we will learn about **How to create a tensor with two-dimensional elements and flatten this vector** in python.

Here we are using flatten() function that is used to flatten an N-dimensional tensor to a one-dimensional tensor.

**Code:**

In the following code firstly we will import the torch library such as import torch.

**f = torch.tensor([[2,4,6,8,10,12,14,16,18,20],[2,4,6,8,10,12,14,16,18,20]])**is used to creating a two-dimensional tensor with 10 elements.**print(f)**is used to show the actual tensor.**print(torch.flatten(f))**: Here we are flattering a tensor with flatten() function.

```
# import torch module
import torch
# Here we creating an 2 D tensor with 10 elements each
f = torch.tensor([[2,4,6,8,10,12,14,16,18,20],
[2,4,6,8,10,12,14,16,18,20]])
# Showing the actual tensor
print(f)
# Here we are flattening a tensor with flatten() function
print(torch.flatten(f))
```

**Output:**

After running the above code we get the following output in which we can see that the 2d tensor is creating with 10 elements values are printing on the screen.

So, with this, we understood How we can create a tensor with 2D elements and flatten this vector.

Read: PyTorch fully connected layer

## How to create a tensor with 3D elements and flatten this vector

In this section, we will learn about **how to create a tensor with 3D elements **and flatten this vector in python.

Here we are using flatten() function that is used to flatten an N-dimensional tensor to a one-dimensional tensor and create a tensor with three-dimensional elements and flatten the vector.

**Code:**

In the following code firstly we will import the torch library such as import torch.

**f = torch.tensor([[[2,4,6,8,10,12,14,16,18,20],[2,4,6,8,10,12,14,16,18,20]], [[2,4,6,8,10,12,14,16,18,20],[2,4,6,8,10,12,14,16,18,20]]])**is used describing the variable by using torch.tensor() function.**print(f)**is used to show the actual tensor.**print(torch.flatten(f)):**Here we are flatten a tensor with flatten() function.

```
# import torch module
import torch
# create an 3 D tensor with 10 elements each
f = torch.tensor([[[2,4,6,8,10,12,14,16,18,20],
[2,4,6,8,10,12,14,16,18,20]],
[[2,4,6,8,10,12,14,16,18,20],
[2,4,6,8,10,12,14,16,18,20]]])
# Showing the actual tensor
print(f)
# Here we are flatten a tensor with flatten() function
print(torch.flatten(f))
```

**Output:**

In the below output we can see that the 3d tensor is created with 10 elements values printing on the screen.

So, with this, we understood How to create a tensor with 3D elements and flatten this vector.

Also, take a look at some more PyTorch tutorials using Python.

- PyTorch Softmax
- PyTorch Model Summary
- PyTorch Stack Tutorial
- PyTorch Full() Function
- PyTorch Logistic Regression
- PyTorch Dataloader + Examples
- PyTorch Add Dimension

So, in this tutorial, we discussed **PyTorch Flatten** and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

- What is PyTorch Flatten
- PyTorch Flatten example
- PyTorch flatten layer
- PyTorch Flatten list of tensors
- PyTorch Flatten parameters
- How to create a tensor with 2D elements and flatten this vector
- How to create a tensor with 3D elements and flatten this vector

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.