小编Ngu*_*hoi的帖子

如何在tensorflow中训练后使用模型(保存/加载图)

我的张量流版本是0.11.我希望在训练后保存图形或保存tensorflow可以加载的其他东西.

我/使用导出和导入MetaGraph

我已经阅读过这篇文章: Tensorflow:如何保存/恢复模型?

我的Save.py文件:

X = tf.placeholder("float", [None, 28, 28, 1], name='X')
Y = tf.placeholder("float", [None, 10], name='Y')

tf.train.Saver()
with tf.Session() as sess:
     ...run something ...
     final_tensor = tf.nn.softmax(py_x, name='final_result')
     tf.add_to_collection("final_tensor", final_tensor)

     predict_op = tf.argmax(py_x, 1)
     tf.add_to_collection("predict_op", predict_op)

saver.save(sess, 'my_project') 
Run Code Online (Sandbox Code Playgroud)

然后我运行load.py:

with tf.Session() as sess:
   new_saver = tf.train.import_meta_graph('my_project.meta')
   new_saver.restore(sess, 'my_project')
   predict_op = tf.get_collection("predict_op")[0]
   for i in range(2):
        test_indices = np.arange(len(teX)) # Get A Test Batch
        np.random.shuffle(test_indices)
        test_indices = test_indices[0:test_size]

        print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
                         sess.run(predict_op, feed_dict={"X:0": teX[test_indices],
                                                         "p_keep_conv:0": …
Run Code Online (Sandbox Code Playgroud)

python machine-learning neural-network deep-learning tensorflow

5
推荐指数
1
解决办法
3902
查看次数