PyTorch学习 tensor基本操作

首先准备数据

import torch

device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)

data = torch.tensor([
    [1., 2],
    [3, 4]
])

获取tensor的基本信息

方法名说明
is_tensor判断是否为一个tensor
numel返回元素的个数,相当于tensor的shape的每一个元素相乘
print(torch.is_tensor(data))
print(torch.numel(data))  # num of element

运行结果:

True
4

特殊的创建tensor的方法

方法名说明
arange(start, end, step)返回[start, end)并以step为步长的tensor
range(start, end, step)(deprecated)返回[start, end]并以step为步长的tensor,数据类型是torch.float32
linspace(start, end, numel)返回[start, end)共计numel个线性元素构成的tensor
eye(size)创建对角线为1的单位tensor,size可以为int,tuple
full(shape, value)相当于torch.ones(shape) * value,以value填充维度为shape的tensor
full_like(tensor, value)相当于torch.ones_like(tensor) * value
randperm(n)生成由0-n的随机序列,包含0,不含n
print(torch.arange(0, 4, 2))  # [start, end)
print(torch.range(0, 4, 2))  # deprecated, float 使用arange
print(torch.linspace(0, 4, 10))  # [start, end), 最后一个参数是numel
print(torch.eye(2, 3))  # 对角线为1
print(torch.full((2, 3), 2))  # torch.ones(shape) * fill_value
print(torch.full_like(data, 2))
print(torch.randperm(4))    # 对0-n的值随机组合

运行结果:

tensor([0, 2])
tensor([0., 2., 4.])
UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).
tensor([0.0000, 0.4444, 0.8889, 1.3333, 1.7778, 2.2222, 2.6667, 3.1111, 3.5556,
        4.0000])
tensor([[1., 0., 0.],
        [0., 1., 0.]])
tensor([[2, 2, 2],
        [2, 2, 2]])
tensor([[2., 2.],
        [2., 2.]])
tensor([0, 2, 1, 3])

indexing slicing joining mutating

准备数据

a_data = torch.rand(size=(2, 2))
b_data = torch.rand(size=(2, 3))

对应数据是:

tensor([[0.8442, 0.6610],
        [0.3926, 0.0318]])
tensor([[0.7560, 0.7292, 0.5139],
        [0.4516, 0.7336, 0.3402]])

维度表示中,以二维tensor为例,0维是列,1维是行,几乎所有涉及维度的操作,维度参数基本默认为0。维度由0增大,从外向内依次递增

以下演示tensor的各种操作

>>> print(torch.cat((a_data, b_data), dim=1)) 
>>> # 哪个维度不同就拼接哪个维度
>>> tensor([[0.8442, 0.6610, 0.7560, 0.7292, 0.5139],
        [0.3926, 0.0318, 0.4516, 0.7336, 0.3402]])
        
>>> print(torch.chunk(b_data, chunks=2, dim=1)) 
>>> # torch.chunk 将tensor依据指定dim维度划分成chunks指定数目的tensor
>>> (tensor([[0.7560, 0.7292],
        [0.4516, 0.7336]]), tensor([[0.5139],
        [0.3402]]))
        
>>> print(torch.gather(b_data, dim=1, index=torch.tensor([[1, 0], [0, 0]])))  
>>> # 沿着dim维,对index指定的位置上取输入的tensor对应的值
>>> # 例如:此例取[[b_data[0, 1], b_data[0, 0]], [b_data[1, 0], b_data[1, 0]]
>>> # 因为dim=1,则保留dim=0正常取值,dim=1的索引替换成index
>>> tensor([[0.7292, 0.7560],
        [0.4516, 0.4516]])
        
>>> print(torch.reshape(b_data, (-1, 2)))   
>>> # 将指定tensor的维度重组为指定的shape
>>> tensor([[0.7560, 0.7292],
        [0.5139, 0.4516],
        [0.7336, 0.3402]])
        
>>> print(torch.zeros_like(b_data, dtype=b_data.dtype).scatter(dim=0,
                                                           index=torch.tensor([[1, 0], [0, 0]]),
                                                           src=torch.arange(1., 7).reshape(2, 3)))
>>> # scatter与gather的取值模式类似
>>> #不同的是将src的对应位置的值取出来进行赋值
>>> tensor([[4., 5., 0.],
        [1., 0., 0.]])
        
>>> print(torch.split(b_data, split_size_or_sections=2)) 
>>> # 此处与chunk函数相同
>>> (tensor([[0.7560, 0.7292, 0.5139],
        [0.4516, 0.7336, 0.3402]]),)
        
>>> print(torch.split(b_data, split_size_or_sections=[1, 2], dim=1))
>>> # 但是split还可以传入列表,按照列表指定size划分,覆盖面比chunk广
>>> (tensor([[0.7560],
        [0.4516]]), tensor([[0.7292, 0.5139],
        [0.7336, 0.3402]]))
        
>>> print(torch.squeeze(torch.reshape(b_data, (2, 3, 1))))
>>> # 移除所有dim数为1的维度
>>> tensor([[0.7560, 0.7292, 0.5139],
        [0.4516, 0.7336, 0.3402]])
        
>>> print(torch.stack([b_data, torch.rand_like(b_data)]))
>>> # 将输入的tensors依据指定dim合并,每一个tensor要有相同的size
>>> tensor([[[0.7560, 0.7292, 0.5139],
         [0.4516, 0.7336, 0.3402]],

        [[0.8377, 0.9999, 0.1765],
         [0.3468, 0.0729, 0.4546]]])
         
>>> print(torch.take(b_data, torch.tensor([0, 2, 5])))
>>> # 将输入的tensor展平后,依据index取元素
>>> tensor([0.7560, 0.5139, 0.3402])

>>> print(torch.tile(b_data, (2, 3))) 
>>> # 将输入的tensor在第i维度上复制dims[i]份
>>> tensor([[0.7560, 0.7292, 0.5139, 0.7560, 0.7292, 0.5139, 0.7560, 0.7292, 0.5139],
        [0.4516, 0.7336, 0.3402, 0.4516, 0.7336, 0.3402, 0.4516, 0.7336, 0.3402],
        [0.7560, 0.7292, 0.5139, 0.7560, 0.7292, 0.5139, 0.7560, 0.7292, 0.5139],
        [0.4516, 0.7336, 0.3402, 0.4516, 0.7336, 0.3402, 0.4516, 0.7336, 0.3402]])
        
>>> print(torch.transpose(b_data, 0, 1))
>>> # 将dim0和dim1转置
>>> tensor([[0.7560, 0.4516],
        [0.7292, 0.7336],
        [0.5139, 0.3402]])
        
>>> print(torch.unbind(b_data))
>>> # 将输入的tensor依据指定维度切分返回元组
>>> (tensor([0.7560, 0.7292, 0.5139]), tensor([0.4516, 0.7336, 0.3402]))

>>> print(torch.unsqueeze(b_data, dim=-1))
>>> # 在指定的维度增加一维
>>> tensor([[[0.7560],
         [0.7292],
         [0.5139]],

        [[0.4516],
         [0.7336],
         [0.3402]]])

>>> print(torch.where(b_data < 0.5, b_data, torch.ones_like(b_data)))
>>> # 如果condition对应位置为True,则返回x对应位置元素,否则返回y的(用于mask)
>>> tensor([[1.0000, 1.0000, 1.0000],
        [0.4516, 1.0000, 0.3402]])

# torch.manual_seed(seed) 设置随机种子,便于模型复现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值