Pytorch学习笔记_1

Pytorch学习笔记

张量的创建与运算

torch.chunk(input,chunks,dim=0)→list of tensor(只能均分)

>>> import torch
>>> b = torch.rand([3,2])
>>> b
tensor([[0.6559, 0.2541],
        [0.9358, 0.6552],
        [0.3711, 0.4700]])
>>> c,d = torch.chunk(b,chunks=2)
>>> c
tensor([[0.6559, 0.2541],
        [0.9358, 0.6552]])
>>> d
tensor([[0.3711, 0.4700]])
>>> c,d = torch.chunk(b,chunks=2,dim=1)
>>> c
tensor([[0.6559],
        [0.9358],
        [0.3711]])
>>> d
tensor([[0.2541],
        [0.6552],
        [0.4700]])

torch.gather(input,dim,index)→Tensor

延着某一维度来取某一些变量

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2
>>> t = torch.tensor([[1,2],[3,4]])
>>> t
tensor([[1, 2],
        [3, 4]])
>>> a = torch.tensor([[0,0],[1,0]])
>>> a
tensor([[0, 0],
        [1, 0]])
>>> torch.gather(t,1,a)
tensor([[1, 1],
        [4, 3]])

torch.reshape(input,shape)→Tensor

reshpe的元素和顺序不会改变;*reshape前后的乘积相等

>>> a = torch.arange(4)#torch.arange(n)创建一个一维的0→n的tensor
>>> a
tensor([0, 1, 2, 3])
>>> torch.reshape(a,[2,2])
tensor([[0, 1],
        [2, 3]])
>>> b=torch.tensor([[0,1],[2,3]])
>>> b
tensor([[0, 1],
        [2, 3]])
>>> torch.reshape(b,(-1,))
tensor([0, 1, 2, 3])

Tensor.scatter_(dim,index,src)→Tensor

将从src中的所有元素写入到当前张量中,通过index指明要写入到哪些位置上,对于src中的每个元素,输出的索引通过src[index]指明

→self is updated as:

self[index[i][j][k]][j][k] = src[i][j][k]  # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k]  # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k]  # if dim == 2
>>> src = torch.arange(1,11).reshape(2,5)
>>> src
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])
>>> index = torch.tensor([[0,1,2,0]])
>>> index
tensor([[0, 1, 2, 0]])
>>> torch.zeros(3,5,dtype=src.dtype).scatter_(0,index,src)
tensor([[1, 0, 0, 4, 0],
        [0, 2, 0, 0, 0],
        [0, 0, 3, 0, 0]])
>>> index = torch.tensor([[0,1,2],[0,1,4]])
>>> index
tensor([[0, 1, 2],
        [0, 1, 4]])
>>> torch.zeros(3,5,dtype=src.dtype).scatter_(1,index,src)
tensor([[1, 2, 3, 0, 0],
        [6, 7, 0, 0, 8],
        [0, 0, 0, 0, 0]])

Tensor.scatter_add_(dim,index,src)→Tensor

将从src中的所有元素+到当前张量中,通过index指明要写入到哪些位置上,对于src中的每个元素,输出的索引通过src[index]指明

→self is updated as:

self[index[i][j][k]][j][k] += src[i][j][k]  # if dim == 0
self[i][index[i][j][k]][k] += src[i][j][k]  # if dim == 1
self[i][j][index[i][j][k]] += src[i][j][k]  # if dim == 2

torch.split(tensor,split_size_or_sections,dim=0)

将tensor划分成多块,每一块都是原来tensor的一部分(可以不均分,对比torch.chunk)

>>> a = torch.arange(10).reshape(5,2)
>>> a
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
>>> torch.split(a, 2)
(tensor([[0, 1],
         [2, 3]]),
 tensor([[4, 5],
         [6, 7]]),
 tensor([[8, 9]]))
>>> torch.split(a, [1,4])
(tensor([[0, 1]]),
 tensor([[2, 3],
         [4, 5],
         [6, 7],
         [8, 9]]))
#传入的split_size_or_sections为一个list,则len(list)为切分的块数,list[0]为第一块的维度,list[1]为第二块的维度

torch.squeeze(input)→Tensor

将tensor中dim=1的维度去掉(降维)

>>> b = torch.rand([3,2])
>>> b
tensor([[0.7005, 0.0228],
        [0.5397, 0.9163],
        [0.3040, 0.1421]])
>>> c=torch.reshape(b,[3,1,2])
>>> c
tensor([[[0.7005, 0.0228]],

        [[0.5397, 0.9163]],

        [[0.3040, 0.1421]]])
>>> torch.squeeze(c)
tensor([[0.7005, 0.0228],
        [0.5397, 0.9163],
        [0.3040, 0.1421]])
>>> torch.squeeze(c).shape
torch.Size([3, 2])
>>> torch.squeeze(c).shape
torch.Size([3, 2])
>>> d = torch.reshape(b,[3,1,2,1,1])
>>> e = torch.squeeze(d,dim=3).shape
>>> e
torch.Size([3, 1, 2, 1])

torch.stack(tensors,dim=0)→Tensor

延着某一个新的维度将一系列tensor拼接起来(*所有tensor的size相同)

>>> a=torch.rand([3,2])
>>> b=torch.rand([3,2])
>>> torch.stack([a,b]).size
>>> torch.stack([a,b]).shape
torch.Size([2, 3, 2])
>>> torch.stack([a,b],dim=1).shape
torch.Size([3, 2, 2])

