TensorFlow Probability(tfp)中等价于np.quantile()的函数是:

huangapple go评论51阅读模式
英文:

TensorFlow Probability (tfp) equivalent of np.quantile()

问题

我正在尝试找到TensorFlow中与np.quantile()1等效的方法。我找到了tfp.stats.quantiles()2(其中tfp代表TensorFlow Probability)。然而,它的构造与np.quantile()有一些不同。

考虑以下示例:

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

inputs = tf.random.normal((1, 4096, 4))

print("NumPy")
print(np.quantile(inputs.numpy(), q=0.9, axis=1, keepdims=False))

我不确定如何使用tfp.stats.quantile()来实现上述内容,从TFP文档中也无法确定。我尝试查看了两种方法的源代码,但没有帮助。

英文:

I am trying to find a TensorFlow equivalent of np.quantile(). I have found tfp.stats.quantiles() (tfp stands for TensorFlow Probability). However, its constructs are a bit different from that of np.quantile().

Consider the following example:

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

inputs = tf.random.normal((1, 4096, 4))

print("NumPy")
print(np.quantile(inputs.numpy(), q=0.9, axis=1, keepdims=False))

I am not sure from the TFP docs how I could write the above using tfp.stats.quantile(). I tried checking out the source code of both methods, but it didn't help.

答案1

得分: 2

以下是翻译好的部分:

让我在这里试着更有帮助些,而不像我之前在 GitHub 上的表现。

`np.quantile` 和 `tfp.stats.quantiles` 之间存在行为差异。关键的区别在于 `numpy.quantile` 会

> 计算沿指定轴的 q 分位数。

其中 `q` 是

> 要计算的分位数或分位数序列,必须介于 0 和 1 之间(包括0和1)。

而 `tfp.stats.quantiles`

> 给定样本向量 `x`,此函数通过返回 `num_quantiles + 1` 个分位点来估算分位点

因此,您需要告诉 `tfp.stats.quantiles` 您想要多少个分位数,然后选择 `q` 分位数。如果从 API 中不清楚如何做到这一点,如果您查看 `tfp.stats.quantiles` 的[源代码](https://github.com/tensorflow/probability/blob/0759c57eb0306a5bb0fcc3d105bbcab9943092a5/tensorflow_probability/python/stats/quantiles.py#L727-L741)(对于 `v0.19.0`),我们可以看到它显示了如何获得与 NumPy 相似的返回结构。

为了完整起见,使用以下方式设置虚拟环境:

```console
$ cat requirements.txt
numpy==1.24.2
tensorflow==2.11.0
tensorflow-probability==0.19.0

使我们能够运行

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

inputs = tf.random.normal((1, 4096, 4), dtype=tf.float64)
q = 0.9

numpy_quantiles = np.quantile(inputs.numpy(), q=q, axis=1, keepdims=False)

tfp_quantiles = tfp.stats.quantiles(
    inputs, num_quantiles=100, axis=1, interpolation="linear"
)[int(q * 100)]

assert np.allclose(numpy_quantiles, tfp_quantiles.numpy())

print(f"{numpy_quantiles=}")
# numpy_quantiles=array([[1.31727661, 1.2699167 , 1.28735237, 1.27137588]])
print(f"{tfp_quantiles=}")
# tfp_quantiles=<tf.Tensor: shape=(1, 4), dtype=float64, numpy=array([[1.31727661, 1.2699167 , 1.28735237, 1.27137588])>

<details>
<summary>英文:</summary>

Let me try to be more helpful here than [I was on GitHub](https://github.com/tensorflow/probability/issues/864#issuecomment-1416956051).

There is a difference in behavior between `np.quantile` and `tfp.stats.quantiles`. The key difference here is that `numpy.quantile` will

&gt; Compute the q-th quantile of the data along the specified axis.

where `q` is the

&gt; Quantile or sequence of quantiles to compute, which must be between 0 and 1 inclusive.

and `tfp.stats.quantiles`

&gt; Given a vector `x` of samples, this function estimates the cut points by returning `num_quantiles + 1` cut points

So you need to tell `tfp.stats.quantiles` how many quantiles you want and then select out the `q`th quantile. If it isn&#39;t clear how to do this just from the API, if you look at the [source for `tfp.stats.quantiles`](https://github.com/tensorflow/probability/blob/0759c57eb0306a5bb0fcc3d105bbcab9943092a5/tensorflow_probability/python/stats/quantiles.py#L727-L741) (for `v0.19.0`) we can see that it shows us how we can get a similar return structure as NumPy.

For completeness, setting up a virtual environment with

```console
$ cat requirements.txt
numpy==1.24.2
tensorflow==2.11.0
tensorflow-probability==0.19.0

allows us to run

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

inputs = tf.random.normal((1, 4096, 4), dtype=tf.float64)
q = 0.9

numpy_quantiles = np.quantile(inputs.numpy(), q=q, axis=1, keepdims=False)

tfp_quantiles = tfp.stats.quantiles(
    inputs, num_quantiles=100, axis=1, interpolation=&quot;linear&quot;
)[int(q * 100)]

assert np.allclose(numpy_quantiles, tfp_quantiles.numpy())

print(f&quot;{numpy_quantiles=}&quot;)
# numpy_quantiles=array([[1.31727661, 1.2699167 , 1.28735237, 1.27137588]])
print(f&quot;{tfp_quantiles=}&quot;)
# tfp_quantiles=&lt;tf.Tensor: shape=(1, 4), dtype=float64, numpy=array([[1.31727661, 1.2699167 , 1.28735237, 1.27137588]])&gt;

答案2

得分: 1

你也可以使用 tfp.stats.percentile(inputs, 90., axis=1, keepdims=False) -- 唯一的区别是 90. 替代了 .90.

英文:

You could also use tfp.stats.percentile(inputs, 90., axis=1, keepdims=False) -- the only difference from quantile is the 90. replacing .90.

huangapple
  • 本文由 发表于 2023年2月6日 09:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定