【问题标题】:Variational Autoencoder cross-entropy loss (xent_loss) with 3D convolutional layers具有 3D 卷积层的变分自动编码器交叉熵损失 (xent_loss)
【发布时间】:2018-06-23 07:58:49
【问题描述】:

我正在调整我在此处找到的 VAE https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py 的实现 https://blog.keras.io/building-autoencoders-in-keras.html

这个实现不使用卷积层,所以可以说一切都是 1D 发生的。我的目标是在这个模型中实现 3D 卷积层。

但是,在运行批次(包含 128 个样本)时,我遇到了损失函数的形状不匹配:

def vae_loss(self, x, x_decoded_mean):
    xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
    #xent_loss.shape >> [128, 40, 20, 40, 1]
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    #kl_loss.shape >> [128]
    return K.mean(xent_loss + kl_loss) # >> error shape mismatch

对于具有一维卷积层的模型,几乎相同的问题已经在Keras - Variational Autoencoder Incompatible shape 得到了回答,但我真的不明白如何推断我的情况的答案 wjich 具有更复杂的输入形状。

我已经尝试过这个解决方案:

xent_loss = original_dim * metrics.binary_crossentropy(K.flatten(x), K.flatten(x_decoded_mean))

但我不知道从数学的角度来看它是否是一个有效的解决方案,尽管现在模型正在运行。

【问题讨论】:

    标签: machine-learning keras autoencoder


    【解决方案1】:

    您的方法是正确的,但它高度依赖于K.binary_crossentropy 的实现。 tensorflowtheano 应该适合你(据我所知)。为了使其更干净且不依赖于实现,我建议您采用以下方式:

    xent_loss_vec = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
    xent_loss = K.mean(xent_loss_vec, axis=[1, 2, 3, 4])
    # xent_loss.shape = (128,)
    

    现在您可以从每个体素的损失中取平均值,感谢 binary_crossentropy 的每个有效实现都应该适合您。

    【讨论】:

    • 当我读到你的答案时这很有意义,但实际上现在它在这一行给了我这个错误:xent_loss = K.mean(xent_loss_vec, axis=[1, 2, 3, 4]) 错误 >>ValueError: Invalid reduction dimension 4 for input with 4 dimensions. for 'custom_variational_layer_7/Mean_1' (op: 'Mean') with input shapes: [?,40,20,40], [4] and with computed input tensors: input[1] = <1 2 3 4> 它确实像这样工作:xent_loss = K.mean(xent_loss_vec, axis=-1)但话又说回来,它在数学上是否正确?
    • 很奇怪。尝试设置axis=[1, 2, 3]
    猜你喜欢
    • 2019-12-01
    • 2020-12-18
    • 2018-03-17
    • 2017-03-14
    • 2019-04-20
    • 2021-07-23
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多