![9566602543cd8a626a14bbaa075934e1.png](https://img-blog.csdnimg.cn/img_convert/9566602543cd8a626a14bbaa075934e1.png)
从数组、列表对象创建
Numpy Array 数组和 Python List 列表是 Python 程序中间非常重要的数据载体容器,很多数据都是通过 Python 语言将数据加载至 Array 数组或者 List 列表容器,再转换到 Tensor 类型。(为了方便描述,后面将 Numpy Array 数组称为数组,将 Python List 列表称为列表。)
PyTorch 从数组或者列表对象中创建 Tensor 有四种方式:
- torch.Tensor
- torch.tensor
- torch.as_tensor
- torch.from_numpy
>>> import torch
>>> import numpy as np
>>> array = np.array([1, 2, 3])
>>> list = [4, 5, 6]
# 方式一:使用torch.Tensor类
>>> tensor_array_a = torch.Tensor(array)
>>> tensor_list_a = torch.Tensor(list)
>>> print(isinstance(tensor_array_a, torch.Tensor)
, tensor_array_a.type())
Tr