当前位置:网站首页>迁移学习冻结网络的方法:
迁移学习冻结网络的方法:
2022-08-01 10:59:00 【Wsyoneself】
说明:pytorch(1-3),TensorFlow(4)
fine tune就是冻结网络前面的层,然后训练最后一层
- 给优化器传入所有参数,但将要冻结层的参数的
requires_grad置为False:optimizer = optim.SGD(model.parameters(), lr=1e-2) # 传入的是所有的参数 for name, param in model.named_parameters(): if 要冻结的网络层的名字(即name的值): param.requires_grad = False - 优化器传入不冻结的网络层的参数:
optimizer = optim.SGD(model.不冻结的网络层的name.parameters(), lr=1e-2) # 优化器只传入fc2的参数 - 最优做法是:优化器只传入requires_grad=True的参数,占用的内存会更小一点,效率也会更高。代码及结合1和2
节省显存:不将不更新的参数传入
optimizer提升速度:将不更新的参数的
requires_grad设置为False,节省了计算这部分参数梯度的时间
- 代码如下:
#定义优化算子 optimizer = tf.train.AdamOptimizer( 1e-3) #选择待优化的参数 output_vars = tf.get_collection(tf.GraphKyes.TRAINABLE_VARIABLES, scope= 'outpt') train_step = optimizer.minimize(loss_score,var_list = output_vars)
把需要更新梯度的层放在tf.get_collection这个函数里面,不需要更新的不放进去。
边栏推荐
- 万字解析:vector类
- Glassmorphism design style
- PDMan-国产免费通用数据库建模工具(极简,漂亮)
- 基于ArkUI eTS开发的坚果食谱(NutRecipes)
- Qt supports HEIC/HEIF format images
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(3)后台功能
- PDMan-domestic free general database modeling tool (minimalist, beautiful)
- 【cartographer ros】10: Delay and error analysis
- 力扣解法汇总1374-生成每种字符都是奇数个的字符串
- WPF 截图控件之绘制箭头(五)「仿微信」
猜你喜欢

表达式引擎在转转平台的实践

【likeshop】回收租凭系统100%开源无加密 商城+回收+租赁

Android 安全与防护策略

Qt supports HEIC/HEIF format images

复现assert和eval成功连接或失败连接蚁剑的原因

.NET深入解析LINQ框架(三:LINQ优雅的前奏)

Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (4) Opening Report

招聘随想2022

The meaning and trigger conditions of gc

在线GC日志分析工具——GCeasy
随机推荐
Android Security and Protection Policy
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
xss漏洞学习
cisco交换机基本配置命令(华为交换机保存命令是什么)
retired paddling
Promise学习(一)Promise是什么?怎么用?回调地狱怎么解决?
CTO strongly banning the use of the Calendar, that in what?
MFC实现交通图导航系统
RK3399 platform development series on introduction to (kernel) 1.52, printk function analysis - the function call will be closed
Promise学习(二)一篇文章带你快速了解Promise中的常用API
C#/VB.NET 将PPT或PPTX转换为图像
开天aPaaS之移动手机号码空号检测【开天aPaaS大作战】
Guangyu Mingdao was selected into the list of pilot demonstration projects for the development of digital economy industry in Chongqing in 2022
STM32 personal notes - program run and fly
一篇文章,带你详细了解华为认证体系证书(1)
mysql进阶(二十二)MySQL错误之Incorrect string value中文字符输入错误问题分析
昇思大模型体验平台初体验——以小模型LeNet为例
How to Steal $100 Million from the Perfect Smart Contract
万字解析:vector类
Kaitian aPaaS mobile phone number empty number detection [Kaitian aPaaS battle]