创建tensor
1.torch.eye(n,m)
创建一个对角线为1,其他为0的2维tensor
eg: x = torch.eys(3)
2.torch.linspace(start,end,steps)
创建一个1维tensor,start为3,end为10,共5个数
eg: x = torch.linspace(3,10,steps=5)
3.torch.ones(*size)
torch.zeros(*size)
返回一个全1的tensor,形状由size决定
eg: x = torch.ones(3,4)
4.torch.rand(*size)
返回从[0,1)均匀分布随机抽取的数,形状由size决定
eg: x = torch.rand(3,4)
5.torch.randn(*size)
返回从均值为0,方差为1的高斯分布中随机抽取的数,形状有size决定
eg: x = torch.randn(3,4)
6.torch.arange(start,end,step)
返回从start到end的tensor,步长为step
eg: x = torch.arange(3,10,2)
7.torch.from_numpy(ndarray)
把numpy类型数组转化为tensor
8.torch.Tensor(data) == FloatTensor()
创建一个FloatTensor
eg:x = torch.Tensor(1)
x:[0],这里的标量1传入的是维度
eg: x = torch.Tensor([1])
x:[1],这里传入的是tensor[1]值
9.torch.tensor(data)
这里的data可以是tuple,list,ndarray,scalar,根据原始的数据类型生成LongTensor,FloatTensor,DoubleTensor
eg:x = torch.tensor(1)
x:1,这里的的类型是LongTensor
基本操作
1.torch.cat((x,y),dim=-1)
在指定维度进行拼接
2.torch.chunk(tensor, chunks, dim=0)
与cat相反,在指定维度进行分割
chunks:分割的数目
3.torch.gather(input, dim, index)
根据index在指定的维度进行聚合
eg: t = torch.Tensor([[1,2],[3,4]])
x = torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
x: [1,1],[4,3]
4.torch.index_select(input,dim,index)
根据index从指定维度选取向量
5.torch.masked_select(input,mask)
取mask矩阵中为True的值
6.torch.squeeze(input,dim)
压缩指定为1的维度
7.torch.transpose(input,dim0,dim1)
对于指定的两个维度进行交换
torch.permute(index)
可以根据指定index进行交换
torch.t()
进行转置
8.torch.expand()
把tensor每个维度扩充为指定维度
torch.repeat()
把tensor每个维度重复多少倍
采样
1.torch.normal(mean,std,*size)
返回一个指定维度的高斯分布
2.torch.bernoulli(input)
input为一个元素值为0到1的tensor,返回一个2项分布,有指定概率为1
3.tensor.uniform_(0,1)
返回一个0到1的均匀分布
序列化
保存一个对象到硬盘上
torch.save(model.state_dict(),"checkpoint.pkl")
加载model参数
model.load_state_dict(torch.load("checkpoint.pkl"))
数学运算
1.torch.clamp(input,min,max):把input夹紧到min-max区间
torch.ceil(); torch.abs(); torch.exp(); torch.floor(); torch.log(); torch.pow()
torch.fmod():求余数
2.torch.add(input,value):各个元素加value
torch.add(input,value,other): 各个元素为input + (value * other)
3.torch.div(input,value): 各个元素除value
torch.div(input,other): 对应的位置相除
4.torch.mul(input,other):各个元素相乘
torch.mm(m1,m2):矩阵乘
torch.bmm(t1,t2):batch个矩阵相乘
5.torch.cumprod(input,dim)
求指定维度的累计乘
torch.cumsum(input,dim)
求指定维度的累计和
6.torch.dist(input,other,p)
返回p范式的距离;p为2则是l2距离
比较操作
1.torch.eq(input,other):比较元素是否相等
2.torch.ge(input,other): 是否大于等于;torch.gt():大于
3.torch.le(input,other): 是否小于等于; torch.lt() 小于
3.torch.ne(input,other):不等于
4.torch.kthvalue(input,k,dim):返回dim维度第k个最小值(value,indices)
5.torch.topk(input,k,dim): 返回topk个(value,indices)
其他操作
torch.diag(input,diagonal=0):返回以input为对角线的tensor
torch.trace(input): 返回tensor的迹
torch.tril(input,k=0):返回下三角;k = 1上元素多,k = -1时不包括对角线元素少
torch.triu(input,k):返回上三角
torch.bmm(input,other):一个batch的进行矩阵乘积
torch.dot(input,other):向量进行点乘
损失函数
1 torch.nn.L1Loss(size_average=True,reduce=True)
计算输入x与目标y之间距离的绝对值(曼哈顿距离)的平均值
reduce=True:表明返回的是一个值,否则是一个同纬度的向量
size_average=True:表明是否要除以元素个数求平均
2 torch.nn.MSELoss(size_average=True, reduce=True)
计算输入x和目标y之间的l2距离(欧式距离)的平均值
3 nn.CrossEntropyLoss = nn.LogSoftmax() + nn.NLLLoss()
计算输入x和目标y之间的交叉熵,也就是正确答案的负对数概率
4 nn.functional.kl_div(reduction="sum")
计算两个分布之间的差异
reduction:sum为求和,mean为求平均