tensorflow模型保存、加载之变量重命名实例
话不多说,干就完了。
变量重命名的用处?
简单定义:简单来说就是将模型A中的参数parameter_A赋给模型B中的parameter_B
使用场景:当需要使用已经训练好的模型参数,尤其是使用别人训练好的模型参数时,往往别人模型中的参数命名方式与自己当前的命名方式不同,所以在加载模型参数时需要对参数进行重命名,使得代码更简洁易懂。
实现方法:
1)、模型保存
importos importtensorflowastf weights=tf.Variable(initial_value=tf.truncated_normal(shape=[1024,2], mean=0.0, stddev=0.1), dtype=tf.float32, name="weights") biases=tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="biases") weights_2=tf.Variable(initial_value=weights.initialized_value(), dtype=tf.float32, name="weights_2") #savercheckpoint ifos.path.exists("checkpoints")isFalse: os.makedirs("checkpoints") saver=tf.train.Saver() withtf.Session()assess: init_op=[tf.global_variables_initializer()] sess.run(init_op) saver.save(sess=sess,save_path="checkpoints/variable.ckpt")
2)、模型加载(变量名称保持不变)
importtensorflowastf frommatplotlibimportpyplotasplt importos current_path=os.path.dirname(os.path.abspath(__file__)) defrestore_variable(sess): #neednotinitilizevariable,butneedtodefinethesamevariablelikecheckpoint weights=tf.Variable(initial_value=tf.truncated_normal(shape=[1024,2], mean=0.0, stddev=0.1), dtype=tf.float32, name="weights") biases=tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="biases") weights_2=tf.Variable(initial_value=weights.initialized_value(), dtype=tf.float32, name="weights_2") saver=tf.train.Saver() ckpt_path=os.path.join(current_path,"checkpoints","variable.ckpt") saver.restore(sess=sess,save_path=ckpt_path) weights_val,weights_2_val=sess.run( [ tf.reshape(weights,shape=[2048]), tf.reshape(weights_2,shape=[2048]) ] ) plt.subplot(1,2,1) plt.scatter([iforiinrange(len(weights_val))],weights_val) plt.subplot(1,2,2) plt.scatter([iforiinrange(len(weights_2_val))],weights_2_val) plt.show() if__name__=='__main__': withtf.Session()assess: restore_variable(sess)
3)、模型加载(变量重命名)
importtensorflowastf frommatplotlibimportpyplotasplt importos current_path=os.path.dirname(os.path.abspath(__file__)) defrestore_variable_renamed(sess): conv1_w=tf.Variable(initial_value=tf.truncated_normal(shape=[1024,2], mean=0.0, stddev=0.1), dtype=tf.float32, name="conv1_w") conv1_b=tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="conv1_b") conv2_w=tf.Variable(initial_value=conv1_w.initialized_value(), dtype=tf.float32, name="conv2_w") #variablenamed'weights'inckptassignedtocurrentvariableconv1_w #variablenamed'biases'inckptassignedtocurrentvariableconv1_b #variablenamed'weights_2'inckptassignedtocurrentvariableconv2_w saver=tf.train.Saver({ "weights":conv1_w, "biases":conv1_b, "weights_2":conv2_w }) ckpt_path=os.path.join(current_path,"checkpoints","variable.ckpt") saver.restore(sess=sess,save_path=ckpt_path) conv1_w__val,conv2_w__val=sess.run( [ tf.reshape(conv1_w,shape=[2048]), tf.reshape(conv2_w,shape=[2048]) ] ) plt.subplot(1,2,1) plt.scatter([iforiinrange(len(conv1_w__val))],conv1_w__val) plt.subplot(1,2,2) plt.scatter([iforiinrange(len(conv2_w__val))],conv2_w__val) plt.show() if__name__=='__main__': withtf.Session()assess: restore_variable_renamed(sess)
总结:
#之前模型中叫'weights'的变量赋值给当前的conv1_w变量
#之前模型中叫'biases'的变量赋值给当前的conv1_b变量
#之前模型中叫'weights_2'的变量赋值给当前的conv2_w变量
saver=tf.train.Saver({
"weights":conv1_w,
"biases":conv1_b,
"weights_2":conv2_w
})
以上这篇tensorflow模型保存、加载之变量重命名实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。