当前位置:网站首页>Transfer learning to freeze the network:
Transfer learning to freeze the network:
2022-08-01 11:05:00 【Wsyoneself】
Description: pytorch (1-3), TensorFlow (4)
fine tune is to freeze the front layers of the network, and then train the last layer
- Pass all parameters to the optimizer, but set the
requires_gradof the parameters of the layer to be frozen toFalse:optimizer = optim.SGD(model.parameters(), lr=1e-2) # All parameters are passed infor name, param in model.named_parameters():if the name of the network layer to be frozen (that is, the value of name):param.requires_grad = False - The optimizer passes in the parameters of the unfrozen network layer:
optimizer = optim.SGD(model.name.parameters() of the unfrozen network layer, lr=1e-2) # The optimizer only passes in the parameters of fc2 - The best practice is: the optimizer only passes in the parameter of requires_grad=True, the memory occupied will be smaller, and the efficiency will be higher.Code and Combining 1 and 2
Save video memory: do not pass parameters that will not be updated to
optimizerIncrease the speed: set the
requires_gradof the parameters that are not updated toFalse, saving the time to calculate the gradient of these parameters
- The code is as follows:
#Define optimization operatoroptimizer = tf.train.AdamOptimizer(1e-3)#Select the parameters to be optimizedoutput_vars = tf.get_collection(tf.GraphKyes.TRAINABLE_VARIABLES, scope= 'outpt')train_step = optimizer.minimize(loss_score,var_list = output_vars)
Put the layer that needs to update the gradient in the tf.get_collection function, and do not put it in if it does not need to be updated.- The main function of the function: from a collectionretrieve variable
- is used to get all the elements in the key collection and return a list.The order of the list is based on the order in which the variables were placed in the set.scope is an optional parameter, indicating the namespace (namedomain), if specified, it will return a list of all variables put into 'key' in the name domain (for example, the outpt description in the sample code is the parameter to return the outpt layer), if not specified, it will return all variables.
边栏推荐
- 力扣解法汇总1374-生成每种字符都是奇数个的字符串
- PDMan-domestic free general database modeling tool (minimalist, beautiful)
- 怎么找出电脑隐藏的软件(如何清理电脑隐藏软件)
- Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)
- CTFshow,命令执行:web32
- 图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
- 复现assert和eval成功连接或失败连接蚁剑的原因
- .NET analyzes the LINQ framework in depth (three: the elegant prelude of LINQ)
- WPF 截图控件之绘制箭头(五)「仿微信」
- C language implementation!20000 in 4 seconds
猜你喜欢

2022年中盘点 | 产品打底,科技背书,广汽集团阔步向前

EasyRecovery热门免费数据检测修复软件

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

Why Metropolis–Hastings Works

PowerPC技术与市场杂谈

gc的意义和触发条件

.NET性能优化-使用SourceGenerator-Logger记录日志

Drawing arrows of WPF screenshot control (5) "Imitation WeChat"

Solve vscode input! Unable to quickly generate skeletons (three methods for the new version of vscode to quickly generate skeletons)

OpenHarmony高校技术俱乐部计划发布
随机推荐
.NET深入解析LINQ框架(三:LINQ优雅的前奏)
Promise learning (1) What is Promise?how to use?How to solve callback hell?
跨域网络资源文件下载
MFC实现交通图导航系统
C#/VB.NET 将PPT或PPTX转换为图像
4种常见的鉴权方式及说明
如何从完美的智能合约中窃取 1 亿美元
轮询和长轮询的区别
新书上市 |《谁在掷骰子?》在“不确定性时代”中确定前行
Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (3) Background Functions
Promise learning (4) The ultimate solution for asynchronous programming async + await: write asynchronous code in a synchronous way
利用正则表达式的回溯实现绕过
小程序毕设作品之微信美食菜谱小程序毕业设计成品(1)开发概要
MacOS下postgresql(pgsql)数据库密码为什么不需要填写或可以乱填写
新一代超安全蜂窝电池, 思皓爱跑上市13.99万元起售
【云驻共创】分布式技术之华为云全域调度技术与实践
JWT
退役划水
CTFshow,命令执行:web33
July 31, 2022 -- Take your first steps with C# -- Use C# to create readable code with conventions, spaces, and comments