为可变自动编码器keras添加自定义损失功能

时间:2018-11-18 01:31:22

标签: python keras autoencoder loss-function

我正在尝试为可变自动编码器添加自定义损失函数。除了重构损失,KL散度我希望基于输入和输出对的汉明距离之间的差异来增加损失。

但是我面临的问题是,无论有没有这种额外的损失,结果都是一样的。谁能指出我应该做些什么来纠正它?与尺寸有关吗?

这是我的代码段:

def ham_loss(y_true,y_pred):
    # calculate pairwise hamming distance matrix
    # differences of y_pred probabilities)
    pairwise_diff_pred = K.expand_dims(y_pred, 0) - K.expand_dims(y_pred, 1)
    pairwise_distance_pred = K.sum(pairwise_diff_pred, axis=-1)

    # calculate pairwise hamming distance matrix for inputs
    pairwise_diff_true = K.expand_dims(y_true, 0) - K.expand_dims(y_true, 1)
    pairwise_distance_true = K.sum(pairwise_diff_true, axis=-1)

    #Difference between the distances of y_true and y_predictions
    hamm_sum= Lambda(differences)([pairwise_distance_true, pairwise_distance_pred])
    print(hamm_sum)
    return K.sum(hamm_sum, axis=-1)

def vae_loss(y_true, y_pred):
    """ Calculate loss = reconstruction loss + KL loss for each data in minibatch """
         # E[log P(X|z)]

         recon = K.sum(K.binary_crossentropy(y_true,y_pred),axis=1)
         # D_KL(Q(z|X) || P(z|X)); calculate in closed form as both dist. are Gaussian
         kl = 0.5 * K.sum(K.exp(z_log_var) + K.square(z_mean) - 1. - z_log_var, axis=1)

         hamming_loss = ham_loss(y_true,y_pred)

         return recon + kl + hamming_loss

任何帮助,不胜感激!

先谢谢了。

0 个答案:

没有答案