我正在尝试以张量流概率工作的最小高斯过程示例。在尝试定义对数边际可能性之前,我可以使所有工作正常进行。在此阶段,我收到错误TypeError: Tensor is unhashable if Tensor equality is enabled. Instead,use tensor.experimental_ref() as the key.
,我尝试重塑x和y数组,但这似乎不是问题。当我尝试遵循Google高斯过程回归colab时,也遇到了此错误。谁能给我一些关于我做错事情的指示?下面是一个最小的示例。
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
# Make some data
x = np.array([1,2,4,-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.0
,tensorflow-probability=0.7.0
,numpy=1.17.2
。