Search code examples
pythontensorflowkerasnormal-distribution

how to compute norm.ppf() in tensorflow/Keras?


I want to use inverse of cumulative distribution function (cdf) which can be done using norm.ppf() from scipy https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.stats.norm inside a layer of a tf/keras model architecture.


Solution

  • As per documentation, tfp.distribution.Normal does have a method for calculating ppf (percent point function). It's called quantile:

    scipy.stats.norm(loc=0, scale=1).ppf(0.95)
    

    Output:

    1.6448536269514722
    

    Tensorflow:

    tfp.distributions.Normal(loc=0, scale=1).quantile(0.95)
    

    Output:

    <tf.Tensor: shape=(), dtype=float32, numpy=1.6448536>