1. In nn.Sequential, torch.nn.Unflatten() can help you achieve reshape operation.

  2. For nn.Linear, its input shape is (N, *, H_{in}) and output shape is (H, *, H_{out}). Note that the feature dimension is last. So unsqueeze_noise() is not useful here.

  3. Based on the network structure, the arguments passed to make_gen_block are wrong.

I have checked the following code:

import torch
from torch import nn
class Generator(nn.Module):

   def __init__(self, z_dim=100, im_chan=1, hidden_dim=64, rdim=9216):
       
        super(Generator, self).__init__()
        self.z_dim = z_dim
        self.gen = nn.Sequential(
             nn.Linear(z_dim, rdim),
             nn.BatchNorm1d(rdim,momentum=0.9), # use BN1d
             nn.ReLU(inplace=True),
             nn.Unflatten(1, (256,6,6)), 
             self.make_gen_block(256, hidden_dim*2,kernel_size=2), # note arguments
             self.make_gen_block(hidden_dim*2,hidden_dim,kernel_size=2), # note kernel_size
             self.make_gen_block(hidden_dim,im_chan,kernel_size=2,final_layer=True), # note kernel_size
        )
   def make_gen_block(self, input_channels, output_channels, kernel_size=1, stride=2, final_layer=False):

        if not final_layer:
           return nn.Sequential(
              nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
              nn.BatchNorm2d(output_channels),
              nn.ReLU(inplace=True)
        )
        else:
              return nn.Sequential(
              nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
              nn.Tanh()
        )

   def forward(self, x):
       return self.gen(x)

def get_noise(n_samples, z_dim, device='cpu'):
    return torch.randn(n_samples, z_dim, device=device)

gen = Generator()
num_test = 100
input_noise = get_noise(num_test, gen.z_dim)
output = gen(input_noise)
assert output.shape == (num_test, 1, 48, 48)
Answer from ILS on Stack Overflow
Top answer
1 of 2
4
  1. In nn.Sequential, torch.nn.Unflatten() can help you achieve reshape operation.

  2. For nn.Linear, its input shape is (N, *, H_{in}) and output shape is (H, *, H_{out}). Note that the feature dimension is last. So unsqueeze_noise() is not useful here.

  3. Based on the network structure, the arguments passed to make_gen_block are wrong.

I have checked the following code:

import torch
from torch import nn
class Generator(nn.Module):

   def __init__(self, z_dim=100, im_chan=1, hidden_dim=64, rdim=9216):
       
        super(Generator, self).__init__()
        self.z_dim = z_dim
        self.gen = nn.Sequential(
             nn.Linear(z_dim, rdim),
             nn.BatchNorm1d(rdim,momentum=0.9), # use BN1d
             nn.ReLU(inplace=True),
             nn.Unflatten(1, (256,6,6)), 
             self.make_gen_block(256, hidden_dim*2,kernel_size=2), # note arguments
             self.make_gen_block(hidden_dim*2,hidden_dim,kernel_size=2), # note kernel_size
             self.make_gen_block(hidden_dim,im_chan,kernel_size=2,final_layer=True), # note kernel_size
        )
   def make_gen_block(self, input_channels, output_channels, kernel_size=1, stride=2, final_layer=False):

        if not final_layer:
           return nn.Sequential(
              nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
              nn.BatchNorm2d(output_channels),
              nn.ReLU(inplace=True)
        )
        else:
              return nn.Sequential(
              nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride),
              nn.Tanh()
        )

   def forward(self, x):
       return self.gen(x)

def get_noise(n_samples, z_dim, device='cpu'):
    return torch.randn(n_samples, z_dim, device=device)

gen = Generator()
num_test = 100
input_noise = get_noise(num_test, gen.z_dim)
output = gen(input_noise)
assert output.shape == (num_test, 1, 48, 48)
2 of 2
0

Instead of that you can do this LATENT_DIM = 100

