如何使用张量概率正确地使用密集的层
我是一个新手,具有张力和张量的概率。 我正在使用此网络进行回归任务。
def normal_sp(params):
return tfd.Normal(loc=params[:,0:1], scale=1e-3 + tf.math.softplus(0.05 * params[:,1:2]))
kernel_divergence_fn=lambda q, p, _: tfp.distributions.kl_divergence(q, p) / (x.shape[0] * 1.0)
bias_divergence_fn=lambda q, p, _: tfp.distributions.kl_divergence(q, p) / (x.shape[0] * 1.0)
inputs = Input(shape=(1,),name="input layer")
hidden = tfp.layers.DenseFlipout(50,bias_posterior_fn=tfp.layers.util.default_mean_field_normal_fn(),
bias_prior_fn=tfp.layers.default_multivariate_normal_fn,
kernel_divergence_fn=kernel_divergence_fn,
bias_divergence_fn=bias_divergence_fn,activation="relu",name="DenseFlipout_layer_1")(inputs)
hidden = tfp.layers.DenseFlipout(100,bias_posterior_fn=tfp.layers.util.default_mean_field_normal_fn(),
bias_prior_fn=tfp.layers.default_multivariate_normal_fn,
kernel_divergence_fn=kernel_divergence_fn,
bias_divergence_fn=bias_divergence_fn,activation="relu",name="DenseFlipout_layer_2")(hidden)
hidden = tfp.layers.DenseFlipout(100,bias_posterior_fn=tfp.layers.util.default_mean_field_normal_fn(),
bias_prior_fn=tfp.layers.default_multivariate_normal_fn,
kernel_divergence_fn=kernel_divergence_fn,
bias_divergence_fn=bias_divergence_fn,activation="relu",name="DenseFlipout_layer_3")(hidden)
params = tfp.layers.DenseFlipout(2,bias_posterior_fn=tfp.layers.util.default_mean_field_normal_fn(),
bias_prior_fn=tfp.layers.default_multivariate_normal_fn,
kernel_divergence_fn=kernel_divergence_fn,
bias_divergence_fn=bias_divergence_fn,name="DenseFlipout_layer_4")(hidden)
dist = tfp.layers.DistributionLambda(normal_sp)(params)
model_vi = Model(inputs=inputs, outputs=dist)
model_vi.compile(Adam(learning_rate=0.002), loss=NLL)
model_params = Model(inputs=inputs, outputs=params)
我的问题与损失功能有关:
kl = sum(model.losses)
loss = neg_log_likelihood + kl
但是在此示例中nofollow noreferrer“> https://colab.research.google.com/github/tensorchiefs/dl_book/blob/master/master/chapter/chapter_08/nb_ch08_03.ipynb
损失功能就是NLL。我的问题是:我必须手动添加kl差异,还是张力流自动计算?在第一种情况下,我该如何做,因为模型似乎不起作用?感谢任何帮助的人
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在使用KERAS进行训练,则总损失中包括每层损失(KLS)(我确定这是正确的,您可以通过覆盖KL_Divergence_FN来检查以返回一些荒谬的价值并查看您的总体损失变得荒谬)。
在文档的示例中(这有点古老),Keras没有进行培训。取而代之的是,将优化器应用于手动书面损失,因此必须抓住所有每层损失并将其添加。
If you're using Keras to train, the per-layer losses (KLs) are included in the overall loss (I am 90% sure this is right -- you could check by overriding the kl_divergence_fn to return some absurd value and see if your overall loss becomes absurd).
In the example from the docs (which are, ahem, a bit ancient), keras is not doing the training; instead an optimizer is being applied to a manually written loss, and so one has to grab all the per layer losses and add them in.