|
| 1 | +--- |
| 2 | +Title: '.addcmul()' |
| 3 | +Description: 'Performs element-wise multiplication of two tensors and adds a scaled result to a third tensor.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Machine Learning' |
| 7 | +Tags: |
| 8 | + - 'Deep Learning' |
| 9 | + - 'PyTorch' |
| 10 | + - 'Tensors' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/machine-learning' |
| 14 | +--- |
| 15 | + |
| 16 | +**`.addcmul()`** is a PyTorch [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) operation that performs element-wise multiplication of two tensors and then adds a third tensor to the result, optionally scaled by a value. This operation combines multiplication and addition in a single efficient function call. |
| 17 | + |
| 18 | +The function is particularly useful in tensor operations where there is a need to perform element-wise multiplication and addition in sequence. It provides a concise way to perform these operations without creating intermediate tensors, which can help optimize memory usage in PyTorch applications. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +torch.addcmul(input, tensor1, tensor2, value=1, out=None) |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +- `input` (Tensor): The tensor to be added to the result |
| 29 | +- `tensor1` (Tensor): The first tensor to be multiplied element-wise |
| 30 | +- `tensor2` (Tensor): The second tensor to be multiplied element-wise |
| 31 | +- `value` (Number, optional): Multiplier for the product of `tensor1` and `tensor2`. Default value is `1` |
| 32 | +- `out` (Tensor, optional): The output tensor to store the result |
| 33 | + |
| 34 | +**Return value:** |
| 35 | + |
| 36 | +Returns a new tensor containing the result of the operation, or modifies the `out` tensor if provided. |
| 37 | + |
| 38 | +## Example 1: Basic usage of `.addcmul()` with PyTorch tensors |
| 39 | + |
| 40 | +This example demonstrates the basic functionality of `.addcmul()` by applying it to random tensors to show how the element-wise multiplication and addition works: |
| 41 | + |
| 42 | +```py |
| 43 | +import torch |
| 44 | + |
| 45 | +# Create input tensor and two tensors to be multiplied |
| 46 | +input_tensor = torch.tensor([1.0, 2.0, 3.0]) |
| 47 | +tensor1 = torch.tensor([2.0, 3.0, 4.0]) |
| 48 | +tensor2 = torch.tensor([3.0, 2.0, 1.0]) |
| 49 | + |
| 50 | +# Apply addcmul operation |
| 51 | +# Formula: result = input + value * (tensor1 * tensor2) |
| 52 | +result = torch.addcmul(input_tensor, tensor1, tensor2) |
| 53 | + |
| 54 | +# Display the tensors and result |
| 55 | +print("Input tensor:", input_tensor) |
| 56 | +print("Tensor 1:", tensor1) |
| 57 | +print("Tensor 2:", tensor2) |
| 58 | +print("Result of addcmul (default value=1):", result) |
| 59 | + |
| 60 | +# Try with a different value parameter |
| 61 | +scaled_result = torch.addcmul(input_tensor, tensor1, tensor2, value=0.5) |
| 62 | +print("Result of addcmul with value=0.5:", scaled_result) |
| 63 | +``` |
| 64 | + |
| 65 | +This example results in the following output: |
| 66 | + |
| 67 | +```shell |
| 68 | +Input tensor: tensor([1., 2., 3.]) |
| 69 | +Tensor 1: tensor([2., 3., 4.]) |
| 70 | +Tensor 2: tensor([3., 2., 1.]) |
| 71 | +Result of addcmul (default value=1): tensor([7., 8., 7.]) |
| 72 | +Result of addcmul with value=0.5: tensor([4., 5., 5.]) |
| 73 | +``` |
| 74 | + |
| 75 | +## Example 2: Using `.addcmul()` with different scale values |
| 76 | + |
| 77 | +This example demonstrates how `.addcmul()` behaves with different scale values: |
| 78 | + |
| 79 | +```py |
| 80 | +import torch |
| 81 | + |
| 82 | +# Create three simple tensors |
| 83 | +input_tensor = torch.tensor([1.0, 2.0, 3.0]) |
| 84 | +tensor1 = torch.tensor([2.0, 3.0, 4.0]) |
| 85 | +tensor2 = torch.tensor([3.0, 2.0, 1.0]) |
| 86 | + |
| 87 | +# Apply addcmul with different scale values |
| 88 | +result1 = torch.addcmul(input_tensor, tensor1, tensor2, value=1.0) |
| 89 | +result2 = torch.addcmul(input_tensor, tensor1, tensor2, value=0.5) |
| 90 | +result3 = torch.addcmul(input_tensor, tensor1, tensor2, value=2.0) |
| 91 | + |
| 92 | +# Print results |
| 93 | +print("Input tensor:", input_tensor) |
| 94 | +print("Tensor 1:", tensor1) |
| 95 | +print("Tensor 2:", tensor2) |
| 96 | +print("\naddcmul with value=1.0:", result1) |
| 97 | +print("addcmul with value=0.5:", result2) |
| 98 | +print("addcmul with value=2.0:", result3) |
| 99 | + |
| 100 | +# Show the calculations step by step |
| 101 | +print("\nCalculation steps for first element with value=1.0:") |
| 102 | +print(f"input[0] + value * (tensor1[0] * tensor2[0]) = {input_tensor[0]} + 1.0 * ({tensor1[0]} * {tensor2[0]}) = {result1[0]}") |
| 103 | +``` |
| 104 | + |
| 105 | +This example results in the following output: |
| 106 | + |
| 107 | +```shell |
| 108 | +Input tensor: tensor([1., 2., 3.]) |
| 109 | +Tensor 1: tensor([2., 3., 4.]) |
| 110 | +Tensor 2: tensor([3., 2., 1.]) |
| 111 | + |
| 112 | +addcmul with value=1.0: tensor([7., 8., 7.]) |
| 113 | +addcmul with value=0.5: tensor([4., 5., 5.]) |
| 114 | +addcmul with value=2.0: tensor([13., 14., 11.]) |
| 115 | + |
| 116 | +Calculation steps for first element with value=1.0: |
| 117 | +input[0] + value * (tensor1[0] * tensor2[0]) = 1.0 + 1.0 * (2.0 * 3.0) = 7.0 |
| 118 | +``` |
| 119 | + |
| 120 | +## Example 3: Combining `.addcmul()` with in-place operations |
| 121 | + |
| 122 | +This example shows how to use `.addcmul_()` (the in-place version) to modify tensors directly: |
| 123 | + |
| 124 | +```py |
| 125 | +import torch |
| 126 | + |
| 127 | +# Create tensors |
| 128 | +x = torch.tensor([1.0, 2.0, 3.0]) |
| 129 | +y = torch.tensor([0.1, 0.2, 0.3]) |
| 130 | +z = torch.tensor([1.5, 2.5, 3.5]) |
| 131 | + |
| 132 | +print("Original x:", x) |
| 133 | +print("y:", y) |
| 134 | +print("z:", z) |
| 135 | + |
| 136 | +# Use addcmul with a new tensor to store results |
| 137 | +result = torch.addcmul(x, y, z, value=2.0) |
| 138 | +print("\nResult of torch.addcmul(x, y, z, value=2.0):", result) |
| 139 | +print("Original x is unchanged:", x) |
| 140 | + |
| 141 | +# Use the in-place version addcmul_ |
| 142 | +x.addcmul_(y, z, value=2.0) |
| 143 | +print("\nAfter x.addcmul_(y, z, value=2.0), x is now:", x) |
| 144 | + |
| 145 | +# Verify that x now has the same values as result |
| 146 | +print("x now equals the previous result:", torch.equal(x, result)) |
| 147 | +``` |
| 148 | + |
| 149 | +This example results in the following output: |
| 150 | + |
| 151 | +```shell |
| 152 | +Original x: tensor([1., 2., 3.]) |
| 153 | +y: tensor([0.1, 0.2, 0.3]) |
| 154 | +z: tensor([1.5, 2.5, 3.5]) |
| 155 | + |
| 156 | +Result of torch.addcmul(x, y, z, value=2.0): tensor([1.3000, 3.0000, 5.1000]) |
| 157 | +Original x is unchanged: tensor([1., 2., 3.]) |
| 158 | + |
| 159 | +After x.addcmul_(y, z, value=2.0), x is now: tensor([1.3000, 3.0000, 5.1000]) |
| 160 | +x now equals the previous result: True |
| 161 | +``` |
| 162 | + |
| 163 | +To learn more about how to create, train, and test neural networks, check out this [Intro to PyTorch and Neural Networks](https://www.codecademy.com/enrolled/courses/intro-to-py-torch-and-neural-networks) course on Codecademy. |
0 commit comments