- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.3k
[Term Entry] PyTorch Tensor Operations: .hypot() #7807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
      
        
          +98
        
        
          −0
        
        
          
        
      
    
  
  
     Merged
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      2fee9fe
              
                created hypot directory and md file
              
              
                luismonzonm 6c420d7
              
                created first edition of hypot.md awaiting revission
              
              
                luismonzonm e97ec44
              
                Revise .hypot() documentation for clarity and accuracy
              
              
                mamtawardhani da0ce36
              
                Merge branch 'main' into PyTorch_hypot
              
              
                mamtawardhani 538828f
              
                Apply suggestion from @avdhoottt
              
              
                avdhoottt 143f68b
              
                Merge branch 'main' into PyTorch_hypot
              
              
                avdhoottt a01ba6e
              
                Lint + Format
              
              
                avdhoottt 039b8bf
              
                Merge branch 'main' into PyTorch_hypot
              
              
                avdhoottt bfd7ab5
              
                Lint Issue
              
              
                avdhoottt File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            98 changes: 98 additions & 0 deletions
          
          98 
        
  content/pytorch/concepts/tensor-operations/terms/hypot/hypot.md
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --- | ||
| Title: '.hypot()' | ||
| Description: 'Calculates the hypotenuse of a right triangle given the lengths of its two legs.' | ||
| Subjects: | ||
| - 'AI' | ||
| - 'Computer Science' | ||
| - 'Data Science' | ||
| Tags: | ||
| - 'AI' | ||
| - 'Functions' | ||
| - 'Pytorch' | ||
| - 'Trigonometry' | ||
| CatalogContent: | ||
| - 'intro-to-py-torch-and-neural-networks' | ||
| - 'paths/data-science' | ||
| --- | ||
|  | ||
| 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} | ||
| $$ | ||
|  | ||
| ## Syntax | ||
|  | ||
| ```pseudo | ||
| 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: | ||
|  | ||
| ```py | ||
| 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: | ||
|  | ||
| ```shell | ||
| 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: | ||
|  | ||
| ```py | ||
| 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: | ||
|  | ||
| ```shell | ||
| 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$ | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.