Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemention of Mean Method in distributions.Categorical #1411

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PavanKishore21
Copy link

@PavanKishore21 PavanKishore21 commented Aug 23, 2021

Proposition for correcting issue #685 Error with Implementation of Mean Method in distributions.Categorical

NotImplementedError: mean is not implemented: Categorical

import tensorflow_probability as tfp

prob_dist = tfp.distributions.Categorical(probs=[1.0])
print(prob_dist.mean())

Defined a _mean method for implementing mean

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

1 similar comment
@google-cla
Copy link

google-cla bot commented Aug 23, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no Declares that the user has not signed CLA label Aug 23, 2021
@PavanKishore21
Copy link
Author

@googlebot I signed it!

@google-cla
Copy link

google-cla bot commented Aug 23, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: yes Declares that the user has signed CLA and removed cla: no Declares that the user has not signed CLA labels Aug 23, 2021
@SiegeLordEx
Copy link
Member

This implementation is not correct. The mean of a categorical is defined as sum(i * prob(x=i) for i in range(num_categories). You should be able to modify the implementation of the _entropy to implement this.

This will also need tests to verify the implementation is correct (entropy tests, again, should provide a good set of test cases).

@PavanKishore21
Copy link
Author

PavanKishore21 commented Aug 25, 2021

Hello @SiegeLordEx, Thanks for your response.
I have updated the formula for Mean

tf.reduce_sum(tf.math.multiply_no_nan(i , self._probs(x=i)) for i in range(self._num_categories), axis=-1

Check this out

@fotisdr
Copy link

fotisdr commented May 5, 2023

The previous implementation was incorrect. I've written an example implementation of how the Categorical mean should be, the code below works for me and produces the desired result. The mean should also account for unnormalised probabilities between the classes, since there are cases where the probabilities might not sum up to 1.

  def _mean(self):
    probs = self.probs_parameter()
    num_categories = self._num_categories(probs)
    # Initialise the mean with zeros
    categorical_mean = tf.zeros(tf.shape(probs[...,0]))
    # Compute the normalisation factors such that all probabilities sum up to 1
    normalisation = tf.reduce_sum(probs, axis=-1)
    # Sum up all the normalised probabilities
    # sum(i * prob(X=i) for i in range(num_categories) )
    for i in range(num_categories):
    	categorical_mean = categorical_mean + tf.cast(i,probs.dtype) * probs[...,i] / normalisation
    
    return categorical_mean

Shall I make a new commit/pull request with this?

@brianwa84
Copy link
Contributor

brianwa84 commented May 8, 2023 via email

@fotisdr
Copy link

fotisdr commented May 8, 2023

I don't think there is currently an implementation. The following might be simpler: tf.reduce_sum(tf.range(self._num_categories(probs)) * probs, axis=-1) / tf.reduce_sum(probs, axis=-1) You could send a PR, sure.

Hi,

Indeed this looks much simpler, thanks! We want to ensure that the multiplication is applied across the last axis of probs, i.e. each slice of probs across the last dimension (probs[...,i]) gets multiplied by each number i in tf.range(self._num_categories(probs)). The multiplication should be doing this by default, but I will test the code and submit a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Declares that the user has signed CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants