PyTorch torch.ravel function


Pytorch torch 参考手册Pytorch torch reference manual

torch.ravelIt is a function in PyTorch used to flatten a tensor into one dimension. It returns a view of the input tensor (if possible), not a copy.

Function definition

torch.ravel(input)

Parameter description:

  • input: input tensor

Usage example

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 result is:

tensor([1, 2, 3, 4, 5, 6])

Example

import torch

# Create a three-dimensional tensor
x = torch.arange(12).reshape(2, 3, 2)

# Flatten to one dimension
y = torch.ravel(x)
print(y)

The output result is:

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

Pytorch torch 参考手册Pytorch torch reference manual

Other extensions