PyTorch torch.ravel function
PyTorch torch reference manual
torch.ravel is a function in PyTorch used to flatten a tensor into one dimension. It returns a view of the input tensor (if possible) rather than a copy.
Function definition
torch.ravel(input)
Parameter Description:
input: the input tensor
Usage examples
Example
import
torch
Create a 2x3 matrix
x
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
)
Flatten to one dimension
y
=
torch.
ravel
(
x
)
(
y
)
The output is:
tensor([1, 2, 3, 4, 5, 6])
Example
import
torch
Create a 3D tensor
x
=
torch.
arange
(
12
)
.
reshape
(
2
,
3
,
2
)
Flatten to one dimension
y
=
torch.
ravel
(
x
)
(
y
)
The output is:
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])