๐ŸŽ“ A1Learner
๐Ÿ”

PyTorch Elementwise Operations

Elementwise operations run the same maths on every cell of a tensor. Each operator pairs cells at the same position, so + and * work cell by cell.

Add cell by cell

The + operator pairs cells at the same position.

a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([10.0, 20.0, 30.0])
print(a + b)

Multiply, divide, and raise to a power

Each operator works on the pair of cells at each position.

print(a * b)
print(b / a)
print(a ** 2)

The method names

add and mul are method names for + and *. They give the same results.

print(a.add(b))
print(a.mul(b))

One number for every cell

A plain number pairs with every cell.

prices = torch.tensor([10.0, 20.0, 30.0])
print(prices * 1.5)

Example

import torch

prices = torch.tensor([[10.0, 20.0, 5.0],
                       [4.0, 12.0, 8.0]])
tax = prices * 0.1
with_tax = prices + tax
print(with_tax)

Key points

  • Elementwise: each output cell comes from the cells at the same position in the inputs
  • + and -: add and subtract the cells at each position
  • * and /: multiply and divide the cells at each position
  • **: raises each cell to the given power
  • add and mul: method names that spell + and * and give the same results
  • A plain number pairs with every cell of the tensor

In this example, prices is a table of six prices in two rows. tax holds ten percent of each price, worked out cell by cell. Adding prices and tax gives each price with its tax included. Every cell was handled on its own, at its own position.

Note

Matrix multiplication uses a different operator, @.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

What is torch.tensor([2.0, 3.0]) * torch.tensor([4.0, 5.0])?