Log-space transformation
Transforming positive tensor values into log space for numerical or modeling workflows.
PyTorch · Function
Computes the element-wise natural logarithm of a tensor. It returns a new tensor and optionally accepts an output tensor.
torch.log applies the natural logarithm independently to every element of a tensor. It returns a tensor containing the transformed values and can optionally write into a supplied output tensor.
torch.log(input, *, out=None)| Name | Type | Description |
|---|---|---|
input | Tensor | Tensor containing the values to transform element-wise. |
out | Tensor, optional | Optional destination tensor for the result. |
A new Tensor containing the natural logarithm of each element of input.
import torch
x = torch.tensor([1.0, 2.0, 0.5])
y = torch.log(x)
print(y)Computes the element-wise natural logarithm of a positive tensor.
Transforming positive tensor values into log space for numerical or modeling workflows.
Applying the same natural-log operation across every tensor element.
Reusing caller-managed storage for the computed result.
torch.log computes the natural logarithm element by element, so each output value is \(\log_e(x_i)\). It accepts a tensor as input and returns a tensor.
Follow-up: How would you avoid invalid logarithms? Ensure the input values are positive before applying the operation; the page's example uses positive values.
The required argument is input, a Tensor. The optional keyword-only argument out specifies an output Tensor.
Follow-up: What is the purpose of out? It lets the caller provide the destination tensor for the result.
For an input tensor containing [1.0, 2.0, 0.5], torch.log produces the element-wise natural-log values. The operation preserves the tensor-shaped, element-wise structure.
Follow-up: Which related operation performs the transformation in place? torch.log_ is the in-place version of torch.log.
torch.log_torch._foreach_logtorch._foreach_log_