【问题标题】:Tensorflow-probability error with 'Tensor is unhashable' in Gaussian Process高斯过程中“张量不可散列”的张量流概率错误
【发布时间】:2019-11-05 22:17:45
【问题描述】:

我正在尝试获得一个在张量流概率中工作的最小高斯过程示例。在我尝试定义对数边际可能性之前,我可以让一切正常工作。在这个阶段,我收到错误TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key. 我已经尝试重塑 x 和 y 数组,但这似乎不是问题。当我尝试遵循谷歌高斯过程回归 colab 时,我也遇到了这个错误。谁能给我一些关于我做错了什么的指示?下面是一个最小的例子。

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
# Make some data
x = np.array([1, 2, 4, 0, -1, -2, -3], dtype=np.float64)
y = np.sin(x)
noise = x * 0 + 0.1
# Define a mean function
def meanfn(y):
    return tf.constant([np.mean(y)], dtype=np.float64)
# Define the kernel
periodic_amplitude = tf.exp(tf.Variable(np.float64(0)), name='periodic_amplitude')
periodic_length_scale = tf.exp(tf.Variable(np.float64(1)), name='periodic_length_scale')
periodic_period = tf.exp(tf.Variable(np.float64(0)), name='periodic_period')
local_periodic_kernel = tfp.positive_semidefinite_kernels.ExpSinSquared(amplitude=periodic_amplitude, length_scale=periodic_length_scale, period=periodic_period)
# Define the gp
gp = tfp.distributions.GaussianProcess(
    mean_fn=meanfn,
    kernel=local_periodic_kernel,
    index_points=x.reshape(-1,1),
    observation_noise_variance=noise)
# Negative marginal likelihood
neg_marginal_lik = -gp.log_prob(y)

在我的电脑上,版本是tensorflow=2.0.0tensorflow-probability=0.7.0numpy=1.17.2

【问题讨论】:

    标签: python tensorflow tensorflow-probability


    【解决方案1】:

    对于 TF 2.0.0,您应该使用 TFP 0.8.0。

    【讨论】:

    • 谢谢!我使用 Conda 安装软件包,0.7.0 是 Conda 上的最新版本,所以我认为它是当前版本。我刚刚用 pip 安装了 0.8.0 并且可以确认我的代码现在可以工作了。
    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多