从具有张量流概率的分布中采样时,张量是不可散列的错误(在 colab 上)

tensor is unhashable error when sampling from distribution with tensorflow probability (on colab)

我想将我的一些代码移动到 tensorflow 发行版,并且正在学习 colab 上的教程。运行时请求tensorflow版本2.x后,官方示例代码不再有效

可以find the colab notebook here,我在最开始添加了如下代码:

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass

我只有 运行 colab 上的代码,但我不认为它是特定于它的。如果你想尝试在你本地的tensorflow安装上重现这个问题,这应该是相关代码:

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions

nd = tfd.MultivariateNormalDiag(loc=[0., 10.], scale_diag=[1., 4.])
nd.sample()

nd 采样的最后一行创建了以下错误消息:

TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

tensorflow概率为0.7.0版本,tensorflow为2.0.0版本

哦,刚发帖就发现了这个问题:https://github.com/tensorflow/probability/issues/540

问题已通过安装更新​​版本的 tensorflow probability 解决。 在 colab 中,可以通过执行来完成:

!pip install tensorflow-probability==0.8.0rc0

然后重新启动运行时。

我在这里也检查了本地机器,它的tensorflow-probability 0.8.0。但是,如果您尝试在 colab 中安装它,它会抱怨某些依赖项不匹配。我想当 Google 在这些教程笔记本中推出他们的库的最新版本时,这整个问题很快就会自行解决。