pytorch安装
登陆官网:PyTorch
Mac:conda install pytorch torchvision -c pytorch
Ubutun: conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
windows: conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
1.torch.normal(means,std,out)
means (Tensor) – 均值
std (Tensor) – 标准差
out (Tensor) – 可选的输出张量
这个是官网给出的解释,大意是返回一个张量,张量里面的随机数是从相互独立的正态分布中随机生成的。
例如:
n_data=torch.ones(100,2)
x0=torch.normal(2*n_data,1)
#每个元素是从 均值=2*n_data中对应位置的取值,标准差为1的正态分布中随机生成的
2.torch.cat()
torch.cat是将两个张量(tensor)拼接在一起
例如:
x=torch.cat((x0,x1),0).type(torch.FloatTensor)
#根据维度0合并(竖着拼)
x=torch.cat((x0,x1),1).type(torch.FloatTensor)
#根据维度1合并(横着拼)
3.torch.linear()
torch.linspace(start, end, steps=100, out=None)
:
返回一个1维张量,包含在start
和end
上均匀间隔的steps
个点
- start (float) -序列起点
- end (float) - 序列终点
- steps (int) - 在
start
与end
间生成的样本数 - out (Tensor, optional) - 结果张量
4.torch.manual_seed()
为CPU设置种子用于生成随机数,以使得结果是确定的 ,也就是每次生成的随机数是固定的,否则每次生成的随机数都是不固定
对于GPU来说
torch.cuda.manual_seed(args.seed)#单个GPU
torch.cuda.manual_seed_all()#为所有GPU设置种子
5.torch.squeeze()与torch.unsqueeze():
torch.squeeze() 这个函数主要对数据的维度进行压缩,去掉维数为1的的维度,比如是一行或者一列这种,一个一行三列(1,3)的数去掉第一个维数为一的维度之后就变成(3)行。squeeze(a)就是将a中所有为1的维度删掉。不为1的维度没有影响。a.squeeze(N) 就是去掉a中指定的维数为一的维度。还有一种形式就是b=torch.squeeze(a,N) a中去掉指定的维数为一的维度。
torch.unsqueeze()主要是对数据维度进行扩充。给指定位置加上维数为一的维度,比如原本有个三行的数据(3),在0的位置加了一维就变成一行三列(1,3)。a.squeeze(N) 就是在a中指定位置N加上一个维数为1的维度。还有一种形式就是b=torch.squeeze(a,N) a就是在a中指定位置N加上一个维数为1的维度。
比如:
x=torch.unsqueeze(torch.linspace(1,10,10),dim=1)#生成的数据类型是(1,10)
6.torch.gather()
gather(input,dim, index)的作用就是在指定的维度dim上根据index检索input的数值,当dim=0指的是行,逐行根据index读取input位置的数值,dim=1指的是列,逐列根据index检索input的数值。
例子:
a=torch.arange(0,6).view(2,3)
print(a)
0,1,2
3,4,5
index=([[0,1]])
b=a.gather(0,index)
print(b)
0,4
7.torch.sum()
torch.sum(input, dim, out=None) → Tensor
- input (Tensor) – 输入张量
- dim (int) – 缩减的维度
- out (Tensor, optional) – 结果张量
- dim(0)按列求和,结果个数和列数相同
- dim(1)按行求和,结果个数和行数相同
8.detach()
detach()就是截断方向传播的梯度流
官方解释:
-
def detach(self):
-
"""Returns a new Variable, detached from the current graph.
-
Result will never require gradient. If the input is volatile, the output
-
will be volatile too.
-
.. note::
-
Returned Variable uses the same data tensor, as the original one, and
-
in-place modifications on either of them will be seen, and may trigger
-
errors in correctness checks.
-
"""
-
result = NoGrad()(self) # this is needed, because it merges version counters
-
result._grad_fn = None
-
就是指将某个node变成不需要梯度的Variable
-
在MDNet算法中的硬样本挖掘法实现
-
# hard negative mining,在训练验证过程判别硬样本 if batch_neg_cand > batch_neg: # model.eval(),disables tracking of gradients in autograd(禁用自动梯度跟踪), model.eval() for start in range(0, batch_neg_cand, batch_test): end = min(start + batch_test, batch_neg_cand) #torch.no_grad() 更改调用它的模块的forward()行为,即仅仅是权重更新,不做前向过程比,autograd不做记录 with torch.no_grad(): score = model(batch_neg_feats[start:end], in_layer=in_layer) if start==0: #detach就是截断反向传播的梯度流,不进行梯度更新 neg_cand_score = score.detach()[:, 1].clone() else: neg_cand_score = torch.cat((neg_cand_score, score.detach()[:, 1].clone()), 0) _, top_idx = neg_cand_score.topk(batch_neg) batch_neg_feats = batch_neg_feats[top_idx] model.train()
9.torch.clamp()
torch.clamp(input, min, max, out=None)
将input张量的每一个元素大小范围限定在(min,max)之间,并返回结果到一个新张量