现在位置: 首页 > PyTorch 教程 > 正文

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 参考手册