我正在尝试使用 TF2.0 急切模式执行精确的 GP 回归,基于来自 https://colab.research.google.com/github/tensorflow/probability/blob/master/tensorflow_probability/examples的基于原始图的示例/jupyter_notebooks/Gaussian_Process_Regression_In_TFP.ipynb
amplitude = (
np.finfo(np.float64).tiny +
tf.nn.softplus(tf.Variable(initial_value=1., name='amplitude', dtype=np.float64))
)
length_scale = (
np.finfo(np.float64).tiny +
tf.nn.softplus(tf.Variable(initial_value=1., name='length_scale', dtype=np.float64))
)
observation_noise_variance = (
np.finfo(np.float64).tiny +
tf.nn.softplus(tf.Variable(initial_value=1e-6,
name='observation_noise_variance',
dtype=np.float64))
)
kernel = tfk.ExponentiatedQuadratic(amplitude, length_scale)
gp = tfd.GaussianProcess(
kernel=kernel,
index_points=tf.expand_dims(x, 1),
observation_noise_variance=observation_noise_variance
)
neg_log_likelihood = lambda: -gp.log_prob(y)
optimizer = tf.optimizers.Adam(learning_rate=.01)
num_iters = 1000
lls_ = np.zeros(num_iters, np.float64)
for i in range(num_iters):
lls_[i] = neg_log_likelihood()
optimizer.minimize(neg_log_likelihood, var_list=[amplitude, length_scale, observation_noise_variance])
然而优化失败:
“tensorflow.python.framework.ops.EagerTensor”对象没有属性“_in_graph_mode”
如果我将幅度、length_scale 和 observation_noise_variance 分别移动到 tf.Variable,例如:
amplitude = tf.Variable(initial_value=1., name='amplitude', dtype=np.float64)
amplitude_ = (
np.finfo(np.float64).tiny +
tf.nn.softplus(amplitude)
)
优化失败:
ValueError:没有为任何变量提供梯度:['amplitude:0', 'length_scale:0', 'observation_noise_variance:0']。
我究竟做错了什么?