TUTORIALAI 与数据科学FETCHED

PyTorch torch.frombuffer 函数

PyTorch torch.frombuffer 函数 Pytorch torch 参考手册 torch.frombuffer 是 PyTorch 中用于从 buffer 创建一维张量的函数。 函数定义 torch.frombuffer(buffer, dtype, count, offset) 使用示例 实例 [mycode4 type='python'] import torch import..

更新于 2026/7/16

教程详情页默认 noindex。

PyTorch torch.frombuffer 函数


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

torch.frombuffer 是 PyTorch 中用于从 buffer 创建一维张量的函数。

函数定义

torch.frombuffer(buffer, dtype, count, offset)

使用示例

实例

import torch
import array

# 从 array 创建
arr = array.array('i', [1, 2, 3, 4, 5])
x = torch.frombuffer(arr, dtype=torch.int32)

print(x)

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