如何在Keras中实现自适应损失? [英] How to implement an adaptive loss in Keras?

查看:71
本文介绍了如何在Keras中实现自适应损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras来实现> 常规和自适应鲁棒损失函数 .作者提供了处理详细细节的tensorflow代码.我只是想在Keras中使用他的预建函数.

I am trying to use Keras to implement the work done in A General and Adaptive Robust Loss Function. The author provides tensorflow code that works the hard details. I am just trying to use his prebuilt function in Keras.

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

His custom loss function is learning a parameter 'alpha' that controls the shape of the loss function. I would like to track 'alpha' in addition to the loss during training.

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

I am somewhat familiar with Keras custom loss functions and using wrappers, but I am not entirely sure how to use callbacks to track 'alpha'. Below is how I would choose to naively construct the loss function in Keras. However I am not sure how I would then access the 'alpha' to track.

从提供的张量流代码中,函数lossfun(x)返回一个元组.

From the provided tensorflow code, the function lossfun(x) returns a tuple.

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

Again, what I am hoping to do is track the variable 'alpha' during training.

推荐答案

以下示例将alpha显示为度量.在colab中进行了测试.

The following example displays alpha as a metric. Tested in 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])

这篇关于如何在Keras中实现自适应损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