|
| 1 | +--- |
| 2 | +Title: 'frac()' |
| 3 | +Description: 'Returns the fractional part of each element in a PyTorch tensor.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Tensor' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-pytorch' |
| 12 | + - 'intro-to-py-torch-and-neural-networks' |
| 13 | + - 'learn-python-3' |
| 14 | +--- |
| 15 | + |
| 16 | +In PyTorch, the **`torch.frac()`** function returns the **fractional part** of each element in a tensor. It removes the integer component and keeps only the decimal part. The fractional part preserves the sign of the original number, so negative values will have negative fractional parts. |
| 17 | + |
| 18 | +Mathematically, for each element `x` in the tensor: |
| 19 | + |
| 20 | +$$ |
| 21 | +\text{out}_i = \text{input}_i - \lfloor | \text{input}_i | \rfloor \cdot \text{sgn}(\text{input}_i) |
| 22 | +$$ |
| 23 | + |
| 24 | +## Syntax |
| 25 | + |
| 26 | +```pseudo |
| 27 | +torch.frac(input, *, out=None) → Tensor |
| 28 | +``` |
| 29 | + |
| 30 | +**Parameters:** |
| 31 | + |
| 32 | +- `input`: The input tensor. |
| 33 | +- `out` (optional): Output tensor to store the result. |
| 34 | + |
| 35 | +**Return value:** |
| 36 | + |
| 37 | +A tensor of the same shape as the input, containing only the fractional parts of the elements. |
| 38 | + |
| 39 | +## Example 1: Fractional Part of a 1D Tensor |
| 40 | + |
| 41 | +This example shows how to extract fractional parts from a 1D tensor with positive and negative values: |
| 42 | + |
| 43 | +```py |
| 44 | +import torch |
| 45 | + |
| 46 | +x = torch.tensor([1.5, -2.7, 3.0, -4.9]) |
| 47 | +result = torch.frac(x) |
| 48 | +print(result) |
| 49 | +``` |
| 50 | + |
| 51 | +This example results in the following output: |
| 52 | + |
| 53 | +```shell |
| 54 | +tensor([ 0.5000, -0.7000, 0.0000, -0.9000]) |
| 55 | +``` |
| 56 | + |
| 57 | +## Example 2: Fractional Part of a 2D Tensor |
| 58 | + |
| 59 | +This example demonstrates computing fractional parts for elements in a 2D tensor (matrix): |
| 60 | + |
| 61 | +```py |
| 62 | +import torch |
| 63 | + |
| 64 | +x = torch.tensor([[2.3, -3.8], |
| 65 | + [4.0, -5.1]]) |
| 66 | +print(torch.frac(x)) |
| 67 | +``` |
| 68 | + |
| 69 | +This example results in the following output: |
| 70 | + |
| 71 | +```shell |
| 72 | +tensor([[ 0.3000, -0.8000], |
| 73 | + [ 0.0000, -0.1000]]) |
| 74 | +``` |
| 75 | + |
| 76 | +## Example 3: Using `out` Parameter |
| 77 | + |
| 78 | +This example shows how to use the `out` parameter to store results directly in a preallocated tensor: |
| 79 | + |
| 80 | +```py |
| 81 | +import torch |
| 82 | + |
| 83 | +x = torch.tensor([6.25, -7.75, 0.0]) |
| 84 | +out_tensor = torch.empty_like(x) |
| 85 | +torch.frac(x, out=out_tensor) |
| 86 | +print(out_tensor) |
| 87 | +``` |
| 88 | + |
| 89 | +This example results in the following output: |
| 90 | + |
| 91 | +```shell |
| 92 | +tensor([ 0.2500, -0.7500, 0.0000]) |
| 93 | +``` |
| 94 | + |
| 95 | +## Frequently Asked Questions |
| 96 | + |
| 97 | +### 1. What does `.frac()` do in PyTorch? |
| 98 | + |
| 99 | +The `torch.frac()` function extracts the fractional (decimal) part of each element in a tensor while removing the integer part. |
| 100 | + |
| 101 | +### 2. Does `.frac()` always return positive values? |
| 102 | + |
| 103 | +No. The fractional part preserves the sign of the original number. For example: |
| 104 | + |
| 105 | +```py |
| 106 | +import torch |
| 107 | +print(torch.frac(torch.tensor([-2.7]))) # tensor(-0.7000) |
| 108 | +``` |
| 109 | + |
| 110 | +### 3. When would you use `.frac()`? |
| 111 | + |
| 112 | +- To isolate decimal values in data preprocessing. |
| 113 | +- For scientific computing when the fractional part of values is relevant. |
| 114 | +- In testing and numerical analysis to study non-integer behavior of data. |
0 commit comments