【问题标题】:Custom loss function implementation自定义损失函数实现
【发布时间】:2017-03-13 22:49:39
【问题描述】:

我正在尝试实现自己的新损失函数。 当我尝试调试它(或在其中打印)时,我注意到它仅在代码的模型创建部分被调用一次。

如果在拟合模型时无法将代码运行到此函数中,我如何知道 y_pred 和 y_true 包含的内容(形状、数据等)?

我写了这个损失函数:

def my_loss(y_true, y_pred):
    # run over the sequence, jump by 3
    # calc the label
    # if the label incorrect punish

    y_pred = K.reshape(y_pred, (1, 88, 3))

    y_pred = K.argmax(y_pred, axis=1)

    zero_count = K.sum(K.clip(y_pred, 0, 0))
    one_count = K.sum(K.clip(y_pred, 1, 1))
    two_count = K.sum(K.clip(y_pred, 2, 2))

    zero_punish = 1 - zero_count / K.count_params(y_true)
    one_punish = 1- one_count/ K.count_params(y_true)
    two_punish = 1- two_count/ K.count_params(y_true)

    false_arr = K.not_equal(y_true, y_pred)

    mask0 = K.equal(y_true, K.zeros_like(y_pred))
    mask0_miss = K.dot(false_arr, mask0) * zero_punish

    mask1 = K.equal(y_true, K.ones_like(y_pred))
    mask1_miss = K.dot(false_arr, mask1) * one_punish

    mask2 = K.equal(y_true, K.zeros_like(y_pred)+2)
    mask2_miss = K.dot(false_arr, mask2) * two_punish

    return K.sum(mask0_miss) + K.sum(mask1_miss) + K.sum(mask2_miss)

它失败了:

theano.gof.fg.MissingInputError: A variable that is an input to the graph was 
neither provided as an input to the function nor given a value. A chain of 
variables leading from this input to an output is [/dense_1_target, Shape.0]. 
This chain may not be unique
Backtrace when the variable is created:

我该如何解决?

【问题讨论】:

  • 您能否显示相关的故障代码,以便我们继续处理?您目前的问题基本上是在问“当 B 不起作用时,我该怎么做 A?”而不是“我如何让 B 再次工作?”

标签: python keras theano


【解决方案1】:

您必须了解 Theano 是一种符号语言。例如,当我们在 Keras 中定义如下损失函数时:

def myLossFn(y_true, y_pred):
    return K.mean(K.abs(y_pred - y_true), axis=-1)

Theano 只是在计算图中制定了一个符号规则,它将在它获得值时执行,即当您使用一些小批量训练模型时。

关于如何调试模型的问题,您可以使用theano.function。现在,您想知道您的损失计算是否正确。您执行以下操作。

您可以实现损失函数的 python/numpy 版本。将两个随机向量传递给您的 numpy-loss-function 并获得一个数字。要验证 theano 是否给出几乎相同的结果,请定义如下内容。

import theano
from theano import tensor as T
from keras import backend as K

Y_true = T.frow('Y_true')
Y_pred = T.fcol('Y_pred')
out = K.mean(K.abs(Y_pred - Y_true), axis=-1)

f = theano.function([Y_true, Y_pred], out)

# creating some values
y_true = np.random.random((10,))
y_pred = np.random.random((10,))

numpy_loss_result = np.mean(np.abs(y_true-y_pred))
theano_loss_result = f(y_true, y_pred)

# check if both are close enough
print numpy_loss_result-theano_loss_result # should be less than 1e-5

基本上,theano.function 是一种放置值和评估这些符号表达式的方法。我希望这会有所帮助。

【讨论】:

  • 我的问题是当我运行拟合时,损失函数没有被调用 - 或者我看不到它?
  • 你看不到它。也许在运行 fit 函数时还有另一种调试方式,但我更喜欢上面所说的方式。或者,您可以运行train_on_batch() 并使用我的方法来调试每个批次。
  • 好的..这有点奇怪,没有好的方法可以做到这一点..>
猜你喜欢
  • 1970-01-01
  • 2019-12-16
  • 2019-06-13
  • 2020-07-29
  • 1970-01-01
  • 2021-10-25
  • 2018-05-25
  • 2019-04-10
  • 2021-07-27
相关资源
最近更新 更多