Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用tf keras vis正确实现损失函数?_Python_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python 如何使用tf keras vis正确实现损失函数?

Python 如何使用tf keras vis正确实现损失函数?,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,我试图理解tf keras vis使用中演示的以下代码片段 我正在尝试为我一直在从事的一个项目创建显著性地图。我遇到的问题如下: 定义必要的功能 定义损失函数 必须定义返回目标分数的损失函数。在这里,它返回相应的分数金鱼,熊,突击步枪 # The 'output' variable refer to the output of the model, # so, in this case, `output` shape is `(3, 1000)` i.e., (samples, classes)

我试图理解tf keras vis使用中演示的以下代码片段

我正在尝试为我一直在从事的一个项目创建显著性地图。我遇到的问题如下:

定义必要的功能

定义损失函数

必须定义返回目标分数的损失函数。在这里,它返回相应的分数金鱼,熊,突击步枪

# 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])

这究竟是如何计算损失的?我需要理解这一点,以便我可以修改它以将其应用于我自己的模型。如果我只是简单地键入:

def loss(output):
    return output

输出类似于显著性图的内容,但我需要了解原始代码段的情况,从我对链接示例中代码的理解来看,“loss”函数只返回您感兴趣分析的网络输出

在示例代码中:

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])
“丢失”设置为生成三个显著性图像,每个输入图像一个(注意可能更多)。对于第一个,由于图像是金鱼图像,因此返回与金鱼标签匹配的输出(
output[0][1]
,其中图像0是金鱼,标签1是金鱼),同样,对于其他两个示例图像,返回“Bear”和“突击步枪”类的输出


因此,如果您知道正在馈送的样本图像有哪些标签,只需返回这些标签的模型输出。

Hello GPhilo,感谢您的回复。所以在我的例子中,我预测图像是1还是0。在我的例子中,似乎我可以迭代,而不是有3个输出,因为每个图像我都在预测它是1还是0。所以在代码中,我需要从损失函数中输出的只是每个图像的模型输出?我正在努力理解这与“丢失”有什么关系,甚至这种函数的意义我不知道实现的方法是如何工作的,但如果我理解正确,它们产生的可视化就是“解释”模型使用图像的哪些区域来产生特定的输出。计算它们的方法是基于研究输出如何改变输入像素,因此每个图像特定于每个图像的一个输出。您可以查看所有输出,当然,我只是不知道这些视图是否有意义。