python - Tensorflow report error with the restored training model -
i'm newbie machine learning , tensorflow, using example tutorial source code, model trained , printed accuracy, doesn't include source code export model , variables , import predict new image.
so revised source code export model, , create new python script predict using test data set.
here source code export training model:
mnist = input_data.read_data_sets(flags.data_dir, one_hot=true) print("run here3") # create model x = tf.placeholder(tf.float32, [none, 784], name="x") w = tf.variable(tf.zeros([784, 10]), name="w") b = tf.variable(tf.zeros([10])) y = tf.matmul(x, w) + b saver = tf.train.saver() sess = tf.interactivesession() ... ignore source code cost function definition , train model #after model trained, save variables , y tf.add_to_collection('w', w) tf.add_to_collection('b', b) tf.add_to_collection('y', y) saver.save(sess, 'result')
in new python script, try restore model , re-execute y function
sess = tf.session() saver = tf.train.import_meta_graph('result.meta') saver.restore(sess, tf.train.latest_checkpoint('./')) w = tf.get_collection('w')[0] b = tf.get_collection('b')[0] y = tf.get_collection('y')[0] mnist = input_data.read_data_sets('/tmp/tensorflow/mnist/input_data', one_hot=true) img = mnist.test.images[0] x = tf.placeholder(tf.float32, [none, 784]) sess.run(y, feed_dict={x: mnist.test.images})
everything works correctly, w , b value if print them, error while executing last statement(run y function).
caused op u'x', defined at: file "predict.py", line 58, in <module> tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) file "predict.py", line 25, in main saver = tf.train.import_meta_graph('result.meta') file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site- packages/tensorflow/python/training/saver.py", line 1566, in import_meta_graph **kwargs) file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 498, in import_scoped_meta_graph producer_op_list=producer_op_list) file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 288, in import_graph_def op_def=op_def) file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op original_op=self._default_original_op, op_def=op_def) file "/users/zhouqi/git/machine-learning/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ self._traceback = _extract_stack() invalidargumenterror (see above traceback): must feed value placeholder tensor 'x' dtype float [[node: x = placeholder[dtype=dt_float, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
it's weird cause use same statement define x, , feed x using same approach while executing y function, don't know why not work?
the problem new placeholder:
x = tf.placeholder(tf.float32, [none, 784])
creating placeholder same name not enough. need same placeholder used when created model. therefore must add x collection in first file:
tf.add_to_collection('x', x)
and load in new file:
x = tf.get_collection('x')[0]
instead of creating new one.
Comments
Post a Comment