【问题标题】:Mean absolute error in TensorFlow without built-in functions没有内置函数的 TensorFlow 中的平均绝对误差
【发布时间】:2019-05-31 20:25:46
【问题描述】:

我必须在不调用内置函数的情况下使用平均绝对误差来实现损失函数。下面的代码是否正确?因为我的损失值很快从 28.xx 变成了 0.00028。

同时,RMSE等其他损失函数具有更标准的损失曲线

loss = tf.reduce_sum(tf.abs(y_pred - Y) / nFeatures)

【问题讨论】:

  • 您应该除以示例数而不是特征数 (nFeatures)。
  • 示例数是多少?是行数吗?
  • 通常是输入张量的第一个维度(批量维度)。如果您的输入是 [batch, num_features] 的形状,是的,它是行数。

标签: python tensorflow


【解决方案1】:

您可以根据 MA​​E 公式实现自己的丢失函数:

import tensorflow as tf
MAE = tf.reduce_mean(tf.abs(y_true - y_pred))

您也可以在answer 中查看自定义损失函数

import numpy as np
MAE = np.average(np.abs(y_true - y_pred), weights=sample_weight, axis=0)

from tensorflow.python.ops import math_ops
MAE = math_ops.abs(y_true - y_pred)

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 2019-01-02
    • 2021-04-10
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多