赞
踩
view()用于改变tensor的形状
https://blog.csdn.net/weixin_39504171/article/details/106356977
四舍五入取整
四维gather
https://stackoverflow.com/questions/53471716/index-pytorch-4d-tensor-by-values-in-2d-tensor
https://zhuanlan.zhihu.com/p/352877584
https://blog.csdn.net/qq_34806812/article/details/89388210
用来“截断”tensor,使每个元素都保持在min~max范围内
torch.clamp(input,min,max,out=None)
沿着第dim维度将seq中的tensor合并成一个tensor
torch.cat(seq,dim=0,out=None)
torch.chunk(tensor,chunks,dim=0)
对于tensor来说,可能存在某个维度上的大小为1,比如tensor a的shape为[2,1,1,2,2],那么对它使用squeeze后,将压缩所有大小为0的维度使他的shape变为[2,2,2]。如果指定了dim则指处理该维度,比如dim=0,则保持不变,因为该维度大小不为1,反之指定dim=2时,由于该维度大小为1,则压缩为[2,1,2,2]
torch.squeeze(input,dim=None,out=None)
和squeeze相反,用来添加维度,必须指定dim,添加到第dim维上
torch.unsqueeze(input,dim=None,out=None)
这个常用与调整图像tensor,如输入图像数据为(n,224,224,3),
实际pytorch中网络需要接受(n,3,224,224)的图像,就可以使用permute 调整维度顺序。其中dim*接受改变后各维度顺序的参数,比如[0,3,1,2]即可将最后一个维度提到前面。
torch.tensor.permute(dim*)
使用nn.ConstantPad2d,前面tuple里的四个参数左右上下的填充大小,最后指定填充的值。
self.pad = nn.ConstantPad2d((0, 1, 0, 1), 0)
利用pytorch提供的torch.nn.CosineSimilarity函数,但是该函数只接受shape相同的tensor,所以对于shape不同的tensor,需要通过unsqueeze和expand处理成相同的shape。
cos = torch.nn.CosineSimilarity(dim=2, eps=1e-6) def cos_dist(x, y): # x: N x D # y: M x D n = x.size(0) m = y.size(0) d = x.size(1) assert d == y.size(1) x = x.unsqueeze(1).expand(n, m, d) y = y.unsqueeze(0).expand(n, m, d) return -cos(x,y)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。