Skip to content

Commit 5276522

Browse files
authored
[Term Entry] PyTorch Tensor Operations: .mul() (#7843)
* added mul() markdown * yarn format fix for mul.md * removed uneccesary sentence in mul.md * Clarify .mul() function description and syntax Updated the description of the .mul() function for clarity and improved the syntax formatting. * Update mul.md ---------
1 parent a07aeb5 commit 5276522

File tree

1 file changed

+103
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/mul

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
Title: '.mul()'
3+
Description: 'Performs element-wise multiplication of two tensors or multiplies a tensor by a scalar.'
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+
The **`.mul()`** function multiplies each element in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) with another tensor or a scalar and returns a tensor of the same shape as the input (or broadcasted shape when inputs have different shapes).
17+
18+
## Syntax
19+
20+
```pseudo
21+
torch.mul(input, other, *, out=None) → Tensor
22+
```
23+
24+
**Parameters:**
25+
26+
- `input` (Tensor): Input tensor.
27+
- `other` (Tensor or Integer): The second input can be another tensor or a scalar value.
28+
- `out` (Tensor, optional): Optional tensor to store the output.
29+
30+
**Return value:**
31+
32+
Returns a new tensor containing the result of the operation, or modifies the `out` tensor if provided.
33+
34+
## Example 1: Applying `.mul()` with a 1D tensor and a scalar
35+
36+
In this example, `.mul()` multiplies a 1D tensor and a scalar:
37+
38+
```py
39+
import torch
40+
41+
# Create a tensor
42+
x = torch.tensor([1, 2, 3])
43+
44+
# Multiply by scalar
45+
result = torch.mul(x, 2)
46+
47+
print(result)
48+
```
49+
50+
The output of this code is:
51+
52+
```shell
53+
tensor([2, 4, 6])
54+
```
55+
56+
## Example 2: Using `.mul()` with two 1D tensors
57+
58+
In this example, `.mul()` multiplies each element in a 1D tensor with another 1D tensor:
59+
60+
```py
61+
import torch
62+
63+
# Create a tensor
64+
x = torch.tensor([1.0, 2.0, 3.0])
65+
# Create a second tensor
66+
y = torch.tensor([4.0, 5.0, 6.0])
67+
68+
# Multiply by tensor
69+
result = torch.mul(x, y)
70+
71+
print(result)
72+
```
73+
74+
The output of this code is:
75+
76+
```shell
77+
tensor([ 4., 10., 18.])
78+
```
79+
80+
## Example 3: Using `.mul()` with a 2D tensor and a 1D tensor
81+
82+
In this example, `.mul()` multiplies a 2D tensor with a 1D tensor using broadcasting:
83+
84+
```py
85+
import torch
86+
87+
# Create a 2x3 tensor
88+
x = torch.tensor([[1.0, 2.5, 3.0],[4.0, 5.0, 6.5]])
89+
# Create a second tensor
90+
y = torch.tensor([7.0, 8.0, 9.1])
91+
92+
# Multiply by tensor
93+
result = torch.mul(x, y)
94+
95+
print(result)
96+
```
97+
98+
The output of this code is:
99+
100+
```shell
101+
tensor([[ 7.0000, 20.0000, 27.3000],
102+
[28.0000, 40.0000, 59.1500]])
103+
```

0 commit comments

Comments
 (0)