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
🌐
DEV Community
dev.to › hyperkai › unflatten-in-pytorch-5933
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 my_tensor = torch.tensor([7, 1, -8, 3, -6, 0]) torch.unflatten(input=my_tensor, dim=0, sizes=(6,)) my_tensor.unflatten(dim=0, sizes=(6,)) torch.unflatten(input=my_tensor, dim=0, sizes=(-1,)) torch.unflatten(input=my_tensor, dim=-1, sizes=(6,)) torch.unflatten(input=my_tensor, dim=-1, sizes=(-1,)) # tensor([7, 1, -8, 3, -6, 0]) torch.unflatten(input=my_tensor, dim=0, sizes=(1, 6)) torch.unflatten(input=my_tensor, dim=0, sizes=(-1, 6)) torch.unflatten(input=my_tensor, dim=0, sizes=(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.
🌐
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 ...
🌐
PyTorch
docs.pytorch.org › reference api › torch.unflatten
torch.unflatten — PyTorch main documentation
January 1, 2023 - torch.unflatten(input, dim, sizes) → Tensor# Expands a dimension of the input tensor over multiple dimensions. See also · torch.flatten() the inverse of this function. It coalesces several dimensions into one.
🌐
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).
Find elsewhere
🌐
PyTorch
docs.pytorch.org › python api › torch.unflatten
torch.unflatten — PyTorch 2.8 documentation
torch.unflatten(input, dim, sizes) → Tensor# Expands a dimension of the input tensor over multiple dimensions. See also · torch.flatten() the inverse of this function. It coalesces several dimensions into one.
🌐
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.functional.unflatten.html
torchadf.nn.functional.unflatten — torch-adf 22.1.0 documentation
torchadf.nn.functional.unflatten · Edit on GitHub · torchadf.nn.functional.unflatten(in_mean, in_var, dim, unflattened_size, mode='diag') · Unflattens a dimension of the inputs over multiple dimensions. Assumed Density Filtering (ADF) version of the reverse reshaping operation corresponding ...
🌐
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
Unflattens a tensor dim expanding it to a desired shape. For use with [nn_sequential. ... 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
🌐
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)
🌐
PyTorch Forums
discuss.pytorch.org › t › how-to-flatten-and-then-unflatten-all-model-parameters › 34730
How to flatten and then unflatten _all_ model parameters - PyTorch Forums
January 15, 2019 - I’m trying to compute some metrics across all parameters of my model. While these metrics are simple (e.g. topk()) and available as methods on Tensor class I can’t use these methods directly because I get a list of tensors with different sizes via model.parameters(). So I have found that it’s easier to just flatten everything in model.parameter() list and then concatenate them into a single tensor and then compute the metric.
🌐
PyTorch Forums
discuss.pytorch.org › t › flatten-vs-unflatten-concept › 166989
Flatten vs unflatten concept - PyTorch Forums
November 26, 2022 - Hi I am testing a simple linear model. I am not posting a code as this more of conceptual question in PyTorch. I have 2D input with three features(columns), X, Y, Z with each 100 samples. I input in the model Linear1=3,50 Linear2=50,50 Linear3=50,1(output is 100x1 ) however if i flatten the ...