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)