keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.93k stars 19.45k forks source link

Add `beta` and `binomial` distribution functions in `keras.random` #18918

Closed KhawajaAbaid closed 10 months ago

KhawajaAbaid commented 10 months ago

Hi, while working on my project that is based on keras I realized that the keras.random module is missing the two distribution functions that I need in my project, namely beta and binomial, So, I've implemented these two functions in all four backends currently being supported by Keras, i.e. TensorFlow, Jax, PyTorch and Numpy.

I've created gists of these implementations for review's sake before creating a PR as recommended by the guidelines:

  1. PyTorch: https://gist.github.com/KhawajaAbaid/16b6ebc7c90a04896001f32d4659f6fb
  2. Jax: https://gist.github.com/KhawajaAbaid/c5c8a6be502af3a559c011bb2723efda
  3. Numpy: https://gist.github.com/KhawajaAbaid/029742532ac8625340c7ed70263ed2da
  4. TensorFlow: https://gist.github.com/KhawajaAbaid/4bcfd373b776c44867484315a84dbc57

Please note that as tensorflow doesn't offer a built-in method for beta function so I've implemented a workaround using a statistical formula to use gamma distributed random variables to derive beta distributed random variable. Specifically, $U(a, b) = X(a) / (X(a) + Y(b))$ where $U(a,b)$ is the beta distributed random variable using parameters $a$ and $b$ and $X(a)$ and $Y(b)$ are gamma-distributed random variables using parameter $a$ and $b$ respectively.

I think having these two functions part of the keras.random module will greatly help other ML practitioners as they won't have to implement all these variants of these functions to take advantage of backend agnosticism of Keras in their projects.

fchollet commented 10 months ago

Sounds good, we can add them. If you're able, please send a PR.

KhawajaAbaid commented 10 months ago

Thank you. I'll create a PR in a few minutes as I've pretty much already implemented everything. :D

KhawajaAbaid commented 10 months ago

I've created a PR at https://github.com/keras-team/keras/pull/18920

(Really sorry, it took longer than I thought)

SuryanarayanaY commented 10 months ago

Hi @KhawajaAbaid ,

As the proposed PR got merged could you please mark this issue as resolved. Thanks!

KhawajaAbaid commented 10 months ago

Yes sure. Thanks for the reminder.