Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.add

Adds other, scaled by alpha, to input elementwise. Tensor operands support broadcasting and the operation returns a tensor.

How it works

torch.add computes elementwise addition as input plus alpha multiplied by other. The operands may be tensors or a scalar, and tensor operands can be broadcast to a common shape; the optional out tensor receives the result.

Syntax

torch.add(input, other, *, alpha=1, out=None) → Tensor

Parameters

NameTypeDescription
inputTensorInput tensor.
otherTensor or NumberTensor or number added to input.
alphaNumberMultiplier applied to other before addition; defaults to 1.
outTensor, optionalOptional tensor that receives the output.

Returns

Tensor

Key points

  • The operation adds the scaled second operand to the first tensor.
  • Tensor operands can broadcast, and the operation supports integer, floating-point, and complex values.
  • other can be either another tensor or a numeric scalar.
  • alpha controls how strongly other contributes to the result.
  • out is an optional destination tensor for the computed values.

Common mistakes

  • Tensor operands may broadcast, but their values can also undergo type promotion; account for the resulting common shape and dtype.

Example: Scaled tensor addition

import torch

input = torch.tensor([1., 2., 3.])
result = torch.add(input, 5, alpha=2)
print(result)  # tensor([11., 12., 13.])

Adds twice the scalar 5 to every element of the input tensor.

When to use it

Scalar offset

Add a constant offset to every tensor element.

Broadcasted addition

Combine tensors whose dimensions are broadcast-compatible.

Scaled tensor contribution

Apply a weighted residual or correction term.

Interview questions

QWhat does torch.add compute?

It returns the elementwise expression input + alpha × other. other may be a tensor or number, and tensor operands support broadcasting.

Follow-up: How would you add a tensor with a different shape? Use shapes that are broadcast-compatible, such as a vector and a column vector.

QWhat is the purpose of alpha?

alpha scales other before addition, so alpha=3 computes input + 3 × other. Its default value is 1.

Follow-up: What does alpha=-1 do? It subtracts other from input because input + (-1) × other equals input - other.

QWhat is the out argument used for?

The function accepts an optional out tensor for storing the result. Without it, torch.add returns a result tensor directly.

Follow-up: What should be checked when supplying out? Ensure the destination tensor is suitable for the resulting shape and dtype.

  • torch.Tensor.add
  • torch.addmv
  • torch.foreach.add

Source: torch.add — PyTorch 2.14 documentation. Checked 2026-10-16.