简体   繁体   English

在 tfp 中训练变分贝叶斯神经网络时,如何分别可视化损失中不同项的演变?

[英]When training a variational Bayesian neural network in tfp, how can I visualize the evolution of the different terms in the loss separately?

I want to use tensorflow-probability to train a simple fully-connected Bayesian Neural Network.我想使用 tensorflow-probability 来训练一个简单的全连接贝叶斯神经网络。 The loss is composed of KL terms and a negative log likelihood term.损失由 KL 项和负对数似然项组成。 How can I see their separate evolution with tfp?我怎样才能看到它们与 tfp 的单独演变?

I have the following code:我有以下代码:

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp

tfk = tf.keras
tfkl = tf.keras.layers
tfpl = tfp.layers
tfd = tfp.distributions

[make some data for a regression task]

input = tfkl.Input(n_features)
x = input
x = tfpl.DenseFlipout(100, activation='relu')(x)
x = tfpl.DenseFlipout(2)(x)
x = tfpl.DistributionLambda(lambda t: tfd.Normal(loc=t[..., :1],
                                                 scale=1e-3 + tf.math.softplus(t[..., 1:])))(x)

model = tfk.Model(input, x)

negloglik = lambda y, rv_y: -rv_y.log_prob(y)

model.compile(optimizer=tf.optimizers.Adam(), loss=negloglik, metrics=['mse'])
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val));

The loss function is the sum of the explicit term negloglik and a KL divergence term in each DenseFlipout layer (I can see those are there by looking at model.losses , for example).损失函数是每个DenseFlipout层中显式项negloglik和 KL 散度项的DenseFlipout (例如,我可以通过查看model.losses看到它们)。

How can I visualize each of these terms separately?我如何分别可视化这些术语?


An attempt:一次尝试:

If I try to add a function that calculates negloglik to the metrics, such as如果我尝试添加一个计算negloglik到指标的函数,例如

def negloglik_met(y_true, y_pred):
    return -y_pred.log_prob(y_true)

I get AttributeError: 'Tensor' object has no attribute 'log_prob' which is confusing to me.我得到AttributeError: 'Tensor' object has no attribute 'log_prob'这让我感到困惑。 y_pred should be the output of the DistributionLambda layer, so why is it a Tensor and not a Distribution? y_pred应该是DistributionLambda层的输出,那么为什么它是 Tensor 而不是 Distribution?

Something else I hoped would work but does not is adding model.losses[0] to the metrics.我希望的其他东西会起作用,但不会将model.losses[0]添加到指标中。 There I get ValueError: Could not interpret metric function identifier: Tensor("dense_flipout/divergence_kernel:0", shape=(), dtype=float32) .在那里我得到ValueError: Could not interpret metric function identifier: Tensor("dense_flipout/divergence_kernel:0", shape=(), dtype=float32)

I drilled down in the TensorFlow code.我深入研究了 TensorFlow 代码。 It's due to the automatic TensorFlow creating an automatic wrapper around your (lambda) function.这是由于自动 TensorFlow 为您的 (lambda) 函数创建了一个自动包装器。 It casts and reshapes the model output (the distribution) to the type of the metric (which seems odd to me anyways).它将模型输出(分布)转换并重塑为度量的类型(无论如何这对我来说似乎很奇怪)。 So, to prevent it, you should create your own wrapper, that doesn't perform this cast.因此,为了防止它,您应该创建自己的包装器,它不会执行此转换。 The code that does this, is at: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/metrics.py#L583执行此操作的代码位于: https : //github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/metrics.py#L583

So inspire yourself on that block of code to make your own Metric Wrapper.因此,在该代码块上激发自己的灵感,以制作自己的 Metric Wrapper。 This should be a feature of TFP.这应该是 TFP 的一个特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM