当我保存并恢复它的tensorflow模型时,它的形状变量错误,我不知道为什么。
这是我的代码:
import os
import tensorflow as tf
X = tf.placeholder(tf.float32, shape=[None, 2], name="X")
Y = tf.placeholder(tf.float32, shape=[None, 1], name="Y")
W = tf.Variable(tf.random_normal([2, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
hypo = tf.sigmoid(tf.matmul(X, W) +b)
cost = -tf.reduce_mean(Y*(tf.log*(hypo)) + (1-Y)*(tf.log(1-hypo)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
train = optimizer.minimize(cost)
#### Saving model
SAVER_DIR = "model"
saver = tf.train.Saver()
checkpoint_path = os.path.join(SAVER_DIR, "model")
ckpt = tf.train.get_checkpoint_state(SAVER_DIR)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
cost_val, hy_val, _ = sess.run([cost, hypo, train], feed_dict={X:x_dat, Y=y_dat})
saver.save(sess, checkpoint_path, global_step=step)
输入数据“ x_dat”由两列组成,而“ y_dat”是单列。
我制作了[?,2]形的占位符“ X”和[2,1]变量“ W”。
之后,我恢复了模型并使用以下代码检查了形状:
#### Restoring model
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.import_meta_graph('./model/model-2000.meta')
saver.restore(sess,'./model/model-2000')
op = sess.graph.get_operations()
for m in op :
print(m.values())
结果:
(<tf.Tensor 'X:0' shape=(?, 1) dtpye=float32>,)
(<tf.Tensor 'Y:0' shape=(?, 1) dtpye=float32>,)
...
(<tf.Tensor 'weight:0' shape=(1, 1) dtpye=float32_ref>,)
...
(<tf.Tensor 'bias:0' shape=(1,) dtpye=float32_ref>,)
为什么X和重量张量的形状与我保存的模型相比有所不同? 以及如何将输入数据用作关于此还原模型的两列?