PyTorch torch.frombuffer 函数

约 293 字 更新于 2026-11-18 5iyo.com

PyTorch torch.frombuffer 函数


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)