【问题标题】:What is the difference between Kelas MSE loss and my own loss function?Kelas MSE损失和我自己的损失函数有什么区别?
【发布时间】:2019-07-23 16:39:51
【问题描述】:

我是 Keras 的新手。我尝试在 Keras 中制作自定义损失函数。 但是我的代码有问题。 Keras 工作,但估计结果很奇怪。我应该在哪里更改代码?

我只是尝试将 MSE 实现为自定义损失函数。

这是一个损失函数部分。

def loss_function(ytrue, ypred):

    qx_true = ytrue[:, 0]
    qx_pred = ytrue[:, 0]
    qy_true = ytrue[:, 1]
    qy_pred = ytrue[:, 1]
    qz_true = ytrue[:, 2]
    qz_pred = ytrue[:, 2]
    qw_true = ytrue[:, 3]
    qw_pred = ytrue[:, 3]
    tx_true = ytrue[:, 4]
    tx_pred = ypred[:, 4]
    ty_true = ytrue[:, 5]
    ty_pred = ypred[:, 5]
    tz_true = ytrue[:, 6]
    tz_pred = ypred[:, 6]

    loss = ((tx_true - tx_pred) * (tx_true - tx_pred) 
        + (ty_true - ty_pred) * (ty_true - ty_pred) 
        + (tz_true - tz_pred) * (tz_true - tz_pred) 
        + (qx_true - qx_pred) * (qx_true - qx_pred) 
        + (qy_true - qy_pred) * (qy_true - qy_pred) 
        + (qz_true - qz_pred) * (qz_true - qz_pred) 
        + (qw_true - qw_pred) * (qw_true - qw_pred)) / 7

    return loss

这是一个调用损失函数部分

    model.add(Dense(7, name='output'))
    model.compile(loss=loss_function, optimizer=keras.optimizers.Adam())

当我尝试 Keras 原始损失函数时,它可以工作

    model.add(Dense(7, name='output'))
    model.compile(loss=keras.losses.MSE, optimizer=keras.optimizers.Adam())

损失函数的输入是平移的三个参数和四元数的四个参数。当我尝试使用 keras.losses.MSE 时,它起作用了,我也在尝试做同样的事情。

错在哪里?谢谢

【问题讨论】:

    标签: tensorflow keras deep-learning loss-function


    【解决方案1】:

    我相信这个

    qx_true = ytrue[:, 0]
    qx_pred = ytrue[:, 0]
    qy_true = ytrue[:, 1]
    qy_pred = ytrue[:, 1]
    qz_true = ytrue[:, 2]
    qz_pred = ytrue[:, 2]
    qw_true = ytrue[:, 3]
    qw_pred = ytrue[:, 3]
    

    应该是

    qx_true = ytrue[:, 0]
    qx_pred = ypred[:, 0]
    qy_true = ytrue[:, 1]
    qy_pred = ypred[:, 1]
    qz_true = ytrue[:, 2]
    qz_pred = ypred[:, 2]
    qw_true = ytrue[:, 3]
    qw_pred = ypred[:, 3]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2018-08-09
      • 2020-06-27
      • 2021-01-24
      • 2018-06-25
      • 2018-01-20
      • 2020-01-18
      相关资源
      最近更新 更多