关于 Code/4_viewer/6_hook_for_grad_cam.py 中comp_class_vec计算Loss的疑惑
thgpddl opened this issue · comments
thgpddl commented
源代码如下:
def comp_class_vec(ouput_vec, index=None):
if not index:
index = np.argmax(ouput_vec.cpu().data.numpy()) # int
else:
index = np.array(index)
index = index[np.newaxis, np.newaxis] # (1,1) ndarray
index = torch.from_numpy(index) # (1,1) Tensor
one_hot = torch.zeros(1, 1000).scatter_(1, index, 1) # 热编码 (1,1000) Tensor 全0和一个和1
one_hot.requires_grad = True
class_vec = torch.sum(one_hot * output) # 求损失
return class_vec
按照我对该Loss计算方法的理解,
比如5分类,ouput_vec最大最大概率为pos=3的类别,
ouput_vec=[0.1,0.1,0.6,0.1,0.1]
one_hot = [0,0,1,0,0]
计算torch.sum(one_hot * output)=0.6
如果pos=3类别的概率更高,计算出的torch.sum(one_hot * output)会越大。但是按直观来理解,网络判断正确的概率更高了,所以Loss应该更低才对啊?
TingsongYu commented