Tensorflow 读取网络中的参数
使用Tensorflow得到的结构,读取参数:
方法一:
首先确定网络建立过程中,给每个weight和bias 命名,例如:
weight2 = {
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.01),name='w1'),
'wc2': tf.Variable(tf.random_normal([3, 3, 64, 1], stddev=0.01),name='w2'),
'wc3': tf.Variable(tf.random_normal([9, 9, 1, 64], stddev=0.01),name='w3')
}
其中的name很重要。
通过训练网络保存参数:
#模型存储:
model_save_path = './multi_cnn/model_cnn/'
model_name = 'model.ckpt'
saver = tf.train.Saver(tf.all_variables())
checkpoint_path = os.path.join(model_save_path, model_name)
saver.save(sess, checkpoint_path, global_step=epoch)
通过上述存储,然后读取存储模型中的参数:
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state('./model_cnn/')
saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path+'.meta')
saver.restore(sess, ckpt.model_checkpoint_path)
graph = tf.get_default_graph()
fc1 = graph.get_tensor_by_name("w1:0")
fc2 = graph.get_tensor_by_name("w7:0")
fc3 = graph.get_tensor_by_name("w8:0")
print fc #得到的是shape
print sess.run(fc1) #这样可以输出参数
print sess.run(fc2)
print sess.run(fc3)