明霞山资源网 Design By www.htccd.com
如下所示:
#tensorflow 中从ckpt文件中恢复指定的层或将指定的层不进行恢复:
#tensorflow 中不同的layer指定不同的学习率
with tf.Graph().as_default():
#存放的是需要恢复的层参数
variables_to_restore = []
#存放的是需要训练的层参数名,这里是没恢复的需要进行重新训练,实际上恢复了的参数也可以训练
variables_to_train = []
for var in slim.get_model_variables():
excluded = False
for exclusion in fine_tune_layers:
#比如fine tune layer中包含logits,bottleneck
if var.op.name.startswith(exclusion):
excluded = True
break
if not excluded:
variables_to_restore.append(var)
#print('var to restore :',var)
else:
variables_to_train.append(var)
#print('var to train: ',var)
#这里省略掉一些步骤,进入训练步骤:
#将variables_to_train,需要训练的参数给optimizer 的compute_gradients函数
grads = opt.compute_gradients(total_loss, variables_to_train)
#这个函数将只计算variables_to_train中的梯度
#然后将梯度进行应用:
apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
#也可以直接调用opt.minimize(total_loss,variables_to_train)
#minimize只是将compute_gradients与apply_gradients封装成了一个函数,实际上还是调用的这两个函数
#如果在梯度里面不同的参数需要不同的学习率,那么可以:
capped_grads_and_vars = []#[(MyCapper(gv[0]), gv[1]) for gv in grads_and_vars]
#update_gradient_vars是需要更新的参数,使用的是全局学习率
#对于不是update_gradient_vars的参数,将其梯度更新乘以0.0001,使用基本上不动
for grad in grads:
for update_vars in update_gradient_vars:
if grad[1]==update_vars:
capped_grads_and_vars.append((grad[0],grad[1]))
else:
capped_grads_and_vars.append((0.0001*grad[0],grad[1]))
apply_gradient_op = opt.apply_gradients(capped_grads_and_vars, global_step=global_step)
#在恢复模型时:
with sess.as_default():
if pretrained_model:
print('Restoring pretrained model: %s' % pretrained_model)
init_fn = slim.assign_from_checkpoint_fn(
pretrained_model,
variables_to_restore)
init_fn(sess)
#这样就将指定的层参数没有恢复
以上这篇tensorflow 恢复指定层与不同层指定不同学习率的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。