当前位置:网站首页>迁移学习冻结网络的方法:
迁移学习冻结网络的方法:
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这个函数里面,不需要更新的不放进去。
边栏推荐
猜你喜欢
随机推荐
解决vscode输入! 无法快捷生成骨架(新版vscode快速生成骨架的三种方法)
小程序毕设作品之微信美食菜谱小程序毕业设计成品(1)开发概要
Push the local project to the remote repository
4种常见的鉴权方式及说明
Introduction to STM32 development Introduce IIC bus, read and write AT24C02 (EEPROM) (using analog timing)
DBPack SQL Tracing 功能及数据加密功能详解
小程序毕设作品之微信美食菜谱小程序毕业设计成品(4)开题报告
Small application project works WeChat gourmet recipes applet graduation design of finished product (1) the development profile
C#/VB.NET 将PPT或PPTX转换为图像
7/31 训练日志
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (2) Mini Program Functions
跨域网络资源文件下载
DBPack SQL Tracing 功能及数据加密功能详解
Glassmorphism design style
监视网络连接的ss命令
从零开始Blazor Server(4)--登录系统
Google Earth Engine APP——15行代码搞定一个inspector高程监测APP
Drawing arrows of WPF screenshot control (5) "Imitation WeChat"
WPF 截图控件之绘制箭头(五)「仿微信」
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (4) Opening Report








