赞
踩
Tensor即张量,是深度学习中常用的数据形式。
Pytorch官方文档:torch — PyTorch 1.9.1 documentation
Pytorch中文文档:主页 - PyTorch中文文档
常用tensor创建函数如下:
- """ 根据传入数据创建张量 """
- torch.tensor(data, dtype=None, device=None, requires_grad=False)
-
- """ 根据传入数据创建张量,torch.Tensor()等同于torch.FloatTensor() """
- torch.Tensor(data, dtype=None, device=None, requires_grad=False)
-
-
-
- """ 根据size创建值在[0, 1)范围的随机张量 """
- torch.rand(*size, dtype=None, device=None, requires_grad=False)
-
- """ 根据size创建值为1的张量 """
- torch.ones(*size, dtype=None, device=None, requires_grad=False)
-
- """ 根据size创建值为0的张量 """
- torch.zeros(*size, dtype=None, device=None, requires_grad=False)
-
- """ 根据size创建值为value的张量 """
- torch.full([*size], value, dtype=None, device=None, requires_grad=False)
-
-
-
- """ 根据传入tensor的size创建值为1的张量 """
- torch.ones_like(tensor, dtype=None, device=None, requires_grad=False)
-
- """ 根据传入tensor的size创建值为0的张量 """
- torch.zeros_like(tensor, dtype=None, device=None, requires_grad=False)
-
- """ 根据传入tensor的size创建值为value的张量 """
- 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 type | dtype | Tensor types |
---|---|---|
32-bit floating point | torch.float32 或 torch.float | torch.*.FloatTensor |
64-bit floating point | torch.float64 或 torch.double | torch.*.DoubleTensor |
16-bit floating point | torch.float16 或 torch.half | torch.*.HalfTensor |
8-bit integer (unsigned) | torch.uint8 | torch.*.ByteTensor |
8-bit integer (signed) | torch.int8 | torch.*.CharTensor |
16-bit integer (signed) | torch.int16 或 torch.short | torch.*.ShortTensor |
32-bit integer (signed) | torch.int32 或 torch.int | torch.*.IntTensor |
64-bit integer (signed) | torch.int64 或 torch.long | torch.*.LongTensor |
- print('tensor: ', tensor)
- print('type: ', tensor.type())
- print('size: ', tensor.size())
- print('dim: ', tensor.dim())
- print('device: ', tensor.device)
- print('dtype: ', tensor.dtype)
- print('requires_grad: ', tensor.requires_grad)
- t = torch.tensor(1)
-
- """
- tensor: tensor(1)
- type: torch.LongTensor
- size: torch.Size([])
- dim: 0
- device: cpu
- dtype: torch.int64
- requires_grad: False
- """
- t = torch.tensor([1, 2, 3])
-
- """
- tensor: tensor([1, 2, 3])
- type: torch.LongTensor
- size: torch.Size([3])
- dim: 1
- device: cpu
- dtype: torch.int64
- requires_grad: False
- """
- t = torch.Tensor([1, 2, 3])
-
- """
- tensor: tensor([1., 2., 3.])
- type: torch.FloatTensor
- size: torch.Size([3])
- dim: 1
- device: cpu
- dtype: torch.float32
- requires_grad: False
- """
注:torch.Tensor()为torch.FloatTensor()的别名
- t = torch.tensor([[1, 2, 3], [4, 5, 6]])
-
- """
- tensor: tensor([[1, 2, 3],
- [4, 5, 6]])
- type: torch.LongTensor
- size: torch.Size([2, 3])
- dim: 2
- device: cpu
- dtype: torch.int64
- requires_grad: False
- """
- t = torch.rand(2, 3)
-
- """
- tensor: tensor([[0.3600, 0.5245, 0.3553],
- [0.4554, 0.8006, 0.8296]])
- type: torch.FloatTensor
- size: torch.Size([2, 3])
- dim: 2
- device: cpu
- dtype: torch.float32
- requires_grad: False
- """
- t2 = torch.full_like(t, 2)
-
- """
- tensor: tensor([[2., 2., 2.],
- [2., 2., 2.]])
- type: torch.FloatTensor
- size: torch.Size([2, 3])
- dim: 2
- device: cpu
- dtype: torch.float32
- requires_grad: False
- """
- t = torch.full([2, 3, 5], 2, dtype=torch.float, device='cuda:0', requires_grad=False)
-
- """
- tensor: tensor([[[2., 2., 2., 2., 2.],
- [2., 2., 2., 2., 2.],
- [2., 2., 2., 2., 2.]],
- [[2., 2., 2., 2., 2.],
- [2., 2., 2., 2., 2.],
- [2., 2., 2., 2., 2.]]], device='cuda:0')
- type: torch.cuda.FloatTensor
- size: torch.Size([2, 3, 5])
- dim: 3
- device: cuda:0
- dtype: torch.float32
- requires_grad: False
- """
可通过以下语句判断当前环境下GPU是否可用:
- if torch.cuda.is_available():
- print('GPU is available.')
若GPU可用,则可将张量转移到GPU:
- t = torch.rand(2, 3)
-
- t = t.cuda() # 将张量转移到GPU
- print(t.device) # cuda:0
-
- t = t.cpu() # 将张量转移到CPU
- print(t.device) # cpu
也可以通过to方法进行转移:
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
-
- t = torch.rand(2, 3)
- t = t.to(device)
- print(t.device) # cuda:0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。