tensorflow - 从损失函数记录标量

标签 tensorflow machine-learning neural-network deep-learning keras

我正在研究在 Keras 中实现的 ML 模型。对于这个模型,我编写了一个自定义损失函数,其中损失是其他 3 个变量(a_cost,b_cost,c_cost)的性能之和。损失函数有效,但我想稍微调整一下,为此我想看看这 3 个其他变量的行为如何。如何记录这些标量以便它们可以显示在 TensorBoard 中?

def custom_cost(y_true, y_pred):
    # compute a_cost, b_cost, c_cost

    cost = a_cost + b_cost + c_cost
    return cost

# ..build model...
model.compile(loss=custom_cost, optimizer=optimizers.Adam())
tensorboard = callbacks.TensorBoard(log_dir="./logs", write_graph=True)
tensorboard.set_model(model)
model.fit_generator(generator=custom_generator, epochs=100, steps_per_epoch=180,     callbacks=[tensorboard], verbose=True)

最佳答案

当计算a_costb_costc_cost时,您可以定义三个单独的函数来分别计算它们。让:

def a_cost(y_true, y_pred):
    # compute a_cost
    ...
    return a_cost

def b_cost(y_true, y_pred):
    # compute b_cost
    ...
    return b_cost

def c_cost(y_true, y_pred):
    # compute c_cost
    ...
    return c_cost

现在就像将这三个函数添加为 metrics 一样简单:

model.compile(..., metrics=[a_cost, b_cost, c_cost])

关于tensorflow - 从损失函数记录标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511359/

相关文章:

python - 通过梯度下降在每一步更新的自定义损失函数

python - Tensorflow 兼容模块问题?

python - 用于评估的 Tensorflow 平均绝对误差 (MAE)

machine-learning - SVM 的自定义内核,何时应用它们?

python - 将 Dropout 添加到测试/推理阶段

python - Keras:如何访问特定索引以进行乘法

python - sess.run 动态增加内存使用量

Raspberry Pi 上的 Tensorflow Lite - 安装

python - 如何选择标签编码分类变量来创建虚拟变量?

python - 如何将字符串转换为 float ?值错误: could not convert string to float: '0,25691372'