自定义丢失功能,无需使用keras后端库 [英] Custom loss function without using keras backend library

查看:65
本文介绍了自定义丢失功能,无需使用keras后端库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ML模型应用于实验设置,以优化驱动信号.驱动信号本身就是被优化的东西,但是其质量是间接评估的(将其应用于实验装置以产生不同的信号).

I am applying an ML model to an experimental setup to optimise a driving signal. The driving signal itself is the thing being optimised, but its quality is evaluated indirectly (it is applied to an experimental setup to produce a different signal).

我能够通过python中的函数运行并从实验中收集数据.

I am able to run and collect data from the experiment via functions in python.

我想用一个自定义损失函数建立一个ML模型,该函数以优化的信号调用实验驱动程序函数,以获取用于反向传播的误差.

I would like to set up an ML model with a custom loss function that invokes the experiment driver functions with the optimised signal to get the error used for back-prop.

我已经研究过使用keras,但是必须专门使用keras后端函数的限制意味着我无法在该函数中调用驱动程序函数.

I have looked into using keras however the restriction of having to use the keras backend functions exclusively means that I cannot call my driver functions in the function.

如果我要使用没有keras前端的张量流,以及是否有其他ML API允许这样做,我想知道是否有一种方法可以做我想做的事情?

I would like to know if there is a way to do what I want if I were to use tensor-flow without keras front-end, and also if a different ML API allows this?

谢谢.

推荐答案

如果我理解这个问题,您希望能够根据模型评估损失函数时运行的代码来产生损失.

If I understood the question you want to be able to generate the loss based on code that you run when the model evaluates the loss function.

这将是一个示例:

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

FACTORS = np.array([[0.5, 2.0, 4.0]])

def ext_function(inputs):
  """ This can be an arbitrary python function of the inputs
  inputs is a tf.EagerTensor which can be converted into a numpy array.
  """
  r = np.dot(inputs, FACTORS.T)
  return r

class LossFunction(object):
  def __init__(self, model):
    # Use model to obtain the inputs
    self.model = model

  def __call__(self, y_true, y_pred, sample_weight=None):
    """ ignore y_true value from fit params and compute it instead using
    ext_function
    """
    y_true = tf.py_function(ext_function, [self.model.inputs[0]], Tout=tf.float32)
    v = keras.losses.mean_squared_error(y_true, y_pred)
    return K.mean(v)

def make_model():
  inp = Input(shape=(3,))
  out = Dense(1, use_bias=False)(inp)
  model = Model(inp, out)
  model.compile('adam', LossFunction(model))
  return model

model = make_model()
model.summary()

测试:

import numpy as np


N_SAMPLES=100
X = np.random.rand(N_SAMPLES, 3)
Y_dummy = np.random.rand(N_SAMPLES)

history = model.fit(X, Y_dummy, epochs=1000, verbose=False)
print(history.history['loss'][-1])

它实际上做了一些事情:

And it actually does something:

model.layers[1].get_weights()

请注意,简单地生成正确的Y值作为输入将更加简单.我不确切知道您遇到问题的情况.但是,如果可能的话,请尝试预生成Y.而不要使用上面的示例.

Please note that it will be much simpler to simply generate the correct value of Y as input. I don't know exactly the conditions of your problem. But if at all possible try to pre-generate Y. Rather than use the example above.

我已经使用上面的技巧创建了由类加权的自定义指标.即,在其中输入参数之一是类别并且所需损失函数是每类损失的加权平均值的情况下.

I've used the trick above to create custom metrics which are weighted by the class. i.e. in scenarios where one of the input params is a class and the desired loss function is an weighted per class average of the losses.

这篇关于自定义丢失功能,无需使用keras后端库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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