PyTorch - 08 - 创建用于深度学习的PyTorch张量-最佳选择
Creating PyTorch Tensors - Best Options
我们已经看到的PyTorch张量是torch.Tensor PyTorch类的实例。 张量和PyTorch张量的抽象概念之间的区别在于PyTorch张量为我们提供了可在代码中使用的具体实现。
在上一篇文章中,我们看到了如何使用Python列表,序列和NumPy ndarrays等数据在PyTorch中创建张量。 给定一个numpy.ndarray,我们发现有四种创建torch.Tensor对象的方法。
快速回顾一下:
> data = np.array([1,2,3])
> type(data)
numpy.ndarray
> o1 = torch.Tensor(data)
> o2 = torch.tensor(data)
> o3 = torch.as_tensor(data)
> o4 = torch.from_numpy(data)
> print(o1)
tensor([1., 2., 3.])
> print(o2)
tensor([1, 2, 3], dtype=torch.int32)
> print(o3)
tensor([1, 2, 3