我试着训练一个探地雷达模型和一个tensorflow模型。训练部分没有问题。但是对于使用经过训练的模型进行预测,我在tf.placeholder op中接收到一个类型错误。
pred, uncp=sess.run([my, yv], feed_dict={X:xtr})代码类似于usage.html的第二个示例
import numpy as np
import tensorflow as tf
import gpflow
float_type = gpflow.settings.float_type
gpflow.reset_default_graph_and_session()
def cnn_fn(x, output_dim):
out= tf.layers.dense(inputs=x, units=output_dim, activation=tf.nn.relu)
print(out)
return out
N = 150
xtr = np.random.rand(N,1)
ytr = np.sin(12*xtr) + 0.66*np.cos(25*xtr) + np.random.randn(N,1)*0.1 + 3
xtr = np.random.rand(N,28)
print(xtr.shape, ytr.shape)
nepoch=50
gp_dim=xtr.shape[1]
print(gp_dim)
minibatch_size = 16
X = tf.placeholder(tf.float32, [None, gp_dim])
Y = tf.placeholder(tf.float32, [None, 1])
with tf.variable_scope('cnn'):
f_X = tf.cast(cnn_fn(X, gp_dim), dtype=float_type)
k = gpflow.kernels.Matern52(gp_dim)
gp_model = gpflow.models.GPR(f_X, tf.cast(Y, dtype=float_type), k)
loss = -gp_model.likelihood_tensor
m, v = gp_model._build_predict(f_X)
my, yv = gp_model.likelihood.predict_mean_and_var(m, v)
with tf.variable_scope('adam'):
opt_step = tf.train.AdamOptimizer(0.001).minimize(loss)
tf_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='adam')
tf_vars += tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='cnn')
## initialize
sess = tf.Session()
sess.run(tf.variables_initializer(var_list=tf_vars))
gp_model.initialize(session=sess)
for i in range(nepoch):
shind=np.array(range(len(xtr)))
np.random.shuffle(shind)
for j in range(int(len(xtr)/minibatch_size)):
ind=shind[j*minibatch_size: (j+1)*minibatch_size]
sess.run(opt_step, feed_dict={X:xtr[ind], Y:ytr[ind]})执行上述代码运行良好。但是,添加以下行会出现一个错误:
pred, uncp=sess.run([my, yv], feed_dict={X:xtr})有以下错误:
<ipython-input-25-269715087df2> in <module>
----> 1 pred, uncp=sess.run([my, yv], feed_dict={X:xtr})
[...]
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,1]
[[node Placeholder_1 (defined at <ipython-input-24-39ccf45cd248>:2) = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]发布于 2019-05-10 19:40:01
您的代码失败的原因是您实际上没有将值提供给其中一个占位符。如果你真的给他们起了名字,这就更容易辨认了:
X = tf.placeholder(tf.float32, [None, gp_dim], name='X')
Y = tf.placeholder(tf.float32, [None, 1], name='Y')Tensorflow需要很好地定义整个计算图,所使用的GPR模型依赖于X 和 Y。如果运行以下行,它可以正常工作:
pred, uncp = sess.run([my, yv], feed_dict={X: xtr, Y: ytr})Update:正如user1018464在评论中指出的那样,您使用的是GPR模型,其中的预测直接依赖于培训数据(例如,参见http://www.gaussianprocess.org/gpml/chapters/RW2.pdf第16页上的等式(2.22)和(2.23) )。因此,您需要同时传入xtr和ytr来进行预测。
其他模型(如SVGP )通过“诱导特性”(通常是“伪输入/输出”对)来表示函数,这些对概括了数据,在这种情况下,您根本不需要输入原始输入值(我第一次回答时就弄错了)。
您可以按以下方式设置模型:
gp_model = gpflow.models.SVGP(f_X, tf.cast(Y, dtype=float_type), k, gpflow.likelihoods.Gaussian(), xtr.copy(), num_data=N)然后pred, uncp=sess.run([my, yv], feed_dict={X:xtr})按预期工作。
如果您想在不同的位置预测Xtest,您需要设置一个单独的占位符,并重用cnn (注意reuse=True在variable_scope中的同名),如笔记本中的示例2所示:
Xtest = tf.placeholder(tf.float32, [None, Mnist.input_dim], name='Xtest')
with tf.variable_scope('cnn', reuse=True):
f_Xtest = tf.cast(cnn_fn(Xtest, gp_dim), dtype=float_type)像以前一样使用f_X建立模型,但在调用_build_predict时使用f_Xtest
m, v = gp_model._build_predict(f_Xtest)
my, yv = gp_model.likelihood.predict_mean_and_var(m, v)现在需要将X、Y和Xtest传入会话的run():
pred, uncp = sess.run([my, yv], feed_dict={X: xtr, Y: Ytr, Xtest: xtest})其中xtest是numpy数组,其中包含要预测的点。
发布于 2019-05-13 16:53:14
GPflow为您管理TensorFlow会话,当您单独使用GPflow时,您不需要创建自己的TF会话。在您的示例中,tf.layers.dense生成新的变量,应该初始化这些变量,我建议使用由GPflow创建的会话。实际上,您需要替换这些行
## initialize
sess = tf.Session()
sess.run(tf.variables_initializer(var_list=tf_vars))
gp_model.initialize(session=sess)通过以下方式:
sess = gpflow.get_default_session()
sess.run(tf.variables_initializer(var_list=tf_vars)或者用默认会话上下文包装代码:
with tf.Session() as session:
... build and runhttps://stackoverflow.com/questions/56069296
复制相似问题