Tensorflow 读取网络中的参数

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)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值