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

PyTorch torch.asarray 函数


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

torch.asarray 是 PyTorch 中用于将数据转换为张量数组的函数。

函数定义

torch.asarray(data, dtype, device)

使用示例

实例

import torch

# 从列表转换
x = torch.asarray([1, 2, 3])
print(x)

# 从 NumPy 数组转换
import numpy as np
arr = np.array([4, 5, 6])
y = torch.asarray(arr)
print(y)

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