Keras / Tensorflow:组合损耗功能可实现单输出

时间:2018-08-06 10:23:30

标签: python tensorflow keras loss-function

我的模型只有一个输出,但是我想结合两个不同的损失函数:

def get_model():
    # create the model here
    model = Model(inputs=image, outputs=output)

    alpha = 0.2
    model.compile(loss=[mse, gse],
                      loss_weights=[1-alpha, alpha]
                      , ...)

但是它抱怨我需要两个输出,因为我定义了两个损失:

ValueError: When passing a list as loss, it should have one entry per model outputs. 
The model has 1 outputs, but you passed loss=[<function mse at 0x0000024D7E1FB378>, <function gse at 0x0000024D7E1FB510>]

我是否可以编写最终损失函数而不必创建另一个损失函数(因为这会限制我在损失函数之外更改Alpha)?

我该如何做类似(1-alpha)*mse + alpha*gse的事情?


更新:

我的两个损失函数都等效于任何内置keras损失函数的函数签名,接受y_truey_pred并给出张量以补偿损失(可以使用{将其简化为标量{1}}),但我相信,只要这些损失函数返回有效损失,它们的定义方式就不会影响答案。

K.mean()

4 个答案:

答案 0 :(得分:3)

为损失指定自定义函数:

model = Model(inputs=image, outputs=output)

alpha = 0.2
model.compile(
    loss=lambda y_true, y_pred: (1 - alpha) * mse(y_true, y_pred) + alpha * gse(y_true, y_pred),
    ...)

或者如果您不想让丑陋的lambda成为实际功能:

def my_loss(y_true, y_pred):
    return (1 - alpha) * mse(y_true, y_pred) + alpha * gse(y_true, y_pred)

model = Model(inputs=image, outputs=output)

alpha = 0.2
model.compile(loss=my_loss, ...)

编辑:

如果您的alpha不是某个全局常量,则可以有一个“损失函数工厂”:

def make_my_loss(alpha):
    def my_loss(y_true, y_pred):
        return (1 - alpha) * mse(y_true, y_pred) + alpha * gse(y_true, y_pred)
    return my_loss

model = Model(inputs=image, outputs=output)

alpha = 0.2
my_loss = make_my_loss(alpha)
model.compile(loss=my_loss, ...)

答案 1 :(得分:0)

List<String> list = new ArrayList<>() 函数应该是一个函数。您要为模型提供两个函数的列表

尝试:

loss

答案 2 :(得分:0)

是的,定义您自己的自定义损失函数,并在编译时将其传递给loss参数:

def custom_loss(y_true, y_pred):
    return (1-alpha) * K.mean(K.square(y_true-y_pred)) + alpha * gse

(不确定gse的意思)。看看Keras如何实现香草损失是有帮助的:https://github.com/keras-team/keras/blob/master/keras/losses.py

答案 3 :(得分:0)

不是这个答案特别解决了最初的问题,我想写它是因为尝试使用keras.models.load_model加载具有自定义损失的keras模型时会发生相同的错误,并且在任何地方都没有正确回答。具体来说,遵循keras github repository中的VAE示例代码,当使用model.save保存后加载VAE模型时,会发生此错误。

解决方案是使用vae.save_weights('file.h5')仅保存权重,而不是保存完整模型。但是,在使用vae.load_weights('file.h5')加载权重之前,您将不得不再次构建和编译模型。

以下是示例实现。

class VAE():
    def build_model(self): # latent_dim and intermediate_dim can be passed as arguments
        def sampling(args):
            """Reparameterization trick by sampling from an isotropic unit Gaussian.
            # Arguments
                args (tensor): mean and log of variance of Q(z|X)
            # Returns
                z (tensor): sampled latent vector
            """

            z_mean, z_log_var = args
            batch = K.shape(z_mean)[0]
            dim = K.int_shape(z_mean)[1]
            # by default, random_normal has mean = 0 and std = 1.0
            epsilon = K.random_normal(shape=(batch, dim))
            return z_mean + K.exp(0.5 * z_log_var) * epsilon

        # original_dim = self.no_features
        # intermediate_dim = 256
        latent_dim = 8
        inputs = Input(shape=(self.no_features,))
        x = Dense(256, activation='relu')(inputs)
        x = Dense(128, activation='relu')(x)
        x = Dense(64, activation='relu')(x)
        z_mean = Dense(latent_dim, name='z_mean')(x)
        z_log_var = Dense(latent_dim, name='z_log_var')(x)
        # use reparameterization trick to push the sampling out as input
        # note that "output_shape" isn't necessary with the TensorFlow backend
        z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])
        # instantiate encoder model
        encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')


        # build decoder model
        latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
        x = Dense(32, activation='relu')(latent_inputs)
        x = Dense(48, activation='relu')(x)
        x = Dense(64, activation='relu')(x)
        outputs = Dense(self.no_features, activation='linear')(x)

        # instantiate decoder model
        decoder = Model(latent_inputs, outputs, name='decoder')

        # instantiate VAE model
        outputs = decoder(encoder(inputs)[2])
        VAE = Model(inputs, outputs, name='vae_mlp')
        reconstruction_loss = mse(inputs, outputs)
        reconstruction_loss *= self.no_features
        kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
        kl_loss = K.sum(kl_loss, axis=-1)
        kl_loss *= -0.5
        vae_loss = K.mean(reconstruction_loss + kl_loss)
        VAE.add_loss(vae_loss)
        VAE.compile(optimizer='adam')
        return VAE

现在

vae_cls = VAE()
vae = vae_cls.build_model()
# vae.fit()
vae.save_weights('file.h5')

加载模型并进行预测(如果使用其他脚本,则需要导入VAE类),

vae_cls = VAE()
vae = vae_cls.build_model()
vae.load_weights('file.h5')
# vae.predict()

最后,差异:[ref]

Keras model.save保存,

  1. 模型权重
  2. 模型架构
  3. 模型编译详细信息(损失函数和指标)
  4. 模型优化器和正则器状态

Keras model.save_weights仅保存模型权重。 Keras model.to_json()保存了模型架构。

希望这可以帮助有人尝试使用变体自动编码器。