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

时间:2019-06-25 16:04:12

标签: keras callback loss

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

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

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

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

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'。

1 个答案:

答案 0 :(得分:1)

以下示例将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])