torch.take(input,index)→Tensor

将input铺成一维tensor,再根据index去reshape,这里的index.dtype=Longtensor

src = torch.tensor([[4,3,5],[6,7,8]])
>>> a=torch.tensor([0,2,5])
>>> a
tensor([0, 2, 5])
>>> torch.take(src,a)
tensor([4, 5, 8])

torch.tile(input,dims)→Tensor

对tensor不同位置的元素进行拷贝

if dims

>>> x = torch.tensor([1,2,3])
>>> x.tile((2))
tensor([1, 2, 3, 1, 2, 3])
>>> y=torch.tensor([[1,2],[3,4]])
>>> torch.tile(y,(2,2))
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4],
        [1, 2, 1, 2],
        [3, 4, 3, 4]])
#下面的例子torch.tile(z,(2,2))实则为torch.tile(z,(1,2,2))即第一维不变,第二维复制2份,第三维复制2份
>>> z = torch.rand([3,2,2])
>>> z
tensor([[[0.1977, 0.2346],
         [0.5465, 0.7507]],

        [[0.5234, 0.3743],
         [0.8807, 0.4097]],

        [[0.3975, 0.4694],
         [0.5903, 0.7884]]])
>>> torch.tile(z,(2,2))
tensor([[[0.1977, 0.2346, 0.1977, 0.2346],
         [0.5465, 0.7507, 0.5465, 0.7507],
         [0.1977, 0.2346, 0.1977, 0.2346],
         [0.5465, 0.7507, 0.5465, 0.7507]],

        [[0.5234, 0.3743, 0.5234, 0.3743],
         [0.8807, 0.4097, 0.8807, 0.4097],
         [0.5234, 0.3743, 0.5234, 0.3743],
         [0.8807, 0.4097, 0.8807, 0.4097]],

        [[0.3975, 0.4694, 0.3975, 0.4694],
         [0.5903, 0.7884, 0.5903, 0.7884],
         [0.3975, 0.4694, 0.3975, 0.4694],
         [0.5903, 0.7884, 0.5903, 0.7884]]])
>>> torch.tile(z,(2,2)).shape
torch.Size([3, 4, 4])

torch.transpose(input,dim0,dim1)→Tensor

转置tensor

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893,  0.5809],
        [-0.1669,  0.7299,  0.4942]])
>>> torch.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
        [-0.9893,  0.7299],
        [ 0.5809,  0.4942]])

torch.unbind(input,dim=0)→seq

移除一个tensor的维度,延着给定维度返回这个维度所有切片的元组

>>> torch.unbind(torch.tensor([[1, 2, 3],
>>>                            [4, 5, 6],
>>>                            [7, 8, 9]]))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))

>>> a = torch.randn([3,4])
>>> a
tensor([[ 0.8070, -0.4330, -1.0556,  0.2137],
        [-0.3494, -1.7364, -0.8648,  0.7615],
        [ 0.1815, -0.2857, -2.2358,  0.1023]])
>>> torch.unbind(a)
(tensor([ 0.8070, -0.4330, -1.0556,  0.2137]), tensor([-0.3494, -1.7364, -0.8648,  0.7615]), tensor([ 0.1815, -0.2857, -2.2358,  0.1023]))
>>> torch.unbind(a,dim=1)
(tensor([ 0.8070, -0.3494,  0.1815]), tensor([-0.4330, -1.7364, -0.2857]), tensor([-1.0556, -0.8648, -2.2358]), tensor([0.2137, 0.7615, 0.1023]))

torch.unsqueeze(input,dim)→Tensor

在特定维度上新增维度(增维度前后的tensor中元素不变),对比torch.sequeeze

>>> a = torch.rand([2,3,4])
>>> a
tensor([[[0.9525, 0.7392, 0.0100, 0.7057],
         [0.6858, 0.3234, 0.8738, 0.9654],
         [0.3634, 0.4648, 0.5675, 0.8380]],

        [[0.4137, 0.3462, 0.2843, 0.0108],
         [0.6482, 0.8213, 0.3431, 0.9067],
         [0.9824, 0.8066, 0.4383, 0.6881]]])
>>> torch.unsqueeze(a,dim=0).shape
torch.Size([1, 2, 3, 4])
>>> torch.unsqueeze(a,dim=1).shape
torch.Size([2, 1, 3, 4])

torch.where(condition,x,y)→Tensor

根据condition返回x或y,若condition成立→return x;若condition不成立→return y

#在本例中,x中>0的元素保留,<0的元素用1替换
>>> x = torch.randn(3,2)
>>> y = torch.ones(3,2)
>>> torch.where(x>0,x,y)
tensor([[1.0000, 1.0000],
        [1.0000, 1.0000],
        [1.0000, 0.0043]])

torch.manual_seed(seed)

int型随机种子

torch.bernoulli(input)

input为概率tensor,将其中每个元素放到伯努利分布中采样得到的结果return(0,1)

torch.normal(mean,std)

高斯

torch.rand

均匀分布,return[0,1)

torch.randint(low=0,high,size)

随机生成int tensor

torch.randperm(n)

得到一个随机组合

>>> torch.randperm(4)
tensor([2, 0, 3, 1])

你可能感兴趣的:(python,pytorch)