No, torch.flatten() function does not copy any data, and actually it behaves more like a wrapper around the view() function. Simple way to prove it without having any explicit mention of it in the docs is by running the following lines of code: # Create (2, 3, 4) shape data tensor filled with 0. … Answer from gMatas on discuss.pytorch.org
🌐
PyTorch Forums
discuss.pytorch.org › uncategorized
What is the difference of .flatten() and .view(-1) in PyTorch? - Uncategorized - PyTorch Forums
July 27, 2019 - Both .flatten() and .view(-1) flattens a tensor in PyTorch. What’s the difference? Does .flatten() copy data of the tensor? Is .view(-1) faster? Is there any situation that .flatten() doesn’t work? I’ve trie…
Discussions

Switching between torch.flatten(x) and x.view(-1, 256)
I have this code here. I used torch.flatten(x) at first. the fc1 layer expects 2560 input and crossentropyloss expects float data values. After training, the loss is around 100. I changed torch.flatten(x) to x.view(-1, 256), fc1 layer expects 256 input and crossentropyloss wants Long values. ... More on discuss.pytorch.org
🌐 discuss.pytorch.org
3
0
January 24, 2023
torch.flatten is inconsistent with NumPy's flatten
NumPy's flatten always makes a copy of its input's data (see its documentation). PyTorch's flatten, on the other hand, returns the input itself or a view of input when possible. For a b... More on github.com
🌐 github.com
6
December 30, 2020
python - What is the difference between x.view(x.size(0), -1) and torch.nn.Flatten() layer and torch.flatten(x)? pytorch question - Stack Overflow
I'm quite curious on what's the difference between using view(,-1) and flatten like the simple code here: Since I found that the size and data all flatten to one dimension. import torch from torch ... More on stackoverflow.com
🌐 stackoverflow.com
python - Difference between torch.flatten() and nn.Flatten() - Stack Overflow
Found this: torch/csrc/utils/tensor_flatten.h. Seems like it uses view, which is a reshape! More on stackoverflow.com
🌐 stackoverflow.com
🌐
benchatronics
whatsmyskill.com.ng › home › comparison between pytorch flatten vs view
Comparison between pytorch flatten vs view - What is my skills
September 12, 2024 - Focus: Flattening (collapsing) dimensions to create a 1D tensor. Use Case: Useful in deep learning when you need to pass multi-dimensional data into fully connected layers in neural networks. Dimension Control: You can specify which dimensions to collapse with start_dim and end_dim. PyTorch’s view() method is used to reshape a tensor into any size.
🌐
PyTorch Forums
discuss.pytorch.org › vision
Switching between torch.flatten(x) and x.view(-1, 256) - vision - PyTorch Forums
January 24, 2023 - I have this code here. I used torch.flatten(x) at first. the fc1 layer expects 2560 input and crossentropyloss expects float data values. After training, the loss is around 100. I changed torch.flatten(x) to x.view(-1, 256), fc1 layer expects 256 input and crossentropyloss wants Long values. ...
🌐
CodeGenes
codegenes.net › blog › pytorch-flatten-vs-view
PyTorch Flatten vs View: A Comprehensive Guide — codegenes.net
When you want to reshape a tensor without changing its underlying data: The view method is more efficient than flatten when you only need to change the shape of a tensor without altering its data.
🌐
Codemia
codemia.io › home › knowledge hub › what is the difference between .flatten and .view-1 in pytorch?
What is the difference between .flatten and .view-1 in PyTorch? | Codemia
September 23, 2025 - When the tensor is non-contiguous, .flatten() creates a copy to make the data contiguous. This involves a memory allocation and a data copy, which adds overhead. The .view(-1) approach forces you to call .contiguous() explicitly, making the copy visible in your code:
🌐
James D. McCaffrey
jamesmccaffreyblog.com › home › the pytorch view() reshape() squeeze() and flatten() functions
The PyTorch view() reshape() squeeze() and flatten() Functions - James D. McCaffreyJames D. McCaffrey
July 2, 2019 - X = T.tensor([[1,2,3,4], [5,6,7,8], [9,10,11,12]], dtype=T.float32) Y = X.view(1,3,4) print(Y) Now because the first dimension has size 1, the only legal value is [0], for example: ... So in some sense a dimension with size 1 is useless. The squeeze() function eliminate any dimension that has size 1. So, ... The flatten() function is sort of a cousin to squeeze().
🌐
Lesdeuxgamins
lesdeuxgamins.ca › vertic328 › torch-flatten-vs-view
torch.flatten vs view
October 24, 2024 - Here, we use view to broadcast ... in PyTorch. While flatten provides a simple way to linearize tensors, view offers a more flexible approach for reshaping data while maintaining memory efficiency....
Find elsewhere
🌐
Data Science Weekly
datascienceweekly.org › tutorials › flatten-a-pytorch-tensor
Flatten A PyTorch Tensor
So it's still the same after the dot view operation. Perfect! We were able to flatten a PyTorch tensor by using the PyTorch view operation and the negative one.
🌐
GitHub
github.com › pytorch › pytorch › issues › 49954
torch.flatten is inconsistent with NumPy's flatten · Issue #49954 · pytorch/pytorch
December 30, 2020 - NumPy's flatten always makes a copy of its input's data (see its documentation). PyTorch's flatten, on the other hand, returns the input itself or a view of input when possible. For a b...
Author: pytorch
🌐
Sprintchase
sprintchase.com › home › torch.flatten(): flattening a tensor in pytorch
torch.flatten(): Flattening a Tensor in PyTorch
May 16, 2025 - The main use case is before a Linear layer in neural networks. The torch.flatten() is similar to tensor.view(-1) or tensor.reshape(-1) for full flattening, but flatten() is more explicit and supports partial flattening.
🌐
Python Guides
pythonguides.com › pytorch-view
PyTorch view(): Reshape Tensors Without Copying
June 18, 2025 - torch.flatten(x, 1) and nn.Flatten() do the same job and also handle non-contiguous input; see PyTorch flatten. For the layer itself, see PyTorch nn.Linear. The last line shows a rarer form, view(dtype), which reinterprets the same bytes as another type.
Top answer
1 of 2
25

Flattening is available in three forms in PyTorch

  • As a tensor method (oop style) torch.Tensor.flatten applied directly on a tensor: x.flatten().

  • As a function (functional form) torch.flatten applied as: torch.flatten(x).

  • As a module (layer nn.Module) nn.Flatten(). Generally used in a model definition.

All three are identical and share the same implementation, the only difference being nn.Flatten has start_dim set to 1 by default to avoid flattening the first axis (usually the batch axis). While the other two flatten from axis=0 to axis=-1 - i.e. the entire tensor - if no arguments are given.

2 of 2
6

You can think of the job of torch.flatten() as to simply doing a flattening operation of the tensor, without any strings attached. You give a tensor, it flattens, and returns it. That's all there to it.

On the contrary, nn.Flatten() is much more sophisticated (i.e., it's a neural net layer). Being object oriented, it inherits from nn.Module, although it internally uses the plain tensor.flatten() OP in the forward() method for flattening the tensor. You can think of it more like a syntactic sugar over torch.flatten().


Important difference: A notable distinction is that torch.flatten() always returns an 1D tensor as result, provided that the input is at least 1D or greater, whereas nn.Flatten() always returns a 2D tensor, provided that the input is at least 2D or greater (With 1D tensor as input, it will throw an IndexError).


Comparisons:

  • torch.flatten() is an API whereas nn.Flatten() is a neural net layer.

  • torch.flatten() is a python function whereas nn.Flatten() is a python class.

  • because of the above point, nn.Flatten() comes with lot of methods and attributes

  • torch.flatten() can be used in the wild (e.g., for simple tensor OPs) whereas nn.Flatten() is expected to be used in a nn.Sequential() block as one of the layers.

  • torch.flatten() has no information about the computation graph unless it is stuck into other graph-aware block (with tensor.requires_grad flag set to True) whereas nn.Flatten() is always being tracked by autograd.

  • torch.flatten() cannot accept and process (e.g., linear/conv1D) layers as inputs whereas nn.Flatten() is mostly used for processing these neural net layers.

  • both torch.flatten() and nn.Flatten() return views to input tensor. Thus, any modification to the result also affects the input tensor. (See the code below)


Code demo:

# input tensors to work with
In [109]: t1 = torch.arange(12).reshape(3, -1)
In [110]: t2 = torch.arange(12, 24).reshape(3, -1)
In [111]: t3 = torch.arange(12, 36).reshape(3, 2, -1)   # 3D tensor

Flattening with torch.flatten():

In [113]: t1flat = torch.flatten(t1)

In [114]: t1flat
Out[114]: tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

# modification to the flattened tensor    
In [115]: t1flat[-1] = -1

# input tensor is also modified; thus flattening is a view.
In [116]: t1
Out[116]: 
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, -1]])

Flattening with nn.Flatten():

In [123]: nnfl = nn.Flatten()
In [124]: t3flat = nnfl(t3)

# note that the result is 2D, as opposed to 1D with torch.flatten
In [125]: t3flat
Out[125]: 
tensor([[12, 13, 14, 15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24, 25, 26, 27],
        [28, 29, 30, 31, 32, 33, 34, 35]])

# modification to the result
In [126]: t3flat[-1, -1] = -1

# input tensor also modified. Thus, flattened result is a view.
In [127]: t3
Out[127]: 
tensor([[[12, 13, 14, 15],
         [16, 17, 18, 19]],

        [[20, 21, 22, 23],
         [24, 25, 26, 27]],

        [[28, 29, 30, 31],
         [32, 33, 34, -1]]])

tidbit: torch.flatten() is the precursor to nn.Flatten() and its brethren nn.Unflatten() since it existed from the very beginning. Then, there was a legitimate use-case for nn.Flatten(), since this is a common requirement for almost all ConvNets (just before the softmax or elsewhere). So it was added later on in the PR #22245.

There are also recent proposals to use nn.Flatten() in ResNets for model surgery.

🌐
EDUCBA
educba.com › home › software development › software development tutorials › pytorch tutorial › pytorch flatten
PyTorch Flatten | What is PyTorch Flatten along with Examples?
April 6, 2023 - Flatten does not copy the values as input, but it wraps the view function and uses reshape underneath the function. There are three methods in flattening the tensors using PyTorch.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python Guides
pythonguides.com › pytorch-flatten
How to Use PyTorch Flatten for Neural Network Models
June 19, 2025 - Before torch.nn.Flatten was introduced, I used to flatten tensors using the view() or reshape() methods:
🌐
Medium
mr-amit.medium.com › pytorch-view-vs-reshape-a-practical-guide-74bf31eec210
PyTorch view() vs reshape(): A Practical Guide | by It's Amit | Medium
March 6, 2025 - Using view() or reshape() appropriately ... rules for choosing between view() and reshape(): Stick to view() when your tensor is contiguous and you need maximum efficiency....
🌐
MyScale
myscale.com › blog › effortless-pytorch-flatten-guide-tensor-reshaping
Master PyTorch Flatten: Effortless Guide for Tensor Reshaping
April 24, 2024 - A common pitfall encountered by ... in PyTorch. While flatten collapses contiguous dimensions into a single axis, view offers more flexibility in reshaping without changing the underlying data layout....