逐步在训练期间更新Keras中的自定义损失权重

时间:2018-08-02 22:09:42

标签: python tensorflow keras loss-function keras-2

我在Keras(张量流后端)中定义了一个自定义损失函数,该函数由重构MSE以及获知概率分布和标准正态分布之间的kullback leibler散度组成。 (适用于变体自动编码器。)

我希望能够缓慢增加训练过程中KL散度项对成本的影响,权重为“ reg”,从reg = 0.0开始,直到达到1.0。我希望将增加的速率调整为超参数。(到目前为止,我只是将“ reg”参数设置为常数0.5)。

Keras是否有执行此操作的功能?

def vae_loss(y_true,y_pred):
    reg = 0.5
    # Average cosine distance for all words in a sequence 
    reconstruction_loss = tf.reduce_mean(mean_squared_error(y_true, y_pred),1)    
    # Second part of the loss ensures the z probability distribution doesn't stray too far from normal
    KL_divergence_loss = tf.reduce_mean(tf.log(z_sigma) + tf.div((1 + tf.square(z_mu)),2*tf.square(z_sigma)) - 0.5,1)
    loss = reconstruction_loss + tf.multiply(reg,KL_divergence_loss)
    return loss

0 个答案:

没有答案
相关问题