Skip to content

关于 Code/4_viewer/6_hook_for_grad_cam.py 中comp_class_vec计算Loss的疑惑 #44

Description

@thgpddl

源代码如下:

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应该更低才对啊?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions