If you really want a reshape layer, maybe you can wrap it into a nn.Module like this: import torch.nn as nn class Reshape(nn.Module): def __init__(self, *args): super(Reshape, self).__init__() self.shape = args def forward(self, x): return x.view(self.shape) Answer from allenye0119 on discuss.pytorch.org
🌐
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.
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

🌐
Sprintchase
sprintchase.com › home › torch.reshape(): complete guide
torch.reshape(): Complete Guide
May 12, 2025 - If you transform your tensor into a new shape, it will help you prepare your data for neural layers that expect a specific tensor shape. You can also flatten a tensor or adjust the dimensions for broadcasting. ... Let’s initialize a 1D tensor and reshape it into a 2D tensor. import torch tensor = torch.arange(8) print(tensor) # Output: [0, 1, 2, 3, 4, 5, 6, 7] reshaped_tensor = torch.reshape(tensor, (2, 4)) print(reshaped_tensor) # Output: # tensor([[0, 1, 2, 3], # [4, 5, 6, 7]])
🌐
Szymonmaszke
szymonmaszke.github.io › torchlayers › packages › torchlayers.html
torchlayers package — torchlayers documentation
Reshapes input torch.Tensor features while preserving batch dimension. Standard torch.reshape values (e.g. -1) are supported, e.g.: import torch import torchlayers as tl layer = tl.Reshape(20, -1) layer(torch.randn(64, 80)) # shape (64, 20, 4) All tensors must have the same shape (except in the concatenating dimension).
🌐
APXML
apxml.com › courses › getting-started-with-pytorch › chapter-2-advanced-tensor-manipulations › reshaping-rearranging-tensors
Reshape PyTorch Tensors | View, Permute
torch.Tensor.reshape, PyTorch Developers, 2024 (PyTorch Foundation) - Official documentation for the reshape() method, explaining its flexibility in reshaping both contiguous and non-contiguous tensors, and its behavior regarding data copying.
🌐
Readthedocs
nn.readthedocs.io › en › rtd › simple › index.html
Simple Layers - nn - Read the Docs
torch> x=torch.linspace(1,5,5) ... 5] torch> =o 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 [torch.DoubleTensor of dimension 3x5] module = Reshape(dimension1, dimension2, ......
🌐
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 › pytorch › vision › issues › 720
Reshape/View as a module? · Issue #720 · pytorch/vision
January 20, 2019 - Looking at the torchvision.models.resnet34 this is forward: class ResNet(nn.Module): def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.reshape(x.size(0), -1) x = self.fc(x) return x ·
Find elsewhere
🌐
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
🌐
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-in-custom-layers › 105920
Reshaping in custom layers - PyTorch Forums
December 12, 2020 - I’m implementing a custom layer that needs multiple reshaping of two input tensors, THE BATCH IS OF THE SHAPE R^(N, a,b,c), where N is the number of tensors in the batch. Actually I need to reshape each 3D tensor with the size of R^(a,b,c) to be a matrix of the size (a*b,c) and then use torch.mm to multiply them.
🌐
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...
🌐
CodeSignal
codesignal.com › learn › courses › introduction-to-pytorch-tensors › lessons › reshaping-and-flattening-tensors-in-pytorch
Reshaping and Flattening Tensors in PyTorch
# Reshape the tensor reshaped_tensor = tensor_a.view(3, 2) print(f"Reshaped Tensor:\n{reshaped_tensor}\n") ... Another common tensor operation is flattening, where a multi-dimensional tensor is transformed into a one-dimensional tensor. Think of it as unwrapping the elements of the tensor and aligning them in a single row. This operation is particularly useful in machine learning when you want to input multi-dimensional data into a layer that expects one-dimensional data.
🌐
CodingNomads
codingnomads.com › torch-reshape-methods
Torch Reshape Methods
Reshape tensors using the reshape method, which is particularly useful when you need to transform your data into a specific format, such as converting a flat tensor into an "image-like" tensor with height, width, and channel dimensions.
🌐
PyTorch Forums
discuss.pytorch.org › deployment
How to reshape last layer of pytorch CNN model while doing transfer learning - deployment - PyTorch Forums
December 1, 2019 - Actually i am trying to replicate keras structure to pytorch.(new in pytorch). Here is keras architecture base_model = InceptionV3(weights='imagenet', include_top=False) x = base_model.output x = Dense(512, activation='relu')(x) predictions = Dense(49*6,activation='sigmoid')(x) reshape=Reshape((49,6))(predictions) model = Model(inputs=base_model.input, outputs=reshape) for layer in base_model.layers: layer.trainable = False I want to reshape last layer of my netwrok.
🌐
Stack Overflow
stackoverflow.com › questions › 59129827 › how-to-reshape-last-layer-of-pytorch-cnn-model-while-doing-transfer-learning
keras - How to reshape last layer of pytorch CNN model while doing transfer learning - Stack Overflow
The easiest is probably just define another model which is an instance of CNN, maybe like model_cnn = CNN(num_ftrs) then pass the output of the inception network through this as follows...output = model_cnn(model(data)). You could also just wrap model and model_cnn in a single torch.nn.Sequential model. ... How to restructure the output tensor of a cnn layer for use by a linear layer in a simple pytorch model
🌐
Python Guides
pythonguides.com › pytorch-flatten
How to Use PyTorch Flatten for Neural Network Models
June 19, 2025 - PyTorch Flatten is a layer that reshapes a tensor by “flattening” it across specified dimensions. It takes a multi-dimensional input and converts it to a lower-dimensional form without changing the data.
🌐
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), ......
🌐
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)