tensorflow / tensorflow/probability

How to read evaluate output of a variational model

Open
#512 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

Hello,

I have been developing an VAE following some tutorials ( here , here, etc).

I am specially interested in studying the reconstruction error on out-of-distribution sets. Namely, I am implementing the loss function

negloglik = lambda x, rv_x: -rv_x.log_prob(x)

which is the reconstruction loss.

Furthermore, the encoding layer has a KL Divergence Regulator

tfpl.MultivariateNormalTriL(
        encoded_size,
        activity_regularizer=tfpl.KLDivergenceRegularizer(prior))

such that the wholve Loss is the ELBO (Evidence Lower Bound).

Now it comes my question, what is the best way of computing the reconstruction loss of a new sample? I have tried two approaches.

Approach 1: Sample from VAE model and pass it through reconstruction loss.

Consider we have X_test. I define the reconstruction preditor as var(X_test) such that my reconstruction losses are

losses_1 = - vae(X_test).log_prob(X_test).numpy().mean(1)

Approach 2: Use the evaluate method of VAE model.

Straightforwardly:

losses_2 = np.array([vae.evaluate(x.reshape(1,-1), x.reshape(1,-1), verbose=0) for x in X])

Findings

  • Approach 2 is a lot slower to compute. While losses_1 takes around tens of milliseconds, approach losses_2 requires around 4 minutes for the same set. This is three orders of magnitude difference.
  • losses_2 are 5 to 10 times higher than losses_1 for out-of-distribution sets, but overall similar for an in-distribution test set.

So, some questions:

  • Is evaluate method also including the KL div loss?
  • Why is evaluate so much slower?
  • For out-of-distribution detection, should I use the evaluate method or just the reconstruction loss?
  • Am I using the right methodology in approach 1? Or should I sample a reconstruction multiple times from the same sample?

Thanks for the help. Although I am quite versed in TF, Keras, DL, etc I am now learning about the power of Bayesian inference and it's applications.

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 issue's two entry points: vae(X_test).log_prob(X_test) and vae.evaluate(...), then inspect how KLDivergenceRegularizer(prior) contributes to the ELBO. Reproduce the timing and loss differences on in-distribution and out-of-distribution inputs; done means the issue documents whether KL is included, why the paths differ, and which reconstruction/OOD procedure is recommended.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.