简体   繁体   English

如何使用 tf-keras-vis 正确实现损失 function?

[英]How to properly implement a loss function using tf-keras-vis?

I am trying to understand the following snippet of code demonstrated in this example of tf-keras-vis use.我试图理解此 tf-keras-vis 使用示例中演示的以下代码片段。

I am trying to create saliency maps for a project I have been working on.我正在尝试为我一直从事的项目创建显着图。 The snippet I am having issues with is as follows:我遇到问题的代码段如下:

Define necessary functions定义必要的功能

Define Loss functions定义损失函数

You MUST define loss function that return target scores.您必须定义返回目标分数的损失 function。 Here, it returns the scores corresponding Goldfish, Bear, Assault Rifle.在这里,它返回了金鱼、熊、突击步枪对应的分数。

# The 'output' variable refer to the output of the model,
# so, in this case, `output` shape is `(3, 1000)` i.e., (samples, classes).
def loss(output):
    # 1 is the imagenet index corresponding to Goldfish, 294 to Bear and 413 to Assault Rifle.
    return (output[0][1], output[1][294], output[2][413])

How is this exactly calculating loss?这究竟是如何计算损失的? I need to understand this so I can modify it to apply it to my own model. If I simply type:我需要了解这一点,以便我可以修改它以将其应用于我自己的 model。如果我只是键入:

def loss(output):
    return output

Something resembling a saliency map is output but I need to understand what is going on with the original code piece类似于显着性 map 的东西是 output 但我需要了解原始代码片段发生了什么

From how I understand the code in the linked example, the "loss" function simply returns the.network outputs that you're interested in analyzing.根据我对链接示例中代码的理解,“损失” function 只是返回您有兴趣分析的 .network 输出。

In the sample code:在示例代码中:

def loss(output):
    # 1 is the imagenet index corresponding to Goldfish, 294 to Bear and 413 to Assault Rifle.
    return (output[0][1], output[1][294], output[2][413])

the "loss" is set up to generate three saliency images, one per input image (notethat it could be more). “损失”设置为生成三个显着性图像,每个输入图像一个(注意它可能更多)。 For the first one, since the image is a goldfish image, you return the output matching the goldfish label ( output[0][1] , where image 0 is the goldfish one and label 1 is goldfish) and similarly you return the outputs of class "Bear" and "Assault Rifle" for the other two sample images.对于第一个,因为图像是金鱼图像,您返回匹配金鱼 label 的 output( output[0][1] ,其中图像 0 是金鱼,label 1 是金鱼),同样您返回输出class 其他两个示例图像的“熊”和“突击步枪”。

So, provided that you know what labels the sample images you're feeding have, just return the model's output for those labels.因此,如果您知道您提供的样本图像有哪些标签,只需返回模型的 output 以获取这些标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM