softargmax(soft-argmax)

softargmax可以作为argmax的近似。

因为argmax是不可导的,而softargmax是可导的

或者写作

思想就是使得softmax之后的结果,实现最大的那个很大,无限接近于1,而其他的都很小,无限接近于0,从而再乘上对应位置的index,就能实现把最大位置的index取出来的效果了

但是可以看到,位置计算不够准确(2.57 -> 3),一个原因就是最大值的概率不够大,或者说增大相对最大值而减弱其他值的影响就可以得到更加准确的位置坐标。

所以有了下面的式子

在这里插入图片描述

 以beta=10为例子

就相当于除以温度τ

import torch
import torch.nn as nn
 
def soft_argmax(x, beta):
	"""
	Arguments: voxel patch in shape (batch_size, channel, H, W, depth)
	Return: 3D coordinates in shape (batch_size, channel, 3)
	"""
    #x: [bs,c,h,w]
	x = x.reshape(x.shape[0], x.shape[1], -1) #[bs,c,h*w]
	L = x.shape[2]
	soft_max = nn.functional.softmax(x*beta,dim=2)
	soft_max = soft_max.view(x.shape)
	indices = torch.arange(start=0, end=L).unsqueeze(0)
	soft_argmax = soft_max * indices
	indices = soft_argmax.sum(dim=2)  #[bs,c]
	return indices 
 
if __name__ == "__main__":
    x = torch.randn(1024,16,35,35)  #[bs, c, h, w]
    indices = soft_argmax(x, beta=10000.0) #[bs,c]
    print(indices)

Fdevmsy/PyTorch-Soft-Argmax: PyTorch implementation of Soft-Argmax in 1D/2D/3D (github.com)

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值