What is torch cat

This recipe explains what is torch cat

Recipe Objective

What is torch.cat?

The function torch.cat in the given dimension of sequence tensors it will concatenate the given sequence. The shape should be same of all the tensors on which the function is going to be apply. The syntax is as follows:
torch.cat(tensors, dim=0, out=None)
where,
tensors - these are the python sequence of tensors of same shape and type, Non-empty tensors provided must have the same shape, except in the cat dimension.
dim - these are the dimensions over which the tensors are concatenated.
out - this is the output tensors.

Access Deep Learning Project to Build a Similar Images Finder in Python

Step 1 - Import library

import torch

Step 2 - Take Sample data

a = torch.randn(3,4)

Step 4 - Apply cat function

print("This is the cat function with dim is 0", torch.cat((a,a,a), 0), "\n")
print("This is the cat function with dim is 1", torch.cat((a,a,a), 1))

This is the cat function with dim is 0 tensor([[-2.0110,  1.2824, -1.1361,  0.8300],
        [-0.5585, -0.7940, -0.3821, -0.6289],
        [-0.3027,  0.0768, -1.5879,  1.3563],
        [-2.0110,  1.2824, -1.1361,  0.8300],
        [-0.5585, -0.7940, -0.3821, -0.6289],
        [-0.3027,  0.0768, -1.5879,  1.3563],
        [-2.0110,  1.2824, -1.1361,  0.8300],
        [-0.5585, -0.7940, -0.3821, -0.6289],
        [-0.3027,  0.0768, -1.5879,  1.3563]]) 

This is the cat function with dim is 1 tensor([[-2.0110,  1.2824, -1.1361,  0.8300, -2.0110,  1.2824, -1.1361,  0.8300,
         -2.0110,  1.2824, -1.1361,  0.8300],
        [-0.5585, -0.7940, -0.3821, -0.6289, -0.5585, -0.7940, -0.3821, -0.6289,
         -0.5585, -0.7940, -0.3821, -0.6289],
        [-0.3027,  0.0768, -1.5879,  1.3563, -0.3027,  0.0768, -1.5879,  1.3563,
         -0.3027,  0.0768, -1.5879,  1.3563]])

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

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.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks