tensorflow / tensorflow/probability

What does the method get_weights return in the case of Bayesian layers?

Open
#708 3 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

If you execute the following code

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp


input = tf.keras.layers.Input(shape=(1,))
layer = tfp.layers.DenseReparameterization(2)

x = layer(input)

model = tf.keras.models.Model(inputs=input, outputs=x)

inp = np.array([[1]])
print(model.predict(inp))

print("weights =", layer.get_weights())

print("layer.kernel_posterior.mean() =", layer.kernel_posterior.mean())
print("layer.kernel_posterior.stddev() =", layer.kernel_posterior.stddev())

print("layer.kernel_prior.mean() =", layer.kernel_prior.mean())
print("layer.kernel_prior.stddev() =", layer.kernel_prior.stddev())

print("layer.bias_posterior.mean() =", layer.bias_posterior.mean())
print("layer.bias_posterior.stddev() =", layer.bias_posterior.stddev())

You will get something like

[[0.07018379 0.13365692]]

weights = [
array([[0.01465803, 0.04191336]], dtype=float32), 
array([[-2.9708524, -2.859167 ]], dtype=float32), 
array([0.11541488, 0.1064656 ], dtype=float32)
]

layer.kernel_posterior.mean() = tf.Tensor([[0.01465803 0.04191336]], shape=(1, 2), dtype=float32)
layer.kernel_posterior.stddev() = tf.Tensor([[0.04998922 0.05573414]], shape=(1, 2), dtype=float32)

layer.kernel_prior.mean() = tf.Tensor([[0. 0.]], shape=(1, 2), dtype=float32)
layer.kernel_prior.stddev() = tf.Tensor([[1. 1.]], shape=(1, 2), dtype=float32)

layer.bias_posterior.mean() = tf.Tensor([0.11541488 0.1064656 ], shape=(2,), dtype=float32)
layer.bias_posterior.stddev() = tf.Tensor([0. 0.], shape=(2,), dtype=float32)

So, layer.get_weights(), where layer = tfp.layers.DenseReparameterization(2), returns a list of 3 arrays. The first array corresponds to the means of kernel_posterior and the last array corresponds to the means of bias_posterior. What does the second array array([[-2.9708524, -2.859167 ]], dtype=float32) correspond to?

Furthermore, why aren't the standard deviations also weights?

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

Run the minimal reproduction in the issue and inspect DenseReparameterization, get_weights, kernel_posterior, and bias_posterior behavior. Done when the documentation or issue response clearly identifies the second returned array and explains why posterior standard deviations are not returned as separate weights.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.