【问题标题】:Keras custom Loss function returning noneKeras 自定义损失函数返回无
【发布时间】:2018-04-27 11:13:36
【问题描述】:

我正在尝试编写变分自动编码器的实现,但是在损失函数方面我遇到了一些困难:

 def vae_loss(sigma, mu):
        def loss(y_true, y_pred):
            recon = K.sum(K.binary_crossentropy(y_true, y_pred), axis=-1)
            kl = 0.5 * K.sum(K.exp(sigma) + K.square(mu) - 1. - sigma, axis=-1)
            return recon + kl
        return loss

二元交叉熵部分工作正常,但每当我只返回散度项 kl 进行测试时,我都会收到以下错误: ValueError:“试图将'x'转换为张量并失败。错误:不支持任何值。”。

我期待有关我做错了什么的可能提示。你会在下面找到我的整个代码。感谢您的宝贵时间!

import numpy as np
from keras import Model
from keras.layers import Input, Dense, Lambda
import keras.backend as K
from keras.datasets import mnist
from matplotlib import pyplot as plt

class VAE(object):

    def __init__(self, n_latent, batch_size):

        self.encoder, self.encoder_input, self.mu, self.sigma = self.create_encoder(n_latent, batch_size)
        self.decoder, self.decoder_input, self.decoder_output = self.create_decoder(n_latent, batch_size)
        pipeline = self.decoder(self.encoder.outputs[0])

        def vae_loss(sigma, mu):
            def loss(y_true, y_pred):
                recon = K.sum(K.binary_crossentropy(y_true, y_pred), axis=-1)
                kl = 0.5 * K.sum(K.exp(sigma) + K.square(mu) - 1. - sigma, axis=-1)
                return recon + kl
            return loss

        self.VAE = Model(self.encoder_input, pipeline)
        self.VAE.compile(optimizer="adadelta", loss=vae_loss(self.sigma, self.mu))

    def create_encoder(self, n_latent, batch_size):

        input_layer = Input(shape=(784,))
        #net = Dense(512, activation="relu")(input_layer)
        mu = Dense(n_latent, activation="linear")(input_layer)
        print(mu)
        sigma = Dense(n_latent, activation="linear")(input_layer)

        def sample_z(args):
            mu, log_sigma = args
            eps = K.random_normal(shape=(K.shape(input_layer)[0], n_latent), mean=0., stddev=1.)
            K.print_tensor(K.shape(eps))
            return mu + K.exp(log_sigma / 2) * eps

        sample_z = Lambda(sample_z)([mu, sigma])

        model = Model(inputs=input_layer, outputs=[sample_z, mu, sigma])
        return model, input_layer,  mu, sigma

    def create_decoder(self, n_latent, batch_size):

        input_layer = Input(shape=(n_latent,))
        #net = Dense(512, activation="relu")(input_layer)
        reconstruct = Dense(784, activation="linear")(input_layer)

        model = Model(inputs=input_layer, outputs=reconstruct)
        return model, input_layer, reconstruct

【问题讨论】:

    标签: python machine-learning keras autoencoder


    【解决方案1】:

    我假设在反向传播期间“测试”/调试训练阶段时会出现错误(如果我错了,请告诉我)。

    如果是这样,问题在于您要求 Keras 优化您的整个网络 (model.VAE.fit(...)),同时使用仅覆盖编码器部分的损失 (kl)。解码器的梯度保持未定义(没有像 recon 这样的损失覆盖它),导致优化错误。

    为了您的调试目的,如果您尝试编译并仅安装具有此截断损失 (kl) 的编码器,或者如果您想出一个覆盖解码器的虚拟(可微分)损失(例如K.sum(y_pred - y_pred, axis=-1) + kl)。

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 2017-12-18
      • 2020-03-27
      • 1970-01-01
      • 2018-10-28
      • 2017-12-29
      • 1970-01-01
      • 2020-02-27
      • 2018-12-26
      相关资源
      最近更新 更多