在 Keras/Tensorflow 自定义损失函数中使用额外的“可训练”变量

Gio*_*oni 6 python keras tensorflow loss-function

我知道如何在 Keras 中使用附加输入(而不是标准y_true,y_pred对)编写自定义损失函数,请参见下文。我的问题是使用可训练变量(其中一些)输入损失函数,该变量是损失梯度的一部分,因此应该更新。

我的解决方法是:

  • 在网络中输入NXV大小的虚拟输入,其中N是观测值的数量和V附加变量的数量
  • 添加一个Dense()dummy_output,以便 Keras 跟踪我的V“体重”
  • V在我的自定义损失函数中使用该层的权重作为我的真实输出层
  • dummy_output对于该层使用虚拟损失函数(仅返回 0.0 和/或权重 0.0),因此我的V“权重”仅通过我的自定义损失函数进行更新

我的问题是:是否有更自然的类似 Keras/TF 的方法来做到这一点?因为它感觉很做作,更不用说容易出现错误了。

我的解决方法的示例:

(是的,我知道这是一个非常愚蠢的自定义损失函数,实际上事情要复杂得多)

import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Input
from tensorflow.keras import Model

n_col = 10
n_row = 1000
X = np.random.normal(size=(n_row, n_col))
beta = np.arange(10)
y = X @ beta

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# my custom loss function accepting my dummy layer with 2 variables
def custom_loss_builder(dummy_layer):
    def custom_loss(y_true, y_pred):
        var1 = dummy_layer.trainable_weights[0][0]
        var2 = dummy_layer.trainable_weights[0][1]
        return var1 * K.mean(K.square(y_true-y_pred)) + var2 ** 2 # so var2 should get to zero, var1 should get to minus infinity?
    return custom_loss

# my dummy loss function
def dummy_loss(y_true, y_pred):
    return 0.0

# my dummy input, N X V, where V is 2 for 2 vars
dummy_x_train = np.random.normal(size=(X_train.shape[0], 2)) 

# model
inputs = Input(shape=(X_train.shape[1],))
dummy_input = Input(shape=(dummy_x_train.shape[1],))
hidden1 = Dense(10)(inputs) # here only 1 hidden layer in the "real" network, assume whatever network is built here
output = Dense(1)(hidden1)
dummy_output = Dense(1, use_bias=False)(dummy_input)
model = Model(inputs=[inputs, dummy_input], outputs=[output, dummy_output])

# compilation, notice zero loss for the dummy_output layer
model.compile(
  loss=[custom_loss_builder(model.layers[-1]), dummy_loss],
  loss_weights=[1.0, 0.0], optimizer= 'adam')

# run, notice y_train repeating for dummy_output layer, it will not be used, could have created dummy_y_train as well
history = model.fit([X_train, dummy_x_train], [y_train, y_train],
                    batch_size=32, epochs=100, validation_split=0.1, verbose=0,
                   callbacks=[EarlyStopping(monitor='val_loss', patience=5)])
Run Code Online (Sandbox Code Playgroud)

似乎确实可以像它们分别渴望的var1var2(层的初始化)的起始值一样工作:dummy_outputinf0

(该图来自迭代运行模型并保存这两个权重,如下所示)

var1_list = []
var2_list = []
for i in range(100):
    if i % 10 == 0:
        print('step %d' % i)
    model.fit([X_train, dummy_x_train], [y_train, y_train],
              batch_size=32, epochs=1, validation_split=0.1, verbose=0)
    var1, var2 = model.layers[-1].get_weights()[0]
    var1_list.append(var1.item())
    var2_list.append(var2.item())

plt.plot(var1_list, label='var1')
plt.plot(var2_list, 'r', label='var2')
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Gio*_*oni 5

在这里回答我自己的问题,经过几天的努力,我让它在没有虚拟输入的情况下工作,我认为这要好得多,并且应该是“规范”方式,直到 Keras/TF 简化过程。Keras/TF 文档就是这样做

将损失函数与外部可训练变量结合使用的关键是通过使用自定义损失/输出,该层的self.add_loss(...)实现call()如下所示:

class MyLoss(Layer):
    def __init__(self, var1, var2):
        super(MyLoss, self).__init__()
        self.var1 = K.variable(var1) # or tf.Variable(var1) etc.
        self.var2 = K.variable(var2)
    
    def get_vars(self):
        return self.var1, self.var2
    
    def custom_loss(self, y_true, y_pred):
        return self.var1 * K.mean(K.square(y_true-y_pred)) + self.var2 ** 2
    
    def call(self, y_true, y_pred):
        self.add_loss(self.custom_loss(y_true, y_pred))
        return y_pred
Run Code Online (Sandbox Code Playgroud)

现在请注意,该MyLoss层需要两个输入,即实际输入y_true和预测输入y

inputs = Input(shape=(X_train.shape[1],))
y_input = Input(shape=(1,))
hidden1 = Dense(10)(inputs)
output = Dense(1)(hidden1)
my_loss = MyLoss(0.5, 0.5)(y_input, output) # here can also initialize those var1, var2
model = Model(inputs=[inputs, y_input], outputs=my_loss)

model.compile(optimizer= 'adam')
Run Code Online (Sandbox Code Playgroud)

最后,正如 TF 文档提到的,在这种情况下,您不必在函数中指定lossor :yfit()

history = model.fit([X_train, y_train], None,
                    batch_size=32, epochs=100, validation_split=0.1, verbose=0,
                    callbacks=[EarlyStopping(monitor='val_loss', patience=5)])
Run Code Online (Sandbox Code Playgroud)

再次注意,它是作为输入之一y_train出现的。fit()

现在它可以工作了:

var1_list = []
var2_list = []
for i in range(100):
    if i % 10 == 0:
        print('step %d' % i)
    model.fit([X_train, y_train], None,
              batch_size=32, epochs=1, validation_split=0.1, verbose=0)
    var1, var2 = model.layers[-1].get_vars()
    var1_list.append(var1.numpy())
    var2_list.append(var2.numpy())

plt.plot(var1_list, label='var1')
plt.plot(var2_list, 'r', label='var2')
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(我还应该提到 的这种特定模式var1var2很大程度上取决于它们的初始值,如果var1的初始值高于 1 它实际上不会减少,直到减号inf