TUTORIALAI 与数据科学FETCHED

PyTorch torch.ravel 函数

PyTorch torch.ravel 函数 Pytorch torch 参考手册 torch.ravel 是 PyTorch 中用于将张量展平为一维的函数。返回的是输入张量的视图(如果可能),而不是副本。 函数定义 torch.ravel(input) 参数说明: input: 输入张量 使用示例 实例 [mycode4 type='python'] import torch # 创建 ..

更新于 2026/7/16

教程详情页默认 noindex。

PyTorch torch.ravel 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.ravel 是 PyTorch 中用于将张量展平为一维的函数。返回的是输入张量的视图(如果可能),而不是副本。

函数定义

torch.ravel(input)

参数说明:

  • input: 输入张量

使用示例

实例

import torch

# 创建 2x3 矩阵
x = torch.tensor([[1, 2, 3], [4, 5, 6]])

# 展平为一维
y = torch.ravel(x)
print(y)

输出结果为:

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

实例

import torch

# 创建三维张量
x = torch.arange(12).reshape(2, 3, 2)

# 展平为一维
y = torch.ravel(x)
print(y)

输出结果为:

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

Pytorch torch 参考手册 Pytorch torch 参考手册