当前位置:   article > 正文

Pytorch入门:Tensor创建_pytorch 创建tensor

pytorch 创建tensor

Tensor即张量,是深度学习中常用的数据形式。

Pytorch官方文档:torch — PyTorch 1.9.1 documentation

Pytorch中文文档:主页 - PyTorch中文文档

Tensor创建函数

常用tensor创建函数如下:

  1. """ 根据传入数据创建张量 """
  2. torch.tensor(data, dtype=None, device=None, requires_grad=False)
  3. """ 根据传入数据创建张量,torch.Tensor()等同于torch.FloatTensor() """
  4. torch.Tensor(data, dtype=None, device=None, requires_grad=False)
  5. """ 根据size创建值在[0, 1)范围的随机张量 """
  6. torch.rand(*size, dtype=None, device=None, requires_grad=False)
  7. """ 根据size创建值为1的张量 """
  8. torch.ones(*size, dtype=None, device=None, requires_grad=False)
  9. """ 根据size创建值为0的张量 """
  10. torch.zeros(*size, dtype=None, device=None, requires_grad=False)
  11. """ 根据size创建值为value的张量 """
  12. torch.full([*size], value, dtype=None, device=None, requires_grad=False)
  13. """ 根据传入tensor的size创建值为1的张量 """
  14. torch.ones_like(tensor, dtype=None, device=None, requires_grad=False)
  15. """ 根据传入tensor的size创建值为0的张量 """
  16. torch.zeros_like(tensor, dtype=None, device=None, requires_grad=False)
  17. """ 根据传入tensor的size创建值为value的张量 """
  18. torch.full_like(tensor, value, dtype=None, device=None, requires_grad=False)

dtype参数:数据类型。可省略。

device参数:该张量存放于cpu或gpu,如device='cpu'或device='cuda:0'。可省略,默认为cpu。

requires_grad参数:该张量是否需要在计算中保留对应的梯度信息,训练过程的tensor需设置为True,测试阶段可设置为False。可省略,默认为False。

下表列出了Tensor类与其对应dtype详情:

Data typedtypeTensor types
32-bit floating pointtorch.float32 或 torch.floattorch.*.FloatTensor
64-bit floating pointtorch.float64 或 torch.doubletorch.*.DoubleTensor
16-bit floating pointtorch.float16 或 torch.halftorch.*.HalfTensor
8-bit integer (unsigned)torch.uint8torch.*.ByteTensor
8-bit integer (signed)torch.int8torch.*.CharTensor
16-bit integer (signed)torch.int16 或 torch.shorttorch.*.ShortTensor
32-bit integer (signed)torch.int32 或 torch.inttorch.*.IntTensor
64-bit integer (signed)torch.int64 或 torch.longtorch.*.LongTensor

Tensor信息查看

  1. print('tensor: ', tensor)
  2. print('type: ', tensor.type())
  3. print('size: ', tensor.size())
  4. print('dim: ', tensor.dim())
  5. print('device: ', tensor.device)
  6. print('dtype: ', tensor.dtype)
  7. print('requires_grad: ', tensor.requires_grad)

Tensor创建示例

0维Tensor(标量)

  1. t = torch.tensor(1)
  2. """
  3. tensor: tensor(1)
  4. type: torch.LongTensor
  5. size: torch.Size([])
  6. dim: 0
  7. device: cpu
  8. dtype: torch.int64
  9. requires_grad: False
  10. """

1维Tensor(向量)

  1. t = torch.tensor([1, 2, 3])
  2. """
  3. tensor: tensor([1, 2, 3])
  4. type: torch.LongTensor
  5. size: torch.Size([3])
  6. dim: 1
  7. device: cpu
  8. dtype: torch.int64
  9. requires_grad: False
  10. """
  1. t = torch.Tensor([1, 2, 3])
  2. """
  3. tensor: tensor([1., 2., 3.])
  4. type: torch.FloatTensor
  5. size: torch.Size([3])
  6. dim: 1
  7. device: cpu
  8. dtype: torch.float32
  9. requires_grad: False
  10. """

注:torch.Tensor()为torch.FloatTensor()的别名

2维Tensor(矩阵)

  1. t = torch.tensor([[1, 2, 3], [4, 5, 6]])
  2. """
  3. tensor: tensor([[1, 2, 3],
  4. [4, 5, 6]])
  5. type: torch.LongTensor
  6. size: torch.Size([2, 3])
  7. dim: 2
  8. device: cpu
  9. dtype: torch.int64
  10. requires_grad: False
  11. """
  1. t = torch.rand(2, 3)
  2. """
  3. tensor: tensor([[0.3600, 0.5245, 0.3553],
  4. [0.4554, 0.8006, 0.8296]])
  5. type: torch.FloatTensor
  6. size: torch.Size([2, 3])
  7. dim: 2
  8. device: cpu
  9. dtype: torch.float32
  10. requires_grad: False
  11. """
  1. t2 = torch.full_like(t, 2)
  2. """
  3. tensor: tensor([[2., 2., 2.],
  4. [2., 2., 2.]])
  5. type: torch.FloatTensor
  6. size: torch.Size([2, 3])
  7. dim: 2
  8. device: cpu
  9. dtype: torch.float32
  10. requires_grad: False
  11. """

n维Tensor(n>=3)(张量)

  1. t = torch.full([2, 3, 5], 2, dtype=torch.float, device='cuda:0', requires_grad=False)
  2. """
  3. tensor: tensor([[[2., 2., 2., 2., 2.],
  4. [2., 2., 2., 2., 2.],
  5. [2., 2., 2., 2., 2.]],
  6. [[2., 2., 2., 2., 2.],
  7. [2., 2., 2., 2., 2.],
  8. [2., 2., 2., 2., 2.]]], device='cuda:0')
  9. type: torch.cuda.FloatTensor
  10. size: torch.Size([2, 3, 5])
  11. dim: 3
  12. device: cuda:0
  13. dtype: torch.float32
  14. requires_grad: False
  15. """

Tensor的GPU、CPU切换

可通过以下语句判断当前环境下GPU是否可用:

  1. if torch.cuda.is_available():
  2. print('GPU is available.')

若GPU可用,则可将张量转移到GPU:

  1. t = torch.rand(2, 3)
  2. t = t.cuda() # 将张量转移到GPU
  3. print(t.device) # cuda:0
  4. t = t.cpu() # 将张量转移到CPU
  5. print(t.device) # cpu

也可以通过to方法进行转移:

  1. device = 'cuda' if torch.cuda.is_available() else 'cpu'
  2. t = torch.rand(2, 3)
  3. t = t.to(device)
  4. print(t.device) # cuda:0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/513989
推荐阅读
相关标签
  

闽ICP备14008679号