DNN 不同部分的不同损失函数

Different Loss functions on different segments of a DNN

是否可以在深度网络的不同“段”上定义不同的损失函数(在以下意义上):

假设我有一些输入输出对 (x_n,y_n) 和 (x_n,z_n) 我想训练一个深度网络来自 f_k∘...∘f_1(每个 f_i 是一个前馈层)使得


类比/例子/直觉/动机:

假设我希望我的网络输出 大约像 x^2 那么 f_k∘f_k-1(x_n )~~ x_n^2 和 z_n:= x_n^2。那么f_k∘...∘f_1(x_n)一个前馈网络,其输出层近似为函数x^2.


如何在 TensorFlow/Keras 中做到这一点?

您可以通过定义两个输出模型来实现。 它基本上最小化了两个损失的加权平均值。

一个限制是第一层和第 (n-1) 层的输入形状必须相同,因为两者接收相同的输入 x

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Input

input_shape = (1,)
model1 = Sequential([
    Dense(5, activation="relu", input_shape=input_shape),
    Dense(8, activation="relu"),
    Dense(input_shape[0])
])

model2 = Sequential([
    Dense(15, activation="relu", input_shape=input_shape),
    Dense(1)
])

x = Input(input_shape)
y = model2(model1(x))
z = model2(x)
model = Model(inputs=x, outputs=[y, z])
model.compile("adam", "mean_squared_error", loss_weight=[0.5, 0.5])

import numpy as np
n = 1000
x = np.random.normal(size=n * input_shape[0]).reshape((n, input_shape[0]))
y = x**2
z = x**2

history = model.fit(x, [y, z], epochs=100)

可视化:

import matplotlib.pyplot as plt
plt.plot(history.history["loss"])

yhat, zhat = model.predict(x)
plt.scatter(x, yhat)
plt.scatter(x, zhat)