The Wayback Machine - https://web.archive.org/web/20250118080340/https://www.geeksforgeeks.org/python-pytorch-log-method/
Open In App

Python – PyTorch log() method

Last Updated : 26 May, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

PyTorch torch.log() method gives a new tensor having the natural logarithm of the elements of input tensor.

Syntax: torch.log(input, out=None)

Arguments

  • input: This is input tensor.
  • out: The output tensor.

Return: It returns a Tensor.

Let’s see this concept with the help of few examples:
Example 1:




# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([5, 6, 7, 4])
print(a)
  
# Applying the log function and 
# storing the result in 'out'
out = torch.log(a)
print(out)


Output:

5
 6
 7
 4
[torch.FloatTensor of size 4]

 1.6094
 1.7918
 1.9459
 1.3863
[torch.FloatTensor of size 4]

Example 2:




# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([1.45, 2.3, 10])
print(a)
  
# Applying the log function and
# storing the result in 'out'
out = torch.log(a)
print(out)


Output:

 1.4500
  2.3000
 10.0000
[torch.FloatTensor of size 3]

 0.3716
 0.8329
 2.3026
[torch.FloatTensor of size 3]

Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg