【问题标题】:How to debug custom loss function in Keras?如何在 Keras 中调试自定义损失函数?
【发布时间】:2018-03-09 20:26:02
【问题描述】:

我创建了带参数的自定义损失函数。

def w_categorical_crossentropy(weights):
  def loss(y_true, y_pred):
  print(weights)
  print("----")
  print(weights.shape)
  final_mask = K.zeros_like(y_pred[:, 0])
  y_pred_max = K.max(y_pred, axis=1)
  y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
  y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
  return K.categorical_crossentropy(y_pred, y_true)
return loss

现在,我需要控制权重参数值,但打印功能无法正常工作。有没有办法打印重量值?

【问题讨论】:

  • 最好的调试方法是创建一些简单的假数据,你知道结果是什么(或者可以很容易地手动计算),并将函数的结果与真实值进行比较。

标签: debugging printing callback keras tensor


【解决方案1】:

我有时会做的(肯定不是最好的解决方案,也不总是可能的)只是用 np 替换 K 后端并用一些简单的数据对其进行测试。这是一个例子

原 Keras 函数:

def loss(y_true, y_pred):
    means = K.reshape(y_pred[:, 0], (-1, 1))
    stds = K.reshape(y_pred[:, 1], (-1, 1))
    var = K.square(stds)
    denom = K.sqrt(2 * np.pi * var)
    prob_num = - K.square(y_true - means) / (2 * var)
    prob = prob_num - denom
    r = K.exp(prob - old_prediction)
    return -K.mean(K.minimum(r * advantage, K.clip(r, min_value=1 - self.LOSS_CLIPPING, max_value=1 + self.LOSS_CLIPPING) * advantage))

测试功能:

def loss(y_true, y_pred):
    means = np.reshape(y_pred[:, 0], (-1, 1))
    stds = np.reshape(y_pred[:, 1], (-1, 1))
    var = np.square(stds)
    print(var.shape)
    denom = np.sqrt(2 * np.pi * var)
    print(denom.shape)
    prob_num = - np.square(y_true - means) / (2 * var)
    prob = prob_num - denom
    r = np.exp(prob - old_prediction)
    print(r.shape)
    cliped = np.minimum(r * advantage, np.clip(r, a_min=1 - LOSS_CLIPPING, a_max=1 + LOSS_CLIPPING) * advantage)
    print(cliped.shape)
    return -np.mean(cliped)

测试它:

LOSS_CLIPPING = 0.2
y_pred = np.array([[2,1], [1, 1], [5, 1]])
y_true = np.array([[1], [3], [2]])
old_prediction = np.array([[-2], [-5], [-6]])
advantage = np.array([[ 0.51467506],[-0.64960159],[-0.53304715]])
loss(y_true, y_pred)

上面运行后,结果:

(3, 1)
(3, 1)
(3, 1)
(3, 1)
0.43409555193679816

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 2020-12-19
    • 2017-12-18
    • 2020-03-27
    • 1970-01-01
    • 2021-09-21
    • 2018-10-28
    • 2017-12-29
    • 2019-01-18
    相关资源
    最近更新 更多