python - Keras 中的自定义损失函数仅运行一次

标签 python keras loss-function

我在 Keras 中定义了一个自定义损失函数,如下所示:

def loss_function_test(yTrue, yPred):
    global i
    i += 1
    print("\n")
    print("Loss Function:", i, " ----- ", yTrue, yPred)
    print("\n")
    res = k.sum(yTrue - yPred)
    return res

并设置如下:

model.compile(loss=loss_function_test,
              optimizer='sgd',
              metrics=['accuracy'])

问题是检查损失函数代码中的global iprint函数,我意识到当我调用时,该函数在训练中仅被调用一次>模型.fit。即使我调试它,它也只被调用一次。看起来损失函数可能只在编译时被调用。谁能解释一下为什么吗?

最佳答案

构造损失函数 op 的函数仅运行一次。

如果您想查看 tensorflow 评估操作的频率,您需要添加 tf.Print运算到 tensorflow 图中。

关于python - Keras 中的自定义损失函数仅运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020172/

相关文章:

python - 如何让 Plone 读取我的 pythonrc?

python - 使用 python 创建数字模式

python - 如何使用 Python 将本地数据(图像)输入我的 Keras 网络?

python - OptKeras (Keras Optuna Wrapper) - 在我自己的类中使用 optkeras,AttributeError : type object 'FrozenTrial' has no attribute '_field_types'

python - 如何看待 Keras 中提前停止导致的最佳纪元的损失?

python - 自动将一些文本附加到每条日志消息

multithreading - 在带有线程的 Flask 应用程序中使用 Keras 模型

c++ - Mxnet C++ API的输出值

python - 在pytorch的神经网络中将参数限制为-1、0或1

python - Tensorflow - 带有 sample_weight 的自定义损失函数