|
| 1 | +--- |
| 2 | +Title: '.nextafter()' |
| 3 | +Description: 'Returns the next floating-point value after each element of input in the direction of the corresponding element of other.' |
| 4 | +Subjects: |
| 5 | + - 'Data Science' |
| 6 | + - 'Machine Learning' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'Operations' |
| 10 | + - 'PyTorch' |
| 11 | + - 'Tensor' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-pytorch' |
| 14 | + - 'intro-to-py-torch-and-neural-networks' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`torch.nextafter()`** function returns the next representable floating-point value of each element in the `input` tensor in the direction of the corresponding element in the `other` tensor. |
| 18 | + |
| 19 | +It’s useful for precise floating-point control, such as numerical stability adjustments or boundary value analysis. |
| 20 | + |
| 21 | +> **Note:** If an element in `input` equals the corresponding element in `other`, that element is returned unchanged in the output tensor. |
| 22 | +
|
| 23 | +## Syntax |
| 24 | + |
| 25 | +```pseudo |
| 26 | +torch.nextafter(input, other, out=None) |
| 27 | +``` |
| 28 | + |
| 29 | +**Parameters:** |
| 30 | + |
| 31 | +- `input` (Tensor): The input tensor containing starting floating-point values. |
| 32 | +- `other` (Tensor): The tensor indicates each element's direction in `input`. |
| 33 | +- `out` (Tensor, optional): The output tensor to store results. If not provided, a new tensor is returned. |
| 34 | + |
| 35 | +**Return value:** |
| 36 | + |
| 37 | +Returns a tensor of the same shape as `input`, where each element is the next representable floating-point value of `input` in the direction of `other`. |
| 38 | + |
| 39 | +## Example |
| 40 | + |
| 41 | +The following example shows how `torch.nextafter()` moves each element of one tensor slightly closer to the corresponding element in another tensor: |
| 42 | + |
| 43 | +```py |
| 44 | +import torch |
| 45 | + |
| 46 | +# Create input tensors (float type) |
| 47 | +input_tensor = torch.tensor([1.0, 5.0, -10.0, 8.0]) |
| 48 | +other_tensor = torch.tensor([2.0, 1.0, -11.0, 8.0]) |
| 49 | + |
| 50 | +# Calculate nextafter |
| 51 | +result = torch.nextafter(input_tensor, other_tensor) |
| 52 | + |
| 53 | +print("Input Tensor:") |
| 54 | +print(input_tensor) |
| 55 | + |
| 56 | +print("\nOther Tensor:") |
| 57 | +print(other_tensor) |
| 58 | + |
| 59 | +print("\nResult Tensor:") |
| 60 | +print(result) |
| 61 | +``` |
| 62 | + |
| 63 | +The output of the code is: |
| 64 | + |
| 65 | +```shell |
| 66 | +Input Tensor: |
| 67 | +tensor([ 1., 5., -10., 8.]) |
| 68 | + |
| 69 | +Other Tensor: |
| 70 | +tensor([ 2., 1., -11., 8.]) |
| 71 | + |
| 72 | +Result Tensor: |
| 73 | +tensor([ 1.0000, 5.0000, -10.0000, 8.0000]) |
| 74 | +``` |
| 75 | + |
| 76 | +In this: |
| 77 | + |
| 78 | +- `1.0` becomes slightly larger since it moves toward `2.0`. |
| 79 | +- `5.0` becomes slightly smaller since it moves toward `1.0`. |
| 80 | +- `-10.0` becomes slightly more negative since it moves toward `-11.0`. |
| 81 | +- `8.0` remains unchanged since it already equals the corresponding value. |
0 commit comments