如何在 Keras 中实现自适应损失?

How to implement an adaptive loss in Keras?

我正在尝试使用 Keras 来实现 A General and Adaptive Robust Loss Function 中完成的工作。作者提供了处理困难细节的 tensorflow 代码。我只是想在 Keras 中使用他的预建功能。

他的自定义损失函数正在学习控制损失函数形状的参数'alpha'。除了训练期间的损失外,我还想跟踪 'alpha'。

我对 Keras 自定义损失函数和使用包装器有些熟悉,但我不完全确定如何使用回调来跟踪 'alpha'。下面是我会选择如何天真地在 Keras 中构造损失函数。但是我不确定我将如何访问 'alpha' 进行跟踪。

From the provided tensorflow code,函数lossfun(x) returns一个元组。

def lossfun(x,
            alpha_lo=0.001,
            alpha_hi=1.999,
            alpha_init=None,
            scale_lo=1e-5,
            scale_init=1.,
            **kwargs):
    """
    Returns:
        A tuple of the form (`loss`, `alpha`, `scale`).
    """
def customAdaptiveLoss(): 
    def wrappedloss(y_true,y_pred):
        loss, alpha, scale = lossfun((y_true-y_pred))  #Author's function
        return loss
    return wrappedloss

Model.compile(optimizer = optimizers.Adam(0.001),
                        loss = customAdaptiveLoss,)

同样,我希望做的是在训练期间跟踪变量 'alpha'。

以下示例将 alpha 显示为指标。在 colab 中测试。

%%
!git clone https://github.com/google-research/google-research.git

%%
import sys
sys.path.append('google-research')
from robust_loss.adaptive import lossfun

# the robust_loss impl depends on the current workdir to load a data file.
import os
os.chdir('google-research')

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K

class RobustAdaptativeLoss(object):
  def __init__(self):
    z = np.array([[0]])
    self.v_alpha = K.variable(z)

  def loss(self, y_true, y_pred, **kwargs):
    x = y_true - y_pred
    x = K.reshape(x, shape=(-1, 1))
    with tf.variable_scope("lossfun", reuse=True):
      loss, alpha, scale = lossfun(x)
    op = K.update(self.v_alpha, alpha)
    # The alpha update must be part of the graph but it should
    # not influence the result.
    return loss + 0 * op

  def alpha(self, y_true, y_pred):
    return self.v_alpha

def make_model():
  inp = Input(shape=(3,))
  out = Dense(1, use_bias=False)(inp)
  model = Model(inp, out)
  loss = RobustAdaptativeLoss()
  model.compile('adam', loss.loss, metrics=[loss.alpha])
  return model

model = make_model()
model.summary()

init_op = tf.global_variables_initializer()
K.get_session().run(init_op)

import numpy as np

FACTORS = np.array([0.5, 2.0, 5.0])
def target_fn(x):
  return np.dot(x, FACTORS.T)

N_SAMPLES=100
X = np.random.rand(N_SAMPLES, 3)
Y = np.apply_along_axis(target_fn, 1, X)

history = model.fit(X, Y, epochs=2, verbose=True)
print('final loss:', history.history['loss'][-1])