PyTorch .hypot()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 31, 2025

The torch.hypot function in PyTorch calculates the hypotenuse of right triangles, given the lengths of the two legs.

Element-wise, torch.hypot() computes:

$$ \text{out}_i = \sqrt{(\text{input}_i)^2 + (\text{other}_i)^2} $$

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours

Syntax

torch.hypot(input, other, *, out=None)

Parameters:

  • input: The first input tensor.
  • other: The second input tensor. This must be broadcastable with input.
  • out (Optional): The output tensor to store the result.

Return value:

Returns a tensor containing the element-wise Euclidean norm: $\sqrt{(\text{input}_i)^2 + (\text{other}_i)^2}$.

Example 1: Basic Element-Wise Hypotenuse

In this example, torch.hypot() calculates the hypotenuse for corresponding elements of two 1D tensors:

import torch
# Create input tensors
x = torch.tensor ([3.0, 5.0, 8.0])
y = torch.tensor ([4.0, 12.0, 15.0])
# Perform element-wise operation
hypotenuse = torch.hypot(x, y)
# Print the result
print(hypotenuse)

This code would output the following:

tensor([5., 13., 17.])

Example 2L 2D Distance Between Points

In this example, torch.hypot() calculates the distance from the origin for 2D points stored as x, y coordinates:

import torch
# For the following array:
points = torch.tensor([
[3.0, 4.0],
[5.0, 12.0],
[8.0, 15.0],
])
# Split into x and y columns:
x = points[:, 0]
y = points[:, 1]
distances = torch.hypot(x, y)
print(distances)

This will output:

tensor([ 5., 13., 17. ])

This example organizes the 6 x 1 tensor into x, y pairs, and calculates each one individually:

  • $\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt {25} = 5$
  • $\sqrt{5^2 + 12^2} = \sqrt{25 + 144} = \sqrt {169} = 13$
  • $\sqrt{8^2 + 15^2} = \sqrt{64 + 225} = \sqrt {289} = 17$

All contributors

Learn PyTorch on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
    • Intermediate.
      3 hours