【问题标题】:VAE loss Function using Keras使用 Keras 的 VAE 损失函数
【发布时间】:2020-05-05 08:32:25
【问题描述】:

我的任务是使用 Keras 库中的方法实现所提供公式的损失函数。 公式为:IMAGE

我需要在这里提供实现:

def vae_loss_function(x, x_pred, mu, sigma, kl_weight=0.0005):
  latent_loss = ...
  reconstruction_loss = ...
  vae_loss = ...
  return vae_loss

我试图找出我应该使用哪种方法,但我找不到类似的例子。

【问题讨论】:

    标签: python tensorflow keras jupyter


    【解决方案1】:

    您可以使用 keras 后端来实现这些功能。

    这是我用来编码vae_loss的实现

    参考:https://keras.io/examples/variational_autoencoder/

    from tensorflow.keras.losses import mse
    import tensorflow.keras.backend as K
    def vae_loss_function(x, x_pred, mu, sigma, kl_weight=0.0005):
      latent_loss =  0.5*(sigma + K.square(mu) - 1 - K.exp(sigma))
      reconstruction_loss = mse(x, x_pred)
      vae_loss = kl_weights*latent_loss + reconstruction_loss
      return vae_loss
    

    【讨论】:

    • 非常感谢,如果我们谈论 python 和所有这些机器学习人员,我完全是菜鸟,但我必须完成它:P
    • 你还给我解释一件事吗?为什么不在我们的代码中包含这个求和运算符?
    • 因为我们使用带有张量的向量化形式的代码,所以我们不需要循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2018-10-21
    • 2019-04-11
    • 1970-01-01
    • 2018-04-03
    • 2019-01-20
    相关资源
    最近更新 更多