首页 > 编程知识 正文

pytorch numpy,softmax算法

时间:2023-05-06 10:15:25 阅读:173441 作者:107

argmax的目的是求出最大值所在的索引,这个函数是不可导出的

代替soft argmax,可以通过以下公式寻求指导:

输入的Tensor应如下图所示。

tensor (0.1000,0.3000,0.6000,2.1000,0.5500 ),dtype=torch.float64 )通过上述公式得到。

NP.sum(NP.exp(data )/NP.sum ) NP.exp (data ) ) NP.array ) [ 0,1,2,3,4 ] ) e=p*index''ok

从上面看的位置计算不正确的理由之一是,最大值的概率不够,或者通过相对于最大值增大减弱其他值的影响可以得到更正确的位置坐标。

可以看到,上式和softmax的期望只有一个区别:向量的每个元素都与beta相乘。

的坐标为2.99则为3,寻找具有极值的位置(坐标)的方法是微小的。 常用于图像特征点位置的提取。

importtorchimporttorch.nn.functionalasffromtorch.nn.parameterimportparameterimportnumpyasnpclassspatialsoftmax (torch ) channel,device,temperature=None, data_format='NCHW ' ) 3360SF self(__init__ ) ) self.data _ format=data _ format self.height=height self.width=width self.channel=channel self.device=device temperature ) else : self.temperature=1. pos _ x,pos_y=NP.meshgrid ) NP.pos_x,pos_y=NP.meshgrid ) self.width () pos _ x=torch.from _ numpy (pos _ x.reshape ) self.height*self.width ) ).float ) pos ) pos_x ) self.register_buffer )、pos_y )、pos_x )、register_buffer )、pos_y C*2) x _ 0y _0. if sef 3 ) ) ) ) ) ) ) ) 652 self.height * self.width (else : feature=feature.view (-1, self.height* self.width ) softmax_attention=dim=-1 ) self.pos _ x=self.pos _ x.to (self.device ) self.pos _ y=self softmax_attention,dim=1,keepdim=True ) expected _ y=torch.sum keep dim=true (expected _ xy=torch.cat [ 1 ) feature _ key points=expected _ xy elf.channel *2(return feature _ key points if _ name _=' _ main _ ' 3330 ) 1 )=10 data (0,2,1,2 )=10 layer=spatialsoftmax (3,3,temperature=1) print (layer ) data ) ) ) )。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。