Skip to content

Commit 6c57034

Browse files
[Term Entry] PyTorch log() (#7614)
* docs: pytorch log * fix: example * fix: shell result * minor tweaks * Update content/pytorch/concepts/tensor-operations/terms/log/log.md ---------
1 parent 7f2a8a4 commit 6c57034

File tree

1 file changed

+54
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/log

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
Title: '.log()'
3+
Description: 'Returns a new tensor with the natural logarithm of each element in the input tensor.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Machine Learning'
7+
Tags:
8+
- 'Functions'
9+
- 'Python'
10+
- 'PyTorch'
11+
CatalogContent:
12+
- 'intro-to-py-torch-and-neural-networks'
13+
- 'paths/computer-science'
14+
---
15+
16+
In PyTorch, the **`.log()`** function computes the natural logarithm of each element in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). This is mathematically equivalent to applying the function $y_i = log_{e}(x_i)$ element-wise, where $log_{e}$ is the natural logarithm.
17+
18+
## Syntax
19+
20+
```pseudo
21+
torch.log(input, *, out=None) → Tensor
22+
```
23+
24+
**Parameters:**
25+
26+
- `input`: The input tensor containing elements for which the logarithm will be computed.
27+
- `out` (optional): Output tensor to store the result. Must have the same shape as `input`.
28+
29+
**Return value:**
30+
31+
Returns a new tensor where each element is the natural logarithm of the corresponding element within the input tensor.
32+
33+
## Example
34+
35+
The following example shows how to compute the element-wise natural logarithm of a tensor using `torch.log()`:
36+
37+
```py
38+
import torch
39+
import math
40+
41+
# Define a tensor
42+
x = torch.tensor([7.0 , 8.0 , 9.0 , math.log(3.)])
43+
44+
# Compute the natural logarithm
45+
result = torch.log(x)
46+
47+
print(result)
48+
```
49+
50+
Here is the output:
51+
52+
```shell
53+
tensor([1.9459, 2.0794, 2.1972, 0.0940])
54+
```

0 commit comments

Comments
 (0)