Here is example for your question.

import torch

input = torch.randn([2,48,196])
unflatten = torch.nn.Unflatten(2, (14,14))
output = unflatten(input)

If you check output.shape, the shape is [2,48,14,14].

Unflatten function is to expand specific dim to a desired shape. In your case, you want to expand the shape 196 in "dim 2" to new shape of the unflatten dimension "(14,14)".

There are two parameters in Unflatten function.

  1. First parameter is dim. it is specific dimension which you want to be unflatten. In your case, it is 2.
  2. Second parameter is unflatten_size. It is the new shape of the unflatten dimension of the tensor. So it is (14,14).

Therefore, your Unflatten function should be looked like unflatten = torch.nn.Unflatten(2, (14,14))

Answer from zerg468 on Stack Overflow
🌐
GitHub
alband.github.io › doc_view › generated › torch.nn.Unflatten.html
Unflatten — PyTorch master documentation
Applies fn recursively to every submodule (as returned by .children()) as well as self. Typical use includes initializing the parameters of a model (see also torch.nn.init).
🌐
GitHub
github.com › pytorch › pytorch › issues › 44871
module 'torch.nn' has no attribute 'Unflatten' · Issue #44871 · pytorch/pytorch
September 17, 2020 - torch.nn.Unflatten (Python class, in Unflatten) torch.Tensor.unflatten (Python method, in Named Tensors) However, when i try to use the the example code i have these respectively errors: module 'torch.nn' has no attribute 'Unflatten' zip argument Matrix multiplication operator #1 must support iteration ·
🌐
Readthedocs
torch-adf.readthedocs.io › en › stable › api › torchadf.nn.modules.flatten.Unflatten.html
torchadf.nn.modules.flatten.Unflatten — torch-adf 22.1.0 documentation
Unflattens a dimension of the input over multiple dimensions. Assumed Density Filtering (ADF) version of torch.nn.Unflatten. The dimension to be unflattened and target shape refer to the first input (mean) Tensor. Respective dimensions for the second input (covariance) Tensor are inferred according ...
🌐
DEV Community
dev.to › hyperkai › unflatten-in-pytorch-3i7g
Unflatten in PyTorch - DEV Community
November 6, 2024 - Basically, Unflatten() is used to define a model while unflatten() is not used to define a model. import torch from torch import nn unflatten = nn.Unflatten() unflatten # Unflatten(dim=0, unflattened_size=(6,)) unflatten.dim # 0 unflatten.u...
🌐
GitHub
github.com › pytorch › pytorch › blob › main › torch › nn › modules › flatten.py
pytorch/torch/nn/modules/flatten.py at main · pytorch/pytorch
For use with :class:`~nn.Sequential`. · * :attr:`dim` specifies the dimension of the input tensor to be unflattened. · * :attr:`unflattened_size` is the new shape of the unflattened dimension of the tensor and it can be · a `tuple` of ints or a `list` of ints or `torch.Size` for `Tensor` input.
🌐
Torch for R
torch.mlverse.org › docs › reference › nn_unflatten
Unflattens a tensor dim expanding it to a desired shape. For use with [nn_sequential. — nn_unflatten • torch
nn_unflatten(dim, unflattened_size) dim · Dimension to be unflattened · unflattened_size · New shape of the unflattened dimension · if (torch_is_installed()) { input <- torch_randn(2, 50) m <- nn_sequential( nn_linear(50, 50), nn_unflatten(2, c(2, 5, 5)) ) output <- m(input) output$size() } #> [1] 2 2 5 5
Find elsewhere
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.

🌐
DEV Community
dev.to › hyperkai › flatten-in-pytorch-3bl0
Flatten in PyTorch - DEV Community
November 6, 2024 - import torch from torch import nn flatten = nn.Flatten() flatten # Flatten(start_dim=1, end_dim=-1) flatten.start_dim # 1 flatten.end_dim # -1 my_tensor = torch.tensor(7) flatten = nn.Flatten(start_dim=0, end_dim=0) flatten = nn.Flatten(start_dim=0, end_dim=-1) flatten = nn.Flatten(start_dim=-1, end_dim=0) flatten = nn.Flatten(start_dim=-1, end_dim=-1) flatten(input=my_tensor) # tensor([7]) my_tensor = torch.tensor([7, 1, -8, 3, -6, 0]) flatten = nn.Flatten(start_dim=0, end_dim=0) flatten = nn.Flatten(start_dim=0, end_dim=-1) flatten = nn.Flatten(start_dim=-1, end_dim=0) flatten = nn.Flatten(s
🌐
YouTube
youtube.com › watch
Tensor Unfolding, Folding in PyTorch - .unflatten(), torch.nn.Unfold(),torch.nn.fold()| Ali Hassan - YouTube
In this complete guide, we explore tensor unfolding and folding operations in PyTorch, including .unfold(), .unflatten(), torch.nn.Unfold(), and torch.nn.Fol...
Published: July 24, 2025
🌐
GitHub
github.com › pytorch › pytorch › issues › 49742
Error when running nn.Unflatten with TorchScript · Issue #49742 · pytorch/pytorch
December 22, 2020 - 🐛 Bug To Reproduce If you run import torch import torch.nn as nn s = nn.Unflatten(1, (5, 5)) s.forward(torch.rand((5, 25))) # Works Fine jit = torch.jit.script(s) jit.forward(torch.rand((5, 25))) B...
Author: pytorch
🌐
Fedorkobak
fedorkobak.github.io › python › torch › layers › flatten_unflatten.html
Flatten/unflatten — Python
Applying Unflatten to the inner dimension of the input splits rows into subarrays and arranges them as new rows inside the matrices. torch.nn.Unflatten(dim=1, unflattened_size=(3,3))(input)
🌐
GitHub
github.com › pytorch › pytorch › issues › 49743
Allow `nn.Unflatten` to take a `Sequence[int]` instead of ...
December 22, 2020 - Should be easy to fixmodule: nnRelated to torch.nnRelated to torch.nntriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module ... >>> nn.Unflatten(1, [5, 5]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-b881e5cda02d> in <module> ----> 1 nn.Unflatten(1, [5, 5]) /opt/anaconda/envs/ctrldev/lib/python3.6/site-packages/torch/nn/modules/flatten.py in __init__(sel
Author: pytorch
🌐
Torch for R
torch.mlverse.org › docs › reference › nn_flatten
Flattens a contiguous range of dims into a tensor. — nn_flatten • torch
nn_unflatten · if (torch_is_installed()) { input <- torch_randn(32, 1, 5, 5) m <- nn_flatten() m(input) } #> torch_tensor #> Columns 1 to 6 1.4806e-01 1.2782e-01 -1.3181e+00 -1.6748e-01 1.5760e+00 -8.1000e-01 #> -1.2938e+00 -4.6650e-01 -2.3092e+00 -4.5060e-01 2.5774e-01 2.4798e+00 #> 1.5889e+00 5.6571e-01 -1.0141e+00 -2.3078e+00 1.5629e-01 -1.0670e+00 #> 9.2378e-01 -8.1461e-01 -4.6645e-01 1.3164e+00 -4.3752e-01 -4.4730e-01 #> 9.8056e-01 6.2474e-01 -8.2288e-01 -5.6566e-02 1.5581e-01 -3.0590e-01 #> -1.1376e+00 -1.6929e-02 6.5225e-01 1.2261e+00 3.4274e-02 6.0990e-01 #> -2.5636e-01 -1.0151e+00 1.
🌐
Sprintchase
sprintchase.com › home › torch.unflatten(): expand a dimension of the input tensor
torch.unflatten(): Expand a Dimension of the Input Tensor
July 26, 2025 - The torch.unflatten() method expands or reshapes a single dimension of a tensor into multiple dimensions.
🌐
DeepLizard
deeplizard.com › learn › video › fCVuiW9AFzY
Flatten, Reshape, and Squeeze Explained - Tensors for Deep Learning with PyTorch - deeplizard
> t = torch.ones(4, 3) > t tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) > flatten(t) tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
🌐
GitHub
github.com › pytorch › pytorch › issues › 49538
jit tracer doesn't work with unflatten layer · Issue #49538 · pytorch/pytorch
December 17, 2020 - 🐛 Bug To Reproduce Steps to reproduce the behavior: Run the following code: import torch import torch.nn import torch.onnx model = torch.nn.Sequential( torch.nn.Unflatten(1, (1, 10)), ) # Create th...
Author: pytorch