PyTorch torch.asarray 函数

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

PyTorch torch.asarray 函数


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)