Skip to content

Commit 4908b6d

Browse files
authored
[Term Entry] PyTorch Tensor Operations: .log1p()
* Create log1p entry for pytorch * Update log1p.md * minor content fixes * Minor changes ---------
1 parent 62ef4ad commit 4908b6d

File tree

1 file changed

+58
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/log1p

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
Title: '.log1p()'
3+
Description: 'Computes the natural logarithm of one plus the input tensor element-wise.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data Structures'
9+
- 'Functions'
10+
- 'Index'
11+
- 'Values'
12+
CatalogContent:
13+
- 'intro-to-py-torch-and-neural-networks'
14+
- 'paths/computer-science'
15+
---
16+
17+
In PyTorch, **`.log1p()`** computes the natural logarithm of one plus the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) element-wise.
18+
19+
This function improves numerical stability for small input values, avoiding issues that can occur when computing the logarithm directly.
20+
21+
## Syntax
22+
23+
```pseudo
24+
torch.log1p(input, *, out=None)
25+
```
26+
27+
**Parameters:**
28+
29+
- `input`: The input tensor for which the natural logarithm of one plus the elements will be computed.
30+
- `out` (Optional): A tensor to store the result. If provided, the computed values will be written to this tensor instead of creating a new one.
31+
32+
**Return value:**
33+
34+
Returns a new tensor where each element is the natural logarithm of one plus the corresponding element in the input tensor. The original tensor remains unchanged unless an `out` tensor is provided.
35+
36+
## Example
37+
38+
In this example, `.log1p()` computes the natural logarithm of one plus each element in a tensor:
39+
40+
```py
41+
import torch
42+
43+
# Create a tensor
44+
x = torch.tensor([0.0, 1.0, 2.0])
45+
46+
# Compute log1p
47+
y = torch.log1p(x)
48+
49+
print(y)
50+
```
51+
52+
The code above generates the output as follows:
53+
54+
```shell
55+
tensor([0.0000, 0.6931, 1.0986])
56+
```
57+
58+
In this example, the input tensor `x` contains the values `[0.0, 1.0, 2.0]`. The `log1p()` function computes the natural logarithm of one plus each element in the tensor, resulting in the output tensor `[0.0000, 0.6931, 1.0986]`.

0 commit comments

Comments
 (0)