How To Find The Shape Of Torch Tensor?

This beginner-friendly Pytorch code shows you how to find the shape of a torch tensor using the .shape() attribute.

Objective: How To Find The Shape Of Torch Tensor?

This PyTorch code example will teach you how to find shape of torch tensor using the ‘.shape()’ attribute. 

How To Find The Shape Of A Torch Tensor?

You can find the shape of a Torch tensor using the .shape attribute. This attribute returns a ‘torch.Size’ object, a tuple containing each tensor dimension's size. You can also use the .size() method to get the shape of a tensor. The .size() method returns the same torch.Size object as the .shape attribute.

Steps Showing How To Find The Shape Of Torch Tensor

The following steps will show you how to find the shape of a torch tensor using the ‘.shape()’ function, which will return the size of the particular torch tensor.

Step 1 - Import Library

First, you must import the required libraries.

import torch

Get Closer To Your Dream of Becoming a Data Scientist with Solved End-to-End PyTorch Projects

Step 2 - Take Sample Tensor

The next step is to take any sample tensor.

Sample_tensor = torch.tensor([[[33,44,55],[55,66,77],[99,77,22]]])

Step 3 - Torch: Get Shape Of Tensor

The final step is to print the shape of the sample tensor using the .shape() attribute.

print("This is the shape of torch tensor:",Sample_tensor.shape)

The output of the above code is-

This is the shape of torch tensor: torch.Size([1, 3, 3])

This means that the sample tensor has a shape of (1, 3, 3), which means that it has 3 dimensions-

  • The first dimension has 1 element, which is the number of batches.

  • The second dimension has 3 elements, which is the height of the tensor.

  • The third dimension has 3 elements, which is the width of the tensor.

How To Use View () Tensor In The Shape Of Another Tensor Torch?

The view() method in PyTorch allows you to change the shape of a tensor without changing its underlying data. You can view a tensor in the shape of another tensor by passing the desired shape to the view() method. For example, the following code reshapes a 2D tensor to a 1D tensor-

import torch

tensor = torch.randn(2, 3)

# Reshape the tensor to a 1D tensor

reshaped_tensor = tensor.view(-1)

# Print the shape of the reshaped tensor

print(reshaped_tensor.shape)

The output of the above code is-

torch.Size([6])

Learn How To Find Shape Of Torch Tensor Like A Pro With ProjectPro

This step-by-step PyTorch code example has helped you gain valuable insights into working with Torch tensors in PyTorch, focusing on determining the shape of tensors and reshaping them using view(). We have covered the essential steps for understanding a tensor's shape and manipulating it to match the shape of another tensor, which is crucial for data manipulation and model compatibility. Furthermore, if you want to expand your proficiency in PyTorch and apply it to real-world data science and machine learning solutions, you must explore the ProjectPro platform. By engaging with over 270 end-to-end solved projects in the ProjectPro repository, you can gain the skills and expertise needed to excel in data science and machine learning.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.