How To Add PyTorch Tensors?

This beginner-friendly Pytorch code shows you how to add PyTorch tensors using the torch.sum() function.

Objective: How To Add PyTorch Tensors?

This PyTorch code example will teach you to perform PyTorch add two tensors using the ‘torch.sum()’ function.

Steps Showing How To Add Two Tensors in PyTorch

The following steps will show you how to add two PyTorch tensors using the torch.sum() function that will return the sum of the two input tensors.

Step 1 - Import Library To Add Tensors PyTorch

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 for addition.

Sample = torch.tensor([4,5])

Step 3 - Add Two Tensors PyTorch

The final step is to add 2 tensors PyTorch using the torch.sum() function.

add = torch.sum(Sample)

print("This is the addition of two tensors:",add)

The output of the above code is-

This is the addition of two tensors: tensor(9)

PyTorch- Add Tensors Of Different Shape

You can add tensors of different shapes in PyTorch using the torch.add() function. This function supports broadcasting, which allows it to add tensors of different shapes as long as they have compatible dimensions.

import torch

# Create two tensors of different shapes

tensor1 = torch.tensor([1, 2, 3])

tensor2 = torch.tensor([4, 5, 6])

# Add the two tensors

result = torch.add(tensor1, tensor2)

# Print the result

print(result)

The output of the above code is-

tensor([5, 7, 9])

Learn How To Add Pytorch Tensors Like A Pro With ProjectPro

This beginner-friendly PyTorch code example provided a clear and practical guide on adding two PyTorch tensors and tensors of different shapes. The ability to manipulate tensors is fundamental in PyTorch and crucial for various data science and machine learning tasks. 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 ProjectPro. 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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

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

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.