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

问题描述

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

你可以在这里找到 colab notebook,我在最开始添加了以下代码:

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 版本。

标签: pythontensorflowgoogle-colaboratorytensorflow-probability

解决方案


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

通过安装更新​​版本的 tensorflow 概率来解决该问题。在 colab 中,可以通过执行:

!pip install tensorflow-probability==0.8.0rc0

然后重新启动运行时。

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


推荐阅读