Pytorch入门:Tensor创建

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

Pytorch官方文档:torch — PyTorch 1.9.1 documentation

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

Tensor创建函数

常用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 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信息查看

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)

Tensor创建示例

0维Tensor(标量)

t = torch.tensor(1)

"""
tensor:  tensor(1)
type:  torch.LongTensor
size:  torch.Size([])
dim:  0
device:  cpu
dtype:  torch.int64
requires_grad:  False
"""

1维Tensor(向量)

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()的别名

2维Tensor(矩阵)

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
"""

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

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
"""

Tensor的GPU、CPU切换

可通过以下语句判断当前环境下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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值