class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        
        self.linear = nn.Linear(LATENT_DIM, 4*4*LATENT_DIM)
        self.active_0 = nn.ReLU()  
        
        self.conv_trans_1 = nn.ConvTranspose2d(LATENT_DIM, 512, kernel_size=4, stride=2, padding=1)
        self.norm_1 = nn.BatchNorm2d(512)
        self.active_1 = nn.LeakyReLU(0.2)
        
        self.conv_trans_2 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
        self.norm_2 = nn.BatchNorm2d(256)
        self.active_2 = nn.LeakyReLU(0.2)
        
        self.conv_trans_3 = nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1)
        self.norm_3 = nn.BatchNorm2d(128)
        self.active_3 = nn.LeakyReLU(0.2)
        
        self.conv_trans_4 = nn.ConvTranspose2d(128, 3, kernel_size=4, stride=2, padding=1)
        self.norm_4 = nn.BatchNorm2d(3)
        self.active_4 = nn.Tanh()
        
    def forward(self, inputs):
        x = self.linear(inputs)
        x = self.active_0(x)
        x = x.view(-1, Config.LATENT_DIM, 4, 4) ################
        
        x = self.conv_trans_1(x)
        x = self.norm_1(x)
        x = self.active_1(x)
        
        x = self.conv_trans_2(x)
        x = self.norm_2(x)
        x = self.active_2(x)
        
        x = self.conv_trans_3(x)
        x = self.norm_3(x)
        x = self.active_3(x)
        
        x = self.conv_trans_4(x)
        x = self.norm_4(x)
        x = self.active_4(x)
        
        return x

Summary of Model

