Skip to content

Commit b87d733

Browse files
committed
(Type what you did)
1 parent 95f0e9a commit b87d733

File tree

1 file changed

+101
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/sgn

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
Title: '.sgn()'
3+
Description: 'Computes the sign of each element in the input tensor, returning a tensor with the same shape.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Deep Learning'
9+
- 'Methods'
10+
- 'PyTorch'
11+
- 'Tensor'
12+
CatalogContent:
13+
- 'intro-to-py-torch-and-neural-networks'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.sgn()`** function computes the sign of each element in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors), applied element-wise. For real-valued tensors, it returns -1 for negative values, 0 for zero, and 1 for positive values. For complex-valued tensors, it returns the complex sign (the tensor divided by its absolute value), which gives the unit vector in the direction of each complex number.
18+
19+
## Syntax
20+
21+
```pseudo
22+
torch.sgn(input, *, out=None) → Tensor
23+
```
24+
25+
**Parameters:**
26+
27+
- `input` (Tensor): The input tensor (can be real or complex).
28+
- `out` (Tensor, optional): Optional output tensor to store the result.
29+
30+
**Return value:**
31+
32+
A tensor with the same shape as `input`, containing the sign of each element.
33+
34+
## Example 1: Using `.sgn()` with a Real-Valued Tensor
35+
36+
In this example, `.sgn()` computes the sign of each element in a real-valued tensor:
37+
38+
```py
39+
import torch
40+
41+
# Create a tensor with positive, negative, and zero values
42+
x = torch.tensor([-5.0, -2.5, 0.0, 2.5, 5.0])
43+
44+
# Compute the sign
45+
result = torch.sgn(x)
46+
47+
print(result)
48+
```
49+
50+
The output of this code is:
51+
52+
```shell
53+
tensor([-1., -1., 0., 1., 1.])
54+
```
55+
56+
## Example 2: Using `.sgn()` with a 2D Tensor
57+
58+
In this example, `.sgn()` is applied to a 2D tensor:
59+
60+
```py
61+
import torch
62+
63+
# Create a 2x3 tensor
64+
matrix = torch.tensor([[-3.0, -1.0, 0.0], [1.0, 2.0, 3.0]])
65+
66+
# Compute the sign
67+
result = torch.sgn(matrix)
68+
69+
print(result)
70+
```
71+
72+
The output of this code is:
73+
74+
```shell
75+
tensor([[-1., -1., 0.],
76+
[ 1., 1., 1.]])
77+
```
78+
79+
## Example 3: Using `.sgn()` with Complex Numbers
80+
81+
For complex-valued tensors, `.sgn()` returns the complex sign, which is the unit vector in the direction of each complex number (computed as `x / |x|`):
82+
83+
```py
84+
import torch
85+
86+
# Create a tensor with complex numbers
87+
z = torch.tensor([1+2j, -1+2j, 3-4j])
88+
89+
# Compute the complex sign
90+
result = torch.sgn(z)
91+
92+
print(result)
93+
```
94+
95+
The output of this code is:
96+
97+
```shell
98+
tensor([0.4472+0.8944j, -0.4472+0.8944j, 0.6000-0.8000j])
99+
```
100+
101+
In this example, each result has a magnitude of 1 (a unit vector), pointing in the direction of the original complex number.

0 commit comments

Comments
 (0)