Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.ravel function

PyTorch torch.ravel function


PyTorch torch reference manual 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:


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

)

print

(

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

)

print

(

y

)

The output is:

tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])


PyTorch torch reference manual PyTorch torch reference manual