From ca84875271b7949249b4eb16d1de6f5fe3342a12 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Sun, 14 Sep 2025 17:04:21 +0200 Subject: [PATCH 1/4] docs: pytorch log --- .../tensor-operations/terms/log/log.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/log/log.md diff --git a/content/pytorch/concepts/tensor-operations/terms/log/log.md b/content/pytorch/concepts/tensor-operations/terms/log/log.md new file mode 100644 index 00000000000..2185c713fa8 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/log/log.md @@ -0,0 +1,55 @@ +--- +Title: '.log()' +Description: 'Returns a new tensor with the natural logarithm of each element in the input tensor.' +Subjects: + - 'Computer Science' + - 'Machine Learning' +Tags: + - 'Functions' + - 'Machine Learning' + - 'Python' + - 'Tensor' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/computer-science' +--- + +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}$. + +## Syntax + +```pseudo +torch.log(input, *, out=None) → Tensor +``` + +**Parameters:** + +- `input`: The input tensor containing elements for which the logarithm will be computed. +- `out` (optional): A tensor to store the output. If provided, the result is written to this tensor. + +**Return value:** + +Returns a new tensor where each element is the natural logarithm of the corresponding element within the input tensor. + +## Example + +In this example, we compute the element-wise natural logarithm of a tensor using `torch.log()`: + +```py +import torch +import math + +# Define a tensor +x = torch.tensor([5.0 , 6.0 , 7.0 , math.log(2.) ]) + +# Compute the natural logarithm +result = torch.log(x) + +print(result) +``` + +Here is the output: + +```shell +tensor([ 1.6094, 1.7918, 1.9459, -0.3665]) +``` From 36c9601ba8a9c5e759e0e16549254673a12c3ee2 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Sun, 14 Sep 2025 17:08:24 +0200 Subject: [PATCH 2/4] fix: example --- content/pytorch/concepts/tensor-operations/terms/log/log.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/log/log.md b/content/pytorch/concepts/tensor-operations/terms/log/log.md index 2185c713fa8..79398b1ca36 100644 --- a/content/pytorch/concepts/tensor-operations/terms/log/log.md +++ b/content/pytorch/concepts/tensor-operations/terms/log/log.md @@ -40,7 +40,7 @@ import torch import math # Define a tensor -x = torch.tensor([5.0 , 6.0 , 7.0 , math.log(2.) ]) +x = torch.tensor([7.0 , 8.0 , 9.0 , math.log(3.)]) # Compute the natural logarithm result = torch.log(x) From 3433c3f0cc12f18207bacb35c96a50988db71992 Mon Sep 17 00:00:00 2001 From: Alessandro Capialbi Date: Sun, 14 Sep 2025 17:09:59 +0200 Subject: [PATCH 3/4] fix: shell result --- content/pytorch/concepts/tensor-operations/terms/log/log.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/log/log.md b/content/pytorch/concepts/tensor-operations/terms/log/log.md index 79398b1ca36..bed55758968 100644 --- a/content/pytorch/concepts/tensor-operations/terms/log/log.md +++ b/content/pytorch/concepts/tensor-operations/terms/log/log.md @@ -51,5 +51,5 @@ print(result) Here is the output: ```shell -tensor([ 1.6094, 1.7918, 1.9459, -0.3665]) +tensor([1.9459, 2.0794, 2.1972, 0.0940]) ``` From 2f50a1bf72388cbd5221eeb8674a7975848b4dbc Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 16 Sep 2025 10:12:29 +0530 Subject: [PATCH 4/4] minor tweaks --- content/pytorch/concepts/tensor-operations/terms/log/log.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/log/log.md b/content/pytorch/concepts/tensor-operations/terms/log/log.md index bed55758968..41412fc3f23 100644 --- a/content/pytorch/concepts/tensor-operations/terms/log/log.md +++ b/content/pytorch/concepts/tensor-operations/terms/log/log.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -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}$. +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. ## Syntax @@ -25,7 +25,7 @@ torch.log(input, *, out=None) → Tensor **Parameters:** - `input`: The input tensor containing elements for which the logarithm will be computed. -- `out` (optional): A tensor to store the output. If provided, the result is written to this tensor. +- `out` (optional): Output tensor to store the result. Must have the same shape as `input`. **Return value:** @@ -33,7 +33,7 @@ Returns a new tensor where each element is the natural logarithm of the correspo ## Example -In this example, we compute the element-wise natural logarithm of a tensor using `torch.log()`: +The following example shows how to compute the element-wise natural logarithm of a tensor using `torch.log()`: ```py import torch