Element-wise mathematical transformation
Transforming nonnegative tensor data into magnitudes or scale values.
PyTorch · Function
torch.sqrt returns a new tensor containing the element-wise square roots of an input tensor. An optional out tensor can receive the result.
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.
torch.sqrt(input, *, out=None) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | The input tensor. |
out | Tensor, optional | Optional output tensor. |
A new Tensor containing the element-wise square roots of input.
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.
Transforming nonnegative tensor data into magnitudes or scale values.
Computing square-root terms in tensor-based formulas.
Writing results into an existing tensor when an output buffer is appropriate.
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.
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.
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