|
| 1 | +--- |
| 2 | +Title: '.div()' |
| 3 | +Description: 'Performs element-wise division of input tensors or a tensor by a scalar.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Machine Learning' |
| 7 | +Tags: |
| 8 | + - 'Neural Networks' |
| 9 | + - 'PyTorch' |
| 10 | + - 'Tensor' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/machine-learning' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`.div()`** function performs element-wise division between tensors or divides a tensor by a scalar value. It is a fundamental tensor operation in PyTorch, commonly used for performing machine learning and deep learning tasks such as data preprocessing, normalization, and optimization. |
| 17 | + |
| 18 | +Element-wise operations are essential in tensor computations, enabling efficient parallel processing. The `.div()` function offers a simple and optimized way to handle division across tensors in neural networks and mathematical transformations. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +torch.div(input, other, *, rounding_mode=None, out=None) |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +- `input`: The input tensor (dividend). |
| 29 | +- `other`: The tensor or scalar to divide by (divisor). |
| 30 | +- `rounding_mode` (Optional): Controls the rounding behavior. Can be `None` (default), `trunc`, or `floor`. |
| 31 | +- `out` (Optional): The output tensor to store the result. |
| 32 | + |
| 33 | +**Return value:** |
| 34 | + |
| 35 | +A tensor with the result of element-wise division. If `out` is provided, the returned tensor is the same as `out`. |
| 36 | + |
| 37 | +## Example 1: Basic Usage of `.div()` with Tensors |
| 38 | + |
| 39 | +This example demonstrates how to use `.div()` to perform element-wise division between two tensors of the same shape: |
| 40 | + |
| 41 | +```py |
| 42 | +import torch |
| 43 | + |
| 44 | +# Create input tensors |
| 45 | +tensor1 = torch.tensor([4.0, 9.0, 16.0, 25.0]) |
| 46 | +tensor2 = torch.tensor([2.0, 3.0, 4.0, 5.0]) |
| 47 | + |
| 48 | +# Perform element-wise division |
| 49 | +result = torch.div(tensor1, tensor2) |
| 50 | + |
| 51 | +# Print results |
| 52 | +print("Tensor1 (dividend):", tensor1) |
| 53 | +print("Tensor2 (divisor):", tensor2) |
| 54 | +print("Division result:", result) |
| 55 | +``` |
| 56 | + |
| 57 | +Here is the output: |
| 58 | + |
| 59 | +```shell |
| 60 | +Tensor1 (dividend): tensor([ 4., 9., 16., 25.]) |
| 61 | +Tensor2 (divisor): tensor([2., 3., 4., 5.]) |
| 62 | +Division result: tensor([2., 3., 4., 5.]) |
| 63 | +``` |
| 64 | + |
| 65 | +The `.div()` operation computes the element-wise division of `tensor1` by `tensor2`. Each element in the resulting tensor is the quotient of the corresponding elements in the input tensors. |
| 66 | + |
| 67 | +## Example 2: Division with Rounding Modes |
| 68 | + |
| 69 | +The `.div()` function supports optional rounding modes when performing integer division, which control how the result is rounded: |
| 70 | + |
| 71 | +```py |
| 72 | +import torch |
| 73 | + |
| 74 | +# Create input tensors |
| 75 | +tensor1 = torch.tensor([5, 7, 10, 15]) |
| 76 | +tensor2 = torch.tensor([2, 2, 3, 4]) |
| 77 | + |
| 78 | +# Division with different rounding modes |
| 79 | +result_default = torch.div(tensor1, tensor2) |
| 80 | +result_floor = torch.div(tensor1, tensor2, rounding_mode='floor') |
| 81 | +result_trunc = torch.div(tensor1, tensor2, rounding_mode='trunc') |
| 82 | + |
| 83 | +# Print results |
| 84 | +print("Default division:", result_default) |
| 85 | +print("Floor division:", result_floor) |
| 86 | +print("Trunc division:", result_trunc) |
| 87 | +``` |
| 88 | + |
| 89 | +Here is the output: |
| 90 | + |
| 91 | +```shell |
| 92 | +Default division: tensor([2.5000, 3.5000, 3.3333, 3.7500]) |
| 93 | +Floor division: tensor([2, 3, 3, 3]) |
| 94 | +Trunc division: tensor([2, 3, 3, 3]) |
| 95 | +``` |
| 96 | + |
| 97 | +The rounding modes control how the division results are handled: |
| 98 | + |
| 99 | +- Default mode performs true division, returning floating-point results. |
| 100 | +- `floor` mode rounds the result down toward negative infinity (i.e., floor division). |
| 101 | +- `trunc` mode truncates the decimal part, rounding toward zero. |
| 102 | + |
| 103 | +## Example 3: Data Normalization with `.div()` |
| 104 | + |
| 105 | +The `.div()` function is commonly used in data preprocessing and normalization. Here's an example of normalizing a dataset: |
| 106 | + |
| 107 | +```py |
| 108 | +import torch |
| 109 | + |
| 110 | +# Create a sample dataset (3 samples with 4 features each) |
| 111 | +dataset = torch.tensor([ |
| 112 | + [10.0, 20.0, 30.0, 40.0], |
| 113 | + [15.0, 25.0, 35.0, 45.0], |
| 114 | + [20.0, 30.0, 40.0, 50.0] |
| 115 | +]) |
| 116 | + |
| 117 | +# Calculate the range (max - min) for each feature |
| 118 | +feature_min = dataset.min(dim=0).values |
| 119 | +feature_max = dataset.max(dim=0).values |
| 120 | +feature_range = feature_max - feature_min |
| 121 | + |
| 122 | +# Normalize the dataset to [0, 1] range using div |
| 123 | +normalized_data = torch.div(dataset - feature_min, feature_range) |
| 124 | + |
| 125 | +print("Original dataset:") |
| 126 | +print(dataset) |
| 127 | +print("\nNormalized dataset:") |
| 128 | +print(normalized_data) |
| 129 | +``` |
| 130 | + |
| 131 | +Here is the output: |
| 132 | + |
| 133 | +```shell |
| 134 | +Original dataset: |
| 135 | +tensor([[10., 20., 30., 40.], |
| 136 | + [15., 25., 35., 45.], |
| 137 | + [20., 30., 40., 50.]]) |
| 138 | + |
| 139 | +Normalized dataset: |
| 140 | +tensor([[0.0000, 0.0000, 0.0000, 0.0000], |
| 141 | + [0.5000, 0.5000, 0.5000, 0.5000], |
| 142 | + [1.0000, 1.0000, 1.0000, 1.0000]]) |
| 143 | +``` |
| 144 | + |
| 145 | +This example demonstrates how `.div()` can be used to normalize data to a specific range, which is a commonly performed preprocessing step in machine learning workflows. |
0 commit comments