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

PyTorch · Function

torch.sqrt

torch.sqrt returns a new tensor containing the element-wise square roots of an input tensor. An optional out tensor can receive the result.

How it works

torch.sqrt computes the square root independently for every element in the input tensor and returns the resulting tensor. You can optionally provide an output tensor through the out keyword argument. For negative inputs, the documented example shows nan values in the corresponding output positions.

Syntax

torch.sqrt(input, *, out=None) → Tensor

Parameters

NameTypeDescription
inputTensorThe input tensor.
outTensor, optionalOptional output tensor.

Returns

A new Tensor containing the element-wise square roots of input.

Key points

  • The function returns a new tensor whose values are the element-wise square roots of input.
  • The real signature takes one input tensor and an optional keyword-only out tensor.
  • input is the tensor whose elements are transformed.
  • out is an optional tensor that can receive the computed result.

Common mistakes

  • Negative input elements produce nan values, as illustrated by the official example.

Example: Element-wise square root

import torch

a = torch.tensor([0.0, 1.0, 4.0, 9.0])
result = torch.sqrt(a)
print(result)

Computes the element-wise square roots of a tensor.

When to use it

Element-wise mathematical transformation

Transforming nonnegative tensor data into magnitudes or scale values.

Neural-network and scientific calculations

Computing square-root terms in tensor-based formulas.

Optional output storage

Writing results into an existing tensor when an output buffer is appropriate.

Interview questions

QWhat does torch.sqrt compute?

torch.sqrt accepts a Tensor and returns a new Tensor containing the element-wise square roots. Its signature also allows an optional out tensor.

Follow-up: How does it operate on a multidimensional tensor? It applies the same element-wise transformation independently across all tensor elements.

QWhat are torch.sqrt's parameters?

The required input parameter is input, which must be a Tensor. The optional keyword-only out parameter specifies the output tensor.

Follow-up: What should be checked when supplying out? Ensure the supplied tensor is suitable for receiving the result and has compatible shape and type.

QDoes torch.sqrt modify its input?

torch.sqrt is not an in-place operation: the documentation describes it as returning a new tensor. PyTorch separately documents torch.sqrt_ as the in-place version.

Follow-up: When would the in-place variant be risky? It overwrites the original values, so later computations that need them may be affected.

  • torch.sqrt_
  • torch.Tensor.sqrt_
  • torch.rsqrt

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