torch复现论文简明笔记

1)常数初始化:
x = F.normalize(x, p=2, dim=1)按行计算
x = F.normalize(x, p=2, dim=0)按列计算
torch.empty(size)返回形状为size的空tensor
torch.zeros(size)全部是0的tensor
torch.zeros_like(input)返回跟input的tensor一个size的全零tensor
torch.ones(size)全部是1的tensor
torch.ones_like(input)返回跟input的tensor一个size的全一tensor
torch.arange(start=0, end, step=1)返回一个从start到end的序列,可以只输入一个end参数,就跟python的range()一样了。
torch.full(size, fill_value)这个有时候比较方便,把fill_value这个数字变成size形状的张量
2)随机抽样(随机初始化):
torch.rand(size) [0,1)内的均匀分布随机数
torch.rand_like(input)返回跟input的tensor一样size的0-1随机数
torch.randn(size)返回标准正太分布N(0,1)的随机数
torch.normal(mean, std, out=None)正态分布。这里注意,mean和std都是tensor,返回的形状由mean和std的形状决定,一般要求两者形状一样。如果,mean缺失,则默认为均值0,如果std缺失,则默认标准差为1.
3)tensor的切片、合并、变形、抽取操作
torch.cat(seq, dim=0, out=None),把一堆tensor丢进去,按照dim指定的维度拼接、堆叠在一起.
示例:
In [70]: x = torch.tensor([[1,2,3]])
In [71]: x
Out[71]: tensor([[1, 2, 3]])
 #按第0维度堆叠,对于矩阵,相当于“竖着”堆
In [72]: print(torch.cat((x,x,x),0))
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
 #按第1维度堆叠,对于矩阵,相当于“横着”拼
In [73]: print(torch.cat((x,x,x),1)) 
tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3]])
torch.chunk(tensor, chunks, dim=0)把tensor切成块,数量由chunks指定。
示例:
In [74]: a = torch.arange(10)
In [75]: a
Out[75]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [76]: torch.chunk(a,4)
Out[76]: (tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7, 8]), tensor([9]))
切块还有torch.split(tensor, split_size_or_sections, dim=0)具体区别大家自行查阅文档
按index选择:torch.index_select(input, dim, index, out=None)
按mask选择:torch.masked_select(input, mask, out=None)

经常会使用的“压扁”函数:torch.squeeze(input),压缩成1维。注意,压缩后的tensor和原来的tensor共享地址
改变形状:torch.reshape(input, shape)以及tensor.view(shape).前者是把tensor作为函数的输入,后者是任何tensor的函数。实际上,二者的返回值,都只是让我们从另一种视角看某个tensor,所以不会改变本来的形状,除非你把结果又赋值给原来的tensor。下面给一个例子对比二者的用法:
示例:
In [82]: a
Out[82]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 单纯的调用view函数:
In [83]: a.view(2,5)
Out[83]:
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
# a的形状并不会变化
In [84]: print(a) 
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 试试reshape函数:
In [86]: torch.reshape(a,[5,2])
Out[86]:
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
# a的形状依然不会变化:
In [87]: a
Out[87]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
要想让a的形状变化,比如把结果赋值给a,比如a = a.view(2,5)。
4)基本数学操作
加法直接加:x+y   torch.add(x,y)     torch.add(input, value, out=None)
乘法:torch.mul(input, other, out=None)用input乘以other
除法:torch.div(input, other, out=None)用input除以other
指数:torch.pow(input, exponent, out=None)
开根号:torch.sqrt(input, out=None)
四舍五入到整数:torch.round(input, out=None)
argmax函数:torch.argmax(input, dim=None, keepdim=False)返回指定维度最大值的序号,dim给定的定义是:the demention to reduce.也就是把dim这个维度的,变成这个维度的最大值的index。例如:
sigmoid函数:torch.sigmoid(input, out=None)
tanh函数:torch.tanh(input, out=None)
torch.abs(input, out=None)取绝对值
torch.ceil(input, out=None)向上取整,等于向下取整+1
torch.clamp(input, min, max, out=None)刀削函数,把输入数据规范在min-max区间,超过范围的用min、max代替
5)Torch Tensor与Numpy的互相转换
Tensor-->Numpy   直接用.numpy()即可。
Numpy-->Tensor   用torch.from_numpy()来转换。
示例:
In [11]: a = torch.ones(2,4)
In [12]: a
Out[12]:
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.]])
In [13]: b = a.numpy()
In [14]: b
Out[14]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
In [15]: a.add_(1)
Out[15]:
tensor([[2., 2., 2., 2.],
        [2., 2., 2., 2.]])
In [16]: b
Out[16]:
array([[2., 2., 2., 2.],
       [2., 2., 2., 2.]], dtype=float32)

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
 

 

你可能感兴趣的:(深度学习)