Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to perform element-wise division on tensors in PyTorch?
Next article icon

How to perform element-wise multiplication on tensors in PyTorch?

Last Updated : 02 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see how to perform element-wise multiplication on tensors in PyTorch in Python. We can perform element-wise addition using torch.mul() method. 

This function also allows us to perform multiplication on the same or different dimensions of tensors. If tensors are different in dimensions so it will return the higher dimension tensor. we can also multiply a scalar quantity with a tensor using torch.mul() function.  

Syntax: torch.mul(input, other, *, out=None)

Parameters:

  • input: This is input tensor.
  • other: The value or tensor that is to be multiply to every element of tensor.
  • out: it is the output tensor, This is optional parameter.

Return: returns a new modified tensor..

Example 1: 

The following program is to perform multiplication on two single dimension tensors.

Python3
# import torch library import torch  # define two tensors tens_1 = torch.Tensor([1, 2, 3, 4, 5]) tens_2 = torch.Tensor([10, 20, 30, 40, 50])  # display tensors print(" First Tensor: ", tens_1) print(" Second Tensor: ", tens_2)  # multiply tensors tens = torch.mul(tens_1, tens_2)  # display result after perform element wise multiplication print(" After Element-wise multiplication: ", tens) 

Output:

 First Tensor:  tensor([1., 2., 3., 4., 5.])

 Second Tensor:  tensor([10., 20., 30., 40., 50.])

 After Element-wise multiplication:  tensor([ 10.,  40.,  90., 160., 250.])

Example 2: 

The following program is to know how to multiply a scalar quantity to a tensor.

Python3
# import torch library import torch  # define a tensors tens_1 = torch.Tensor([100, 200, 300, 400, 500])  # display tensor print(" First Tensor: ", tens_1)  # multiply a scalar tensors tens = torch.mul(tens_1, 2)  # display result after perform element wise multiplication print(" After multiply 2 in tensor: ", tens) 

Output:

 First Tensor:  tensor([100., 200., 300., 400., 500.])

 After multiply 2 in tensor:  tensor([ 200.,  400.,  600.,  800., 1000.])

Example 3: 

The following program is to perform elements-wise multiplication on 2D tensors.

Python3
# import torch import torch  # Define two 2D tensors tens_1 = torch.Tensor([[10, 20], [30, 40]]) tens_2 = torch.Tensor([[1, 2], [3, 4]])  # display tensors print(" First tensor:  ", tens_1) print(" Second tensor:  ", tens_2)  # Multiply above two 2-D tensors tens = torch.mul(tens_1, tens_2) print(" After multiply 2D tensors: ", tens) 

Output:

First tensor:   tensor([[10., 20.],[30., 40.]])  Second tensor:   tensor([[1., 2.],[3., 4.]])  After multiply 2D tensors:  tensor([[ 10.,  40.],[ 90., 160.]])

Example 4:

The following program is to shows how to perform elements-wise multiplication on two different dimension tensors.

Python3
# import torch import torch  # Define two 2D tensors tens_1 = torch.Tensor([[10, 20], [30, 40]]) tens_2 = torch.Tensor([2, 4])  # display tensors print(" 2D tensor: ", tens_1) print(" 1D tensor:  ", tens_2)  # Multiply above two 2-D tensors tens = torch.mul(tens_1, tens_2) print(" After multiply tensors: ", tens) 

Output:

 2D tensor:  tensor([[10., 20.],         [30., 40.]])  1D tensor:   tensor([2., 4.])  After multiply tensors:  tensor([[ 20.,  80.],         [ 60., 160.]])

Next Article
How to perform element-wise division on tensors in PyTorch?
author
mukulsomukesh
Improve
Article Tags :
  • Python
  • Python-PyTorch
Practice Tags :
  • python

Similar Reads

  • How to perform element-wise addition on tensors in PyTorch?
    In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function.  This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in di
    3 min read
  • How to perform element-wise division on tensors in PyTorch?
    In this article, we will understand how to perform element-wise division of two tensors in PyTorch. To perform the element-wise division of tensors, we can apply the torch.div() method. It takes two tensors (dividend and divisor) as the inputs and returns a new tensor with the element-wise division
    3 min read
  • How to perform element-wise subtraction on tensors in PyTorch?
    In this article, we are going to understand how to perform element-wise subtraction on tensors in PyTorch in Python. We can perform element-wise subtraction using torch.sub() method. torch.sub() method allows us to perform subtraction on the same or different dimensions of tensors. It takes two tens
    3 min read
  • Performing Batch Multiplication in PyTorch Without Using torch.bmm
    Batch multiplication is a fundamental operation in deep learning and scientific computing, especially when working with large datasets and models. PyTorch, a popular deep learning framework, provides several methods for matrix multiplication, including torch.bmm for batch matrix multiplication. Howe
    5 min read
  • How to compute element-wise entropy of an input tensor in PyTorch
    In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a
    2 min read
  • How to compute element-wise remainder of given input tensor in PyTorch?
    In this article, we are going to see how to compute the element-wise remainder in PyTorch. we have two methods to compute element-wise reminders one is torch.remainder() and the other one is torch.fmod() let's go discuss both of them one by one. torch.remainder() method  The PyTorch remainder() meth
    3 min read
  • How to compute the element-wise angle of given input tensor in PyTorch?
    In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl
    3 min read
  • NumPy ndarray.__mul__() Method | Element Wise Multiplication of Array
    The ndarray.__mul__() method does an element-wise multiplication of NumPy ndarray to a particular value that is provided as the parameter. Example C/C++ Code import numpy as np gfg = np.array([1, 2.5, 3, 4.8, 5]) print(gfg.__mul__(5)) Output[ 5. 12.5 15. 24. 25. ] SyntaxSyntax: ndarray.__mul__($self
    1 min read
  • How To Sort The Elements of a Tensor in PyTorch?
    In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method.  We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=Fals
    3 min read
  • How to Perform in-place Operations in PyTorch?
    In this article, we will see different in-place operations performed on tensors in PyTorch. Inplace operations are used to directly alter the values of a tensor. The data collected from the user will not be copied. The fundamental benefit of adopting these procedures is that they reduce memory stora
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences