在进行调试的时候,可以保存模型,,加载模型
保存模型的一种是保存成checkpoint,而如何从保
import tensorflow as tf
v1 = tf.Variable(tf.random_normal([1, 2]), name="v1")
v2 = tf.Variable(tf.random_normal([2, 3]), name="v2")
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
print("v1:", sess.run(v1))
print("v2:", sess.run(v2))
saver_path = saver.save(sess, "save/model.ckpt")
print("Model saved in file:", saver_path)
存的文件中加载变量,进行调试进行我们进行试验分析结果的重点
上述代码进行一个简单的保存模型
import tensorflow as tf
sess = tf.Session()
check_point_path = 'save'
saver = tf.train.import_meta_graph('/home/wb-lxx491269/test/save/model.ckpt.meta')
saver.restore(sess, tf.train.latest_checkpoint(check_point_path))
graph = tf.get_default_graph()
#print(graph.get_operations())
#with open('op.txt','a') as f:
# f.write(str(graph.get_operations()))
op1 = graph.get_tensor_by_name('v1:0')
print(op1)
op1 = graph.get_tensor_by_name('v1:0')
获取某一步步骤的变量
获取所有的变量进行查看
from tensorflow.python import pywrap_tensorflow
import os
model_dir = '/home/wb-lxx491269/test/save'
checkpoint_path = os.path.join(model_dir, "model.ckpt")
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
print("tensor_name: ", key)
print(reader.get_tensor(key)) # Remove this is you want to print only variable namesi