What does squeeze function do in torch

This recipe explains what does squeeze function do in torch

Recipe Objective

What does squeeze function do in torch?

This is achieved by using torch.squeeze(input, dim, out) which will return a tensor, with all the dimensions of input of size 1 removed. For e.g, if the input tensor is of shape A×1×B×C×1×D then the out tensor will be of shape A×B×C×D. Lets understand this with practical implementation.

Get Access to Plant Species Identification Project using Machine Learning

Step 1 - Import library

import torch

Step 2 - Take Sample data

Sample = torch.ones(2,1,2,1,2,3)
Sample.size() torch.Size([2, 1, 2, 1, 2, 3])

Step 3 - Squeeze the tensors

print("Squeezing the tensor with no dimension:", torch.squeeze(Sample).size())
print("Squeezing the tensor with 0 dimension:", torch.squeeze(Sample, 0).size())
print("Squeezing the tensor with 1 dimension:", torch.squeeze(Sample, 1).size())

Squeezing the tensor with no dimension: torch.Size([2, 2, 2, 3])
Squeezing the tensor with 0 dimension: torch.Size([2, 1, 2, 1, 2, 3])
Squeezing the tensor with 1 dimension: torch.Size([2, 2, 1, 2, 3])

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python