我一直在遵循成功保存/恢复 GPflow 模型的方法。但现在我遇到了障碍。
当我尝试使用线性均值函数恢复模型时,恢复会因错误而崩溃。
我认为问题在于 tensorflow 线性均值函数对象的命名约定。上面的“-44dbadbb-0”是随机的,每次重建模型时都会发生变化,所以如果我在保存模型时检查张量名称
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
print_tensors_in_checkpoint_file(file_name='./model.ckpt', tensor_name='', all_tensors=False)
我得到回报:
线性-eeb5f9f3-0/A/无约束 (DT_DOUBLE) [1,1] 线性-eeb5f9f3-0/b/无约束 (DT_DOUBLE) [1] 模型/X/dataholder (DT_DOUBLE) [15,1] 模型/Y/dataholder (DT_DOUBLE) [15,1] model/kern/kernels/0/lengthscales/unconstrained (DT_DOUBLE) [] model/kern/kernels/0/variance/unconstrained (DT_DOUBLE) [] model/kern/kernels/1/lengthscales/无约束 (DT_DOUBLE) [] 模型/内核/内核/1/方差/无约束 (DT_DOUBLE) [] 模型/似然/方差/无约束 (DT_DOUBLE) []
线性函数显然与试图恢复的模型具有不同的名称。
我试图通过在恢复之前重命名变量来解决这个问题,但这不适用于 tensorflow。我也尝试了不同的保存/恢复方法,但是我无法从模型中采样。
保存模型
import gpflow
import numpy as np
import random
import tensorflow as tf
# define data
rng = np.random.RandomState(4)
X = rng.uniform(0, 5.0, 15)[:, np.newaxis]
Y = np.sin((X[:, 0] - 2.5) ** 2).reshape(len(X),1)
# define the mean function
mf = gpflow.mean_functions.Linear(np.ones((1,1)),np.zeros((1,)))
# create the GP model
with gpflow.defer_build():
k = gpflow.kernels.Matern32(1)+gpflow.kernels.RBF(1)
m = gpflow.models.GPR(X, Y, kern=k,name='model',mean_function=mf)
m.likelihood.variance = 1e-03
m.likelihood.trainable = False
tf.global_variables_initializer()
tf_session = m.enquire_session()
m.compile( tf_session )
gpflow.train.ScipyOptimizer().minimize(m)
saver = tf.train.Saver()
save_path = saver.save(tf_session, "./model.ckpt")
print("Model saved in path: %s" % save_path)
恢复模型
import gpflow
import numpy as np
import random
import tensorflow as tf
# define data
rng = np.random.RandomState(4)
X = rng.uniform(0, 5.0, 15)[:, np.newaxis]
Y = np.sin((X[:, 0] - 2.5) ** 2).reshape(len(X),1)
# define the mean function
mf = gpflow.mean_functions.Linear(np.ones((1,1)),np.zeros((1,)))
with gpflow.defer_build():
k = gpflow.kernels.Matern32(1)+gpflow.kernels.RBF(1)
m = gpflow.models.GPR(X, Y, kern=k,name='model',mean_function=mf)
m.likelihood.variance = 1e-03
m.likelihood.trainable = False
# construct and compile the tensorflow session
tf.global_variables_initializer()
tf_session = m.enquire_session()
m.compile( tf_session )
saver = tf.train.Saver()
save_path = saver.restore(tf_session, "./model.ckpt")
print("Model loaded from path: %s" % save_path)
m.anchor(tf_session)
save_path = saver.restore(tf_session, "./model.ckpt")
代码因错误而崩溃:
NotFoundError(请参阅上面的回溯):在检查点中找不到 Key Linear-44dbadbb-0/A/unconstrained...