tensorflow / tensorflow/probability

wrong loss function in examples for VI reparameterization layers

Open
#1,335 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

In example for https://www.tensorflow.org/probability/api_docs/python/tfp/layers/DenseReparameterization, it states

import tensorflow as tf
import tensorflow_probability as tfp

model = tf.keras.Sequential([
    tfp.layers.DenseReparameterization(512, activation=tf.nn.relu),
    tfp.layers.DenseReparameterization(10),
])

logits = model(features)
neg_log_likelihood = tf.nn.softmax_cross_entropy_with_logits(
    labels=labels, logits=logits)
kl = sum(model.losses)
loss = neg_log_likelihood + kl
train_op = tf.train.AdamOptimizer().minimize(loss)

However, the KL loss is already incorporated in the call functions of any child that inherits _DenseVariational

https://github.com/tensorflow/probability/blob/4683836be3701e75f459494bd3de12edb7560a2d/tensorflow_probability/python/layers/dense_variational.py#L308

Thus the examples of any layer that inherits _DenseVariational and also DenseVariational should exclude kl in the loss functions. ie.

import tensorflow as tf
import tensorflow_probability as tfp

model = tf.keras.Sequential([
    tfp.layers.DenseReparameterization(512, activation=tf.nn.relu),
    tfp.layers.DenseReparameterization(10),
])

logits = model(features)
neg_log_likelihood = tf.nn.softmax_cross_entropy_with_logits(
    labels=labels, logits=logits)
loss = neg_log_likelihood
train_op = tf.train.AdamOptimizer().minimize(loss)

Please feel free to correct me if I am wrong.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DenseReparameterization API example at the linked TensorFlow Probability documentation and inspect the referenced dense_variational.py location. Verify whether the layer already adds KL terms to model.losses, then check the affected reparameterization-layer examples and confirm the documented loss matches that behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation, machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.