Search code examples
pythontensorflowtensorflow-probability

Tensorflow-probability error with 'Tensor is unhashable' in Gaussian Process


I am trying to get a minimal gaussian process example working in tensorflow probability. I can get everything to work until I try defining the log marginal likelihood. At this stage I get the error TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key. I have tried reshaping the x and y arrays but that doesn't seem to be the issue. I also had this error when I tried following the google gaussian process regression colab. Can anyone give me some pointers on what I am doing wrong? Below is a minimal example.

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)

On my computer the versions are tensorflow=2.0.0, tensorflow-probability=0.7.0, numpy=1.17.2.


Solution

  • With TF 2.0.0 you should use TFP 0.8.0.