【问题标题】:tensor is unhashable error when sampling from distribution with tensorflow probability (on colab)从具有张量流概率的分布中采样时,张量是不可散列的错误(在 colab 上)
【发布时间】:2019-12-06 10:44:02
【问题描述】:

我想将我的一些代码移至 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 版本。

【问题讨论】:

    标签: python tensorflow google-colaboratory tensorflow-probability


    【解决方案1】:

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

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

    !pip install tensorflow-probability==0.8.0rc0
    

    然后重新启动运行时。

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

    【讨论】:

    • 新版本已发布:如果您使用的是 TF2 的当前稳定版本 2.1,则匹配的 tensorflow probabiltiy 版本为 0.9,您可以使用 !pip install tensorflow-probability== 安装0.9.0.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2012-04-14
    相关资源
    最近更新 更多