🌐
PyTorch Forums
discuss.pytorch.org › uncategorized
What is reshape layer in pytorch? - Uncategorized - PyTorch Forums
March 16, 2017 - Hi all, What is the reshape layer in pytorch? In torch7 it seems to be nn.View, but what it is in pytorch? What I want is to add a reshpe layer in nn.Sequential. Thanks.
🌐
GitHub
github.com › torch › nn › blob › master › Reshape.lua
nn/Reshape.lua at master · torch/nn
local Reshape, parent = torch.class('nn.Reshape', 'nn.Module') · function Reshape:__init(...) parent.__init(self) local arg = {...} · self.size = torch.LongStorage() self.batchsize = torch.LongStorage() if torch.type(arg[#arg]) == 'boolean' then ·
Author: torch
🌐
PyTorch Forums
discuss.pytorch.org › t › add-reshape-nn-module › 53881
Add reshape nn.Module - PyTorch Forums
August 21, 2019 - Looking at the torchvision.mod... x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.reshape(x.size(0), ......
🌐
DeepLizard
deeplizard.com › learn › video › fCVuiW9AFzY
Flatten, Reshape, and Squeeze Explained - Tensors for Deep Learning with PyTorch - deeplizard
> print(t.reshape([1,12])) > print(t.reshape([1,12]).shape) tensor([[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.]]) torch.Size([1, 12]) > print(t.reshape([1,12]).squeeze()) > print(t.reshape([1,12]).squeeze().shape) tensor([1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.]) torch.Size([12]) > print(t.reshape([1,12]).squeeze().unsqueeze(dim=0)) > print(t.reshape([1,12]).squeeze().unsqueeze(dim=0).shape) tensor([[1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.]]) torch.Size([1, 12])
🌐
GitHub
github.com › torch › nn › blob › master › doc › simple.md
nn/doc/simple.md at master · torch/nn
When false, it forces the entire input (including the first dimension) to be reshaped to the input size. Default batchMode=nil, which means that the module considers inputs with more elements than the produce of provided sizes, i.e. dimension1xdimension2x..., to be batches. Example: > x = torch.Tensor(4,4) > for i = 1, 4 do > for j = 1, 4 do > x[i][j] = (i-1)*4+j > end > end > print(x) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [torch.Tensor of dimension 4x4] > print(nn.Reshape(2,8):forward(x)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [torch.Tensor of dimension 2x8] > print(nn.Reshape(8,2):forward(x
Author: torch
🌐
GitHub
github.com › pytorch › vision › issues › 720
Reshape/View as a module? · Issue #720 · pytorch/vision
January 20, 2019 - We have since then added a nn.Flatten module, which does the job of nn.Reshape for the particular case of converting from a convolution to a fc layer.
Find elsewhere
🌐
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 - No data was copied; instead, the reshaped tensor provides a new perspective on the same data. ... This might surprise you: if your tensor isn’t contiguous in memory, view() will raise an error. Let’s demonstrate: # Create a tensor and transpose it (makes it non-contiguous) tensor = torch.arange(6).reshape(2, 3) transposed = tensor.T # Transposing creates a non-contiguous tensor print("\nTransposed tensor (non-contiguous):") print(transposed) # Attempt to use view on the non-contiguous tensor try: reshaped_with_view = transposed.view(-1) except RuntimeError as e: print("\nError when trying to use view on a non-contiguous tensor:") print(f"{e}")
🌐
PyTorch Forums
discuss.pytorch.org › t › reshaping-torch-nn-parameter › 152025
Reshaping torch.nn.parameter - PyTorch Forums
May 18, 2022 - I’m writing a class that is basically just nn.Linear except it has some reshaping. class myLinear2(nn.Module): def __init__(self, in_features, out_features): super().__init__() self.in_features = in_features self.out_features = out_features self.weight = torch.nn.Parameter(torch.randn(out_features, in_features)) self.bias = torch.nn.Parameter(torch.randn(out_features)) def forward(self, input): self.weight = self.weight.reshape(-1, 1, self.weight.shape[-1...
🌐
Python Guides
pythonguides.com › pytorch-reshape-tensor
How To Reshape A Tensor In PyTorch? - Python Guides
June 18, 2025 - Reshaped tensor (3x4): tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) New shape: torch.Size([3, 4]) I executed the above example code and added the screenshot below. The reshape() method is flexible and allows you to use -1 as a dimension, which tells PyTorch to automatically calculate ...
🌐
GitHub
github.com › dusty-nv › pytorch-classification › blob › master › reshape.py
pytorch-classification/reshape.py at master · dusty-nv/pytorch-classification
import torch.nn · · # # reshape the model for N classes · # def reshape_model(model, arch, num_classes): """Reshape a model's output layers for the given number of classes""" · # reshape output layers for the dataset · if arch.startswith("resnet"): model.fc = torch.nn.Linear(model.fc.in_features, num_classes) print("=> reshaped ResNet fully-connected layer with: " + str(model.fc)) ·
Author: dusty-nv
🌐
GeeksforGeeks
geeksforgeeks.org › reshaping-a-tensor-in-pytorch
Reshaping a Tensor in Pytorch - GeeksforGeeks
September 1, 2021 - Example 1: Python program to reshape a 1 D tensor to a two-dimensional tensor. ... # import torch module import torch # create an 1 D etnsor with 8 elements a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]) # display tensor shape print(a.shape) # display actual tensor print(a) # reshape tensor into 4 rows and 2 columns print(a.reshape([4, 2])) # display shape of reshaped tensor print(a.shape)
🌐
Readthedocs
nn.readthedocs.io › en › readthedocs › simple › index.html
Simple Layers - nn - Read the Docs
When false, it forces the entire input (including the first dimension) to be reshaped to the input size. Default batchMode=nil, which means that the module considers inputs with more elements than the produce of provided sizes, i.e. dimension1xdimension2x..., to be batches. Example: > x = torch.Tensor(4,4) > for i = 1, 4 do > for j = 1, 4 do > x[i][j] = (i-1)*4+j > end > end > print(x) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [torch.Tensor of dimension 4x4] > print(nn.Reshape(2,8):forward(x)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [torch.Tensor of dimension 2x8] > print(nn.Reshape(8,2):forward(x
🌐
Sprintchase
sprintchase.com › home › torch.reshape(): complete guide
torch.reshape(): Complete Guide
May 12, 2025 - import torch tensor = torch.randn(2, 4) print(tensor.shape) # Output: torch.Size([2, 4]) flattened_tensor = torch.reshape(tensor, (-1,)) print(flattened_tensor.shape) # Output: torch.Size([8])
🌐
Szymonmaszke
szymonmaszke.github.io › torchlayers › packages › torchlayers.html
torchlayers package — torchlayers documentation
... Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them. ... Reshapes input torch.Tensor features while ...
🌐
ProjectPro
projectpro.io › recipes › reshape-torch-tensor
How to reshape a torch tensor -
September 13, 2022 - reshape = torch.reshape(data, (4,2)) print("This is the reshaped tensor:", "\n",reshape, reshape.shape)
🌐
PyTorch Forums
discuss.pytorch.org › t › how-to-reshape-the-output-of-a-linear-layer-in-pytorch › 164057
How to reshape the output of a Linear layer in Pytorch? - PyTorch Forums
October 21, 2022 - Hi, I’m implementing Generator of a GAN and I need to reshape output of Linear Layer to particular dimension, here is the image of the Generator. Here is my code of Generator: class Generator(nn.Module): def __init__(self, z_dim=100, im_chan=1, hidden_dim=64, rdim=9216): super(Generator, self).__init__() self.z_dim = z_dim self.gen = nn.Sequential( nn.Linear(z_dim, rdim), nn.BatchNorm2d(rdim,momentum=0.9), nn.ReLU(i...
🌐
YouTube
youtube.com › watch
Tensor Operations - Reshape and Slice - Deep Learning with PyTorch 3 - YouTube
In this video we'll look at some basic Tensor Operations for Deep Learning with Pytorch.We'll look at Reshape and View, as well as grabbing individual items ...
Published: May 15, 2023