In situation of finetuning, parameters in backbone network need to be frozen. To achieve this target, there are two steps.

First, locate the layers and change their requires_grad attributes to be False.

for param in net.backbone.parameters():
param.requires_grad = False
for pname, param in net.named_parameters():
if(key_word in pname):
param.requires_grad = False

Here we use parameters() or named_parameters() method, it will give both bias and weight.

Second, filter out those parameters who need to be updated and pass them to the optimizer.

optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad == True, net.parameters()), lr=learning_rate, momentum=mom)

PyTorch固定参数的更多相关文章

  1. 函数的非固定参数-Day3

    一.函数非固定参数 1.默认函数,我们在传参之前,选给参数指定一个默认的值.默认参数特点是非必须传递的. def test(x,y=2): print(x) print(y) print(" ...

  2. 函数和常用模块【day04】:函数的非固定参数(三)

    本节内容 1.概述 2.默认参数 3.参数组 4.总结 一.概述 在上一篇博客中我已经写了,位置参数和关键字参数,下面我们来谈谈默认参数和参数组 二.默认参数 默认参数指的是,我们在传参之前,先给参数 ...

  3. 非固定参数:*args和 **kwargs

    先看一个固定参数栗子: def func1(x, args): print(x, args) func1(1,22) ====================1 22 ================ ...

  4. 【python】-- 函数非固定参数,返回值(return)

    函数非固定参数 1.默认参数: 代码如下: def information_register(name,age,country,sex): print("----注册信息------&quo ...

  5. HandlerMethodArgumentResolver(二):Map参数类型和固定参数类型【享学Spring MVC】

    每篇一句 黄金的导电性最好,为什么电脑主板还是要用铜? 飞机最快,为什么还有人做火车? 清华大学最好,为什么还有人去普通学校? 因为资源都是有限的,我们现实生活中必须兼顾成本与产出的平衡 前言 上文 ...

  6. pytorch固定部分参数

    pytorch固定部分参数 不用梯度 如果是Variable,则可以初始化时指定 j = Variable(torch.randn(5,5), requires_grad=True) 但是如果是m = ...

  7. python学习第二十六天非固定参数几种情况

    python函数参数传递,位置参数,默认参数,关键词参数,最后介绍一个非固定参数,就可以向函数传递一个列表,元组,字典,具体看看用法 1,有一个* 号的参数情况 def goos_stu(id,*us ...

  8. pytorch固定BN层参数

    背景:基于PyTorch的模型,想固定主分支参数,只训练子分支,结果发现在不同epoch相同的测试数据经过主分支输出的结果不同. 原因:未固定主分支BN层中的running_mean和running_ ...

  9. 页面间固定参数,通过cookie传值

    最后在做的页面,比如用户数据(用户头像,名称,年龄)这些信息,因为大部分页面都要用,之前是通过url地址传,另一页面接收.考虑到这样做会让url过长,因此,尝试使用cookie,把固定的值保存在coo ...

随机推荐

  1. axios 请求数据跳转页面报'$router' of undefined问题

    代码: this.$axios.post("/auth", { 'username': this.username, 'password': this.password }).th ...

  2. [ASP.NET MVC]@Partial 和@RenderPartial的区别

    @Partial 和@RenderPartial的区别 Html.partial和RenderPartial的用法与区别 Html.partial和RenderPartial都是输出html片段,区别 ...

  3. JDBC基础篇(MYSQL)——PreparedStatement执行DML、DQL等

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package day03.prepare; import java.sql.Connection; import java.sql ...

  4. C程序设计学习笔记(完结)

    时间:2015-4-16 09:17 不求甚解,每有会意,欣然忘食.学习的过程是痛苦的 第1章    程序设计和C语言     第2章    算法--程序的灵魂   -算法的五个特点          ...

  5. springboot:嵌套使用异步注解@Async还会异步执行吗

    一.引言 在前边的文章<[springboot:使用异步注解@Async的那些坑>中介绍了使用@Async注解获取任务执行结果的错误用法,今天来分享下另外一种常见的错误. 二.代码演示 下 ...

  6. VS C++ C# 混合编程

    创建C++ DLL 注意,32bit和64bit之间不兼容 创建普通dll工程 设置Properties -> Configuration Properties -> General -& ...

  7. RHCS集群架构之mysql及共享存储iscsi

    server1 172.25.7.1(配置Nginx.ricci和luci) server2 172.25.7.2(Apache.iscsi) server3 172.25.7.3(Apache) s ...

  8. 新版idea无法导入mavenweb模板

    目前没有任何办法,最好是下个旧版的

  9. introduction-to-64-bit-assembly

    introduction-to-64-bit-assembly NASM - The Netwide Assembler x86-64 下函数调用及栈帧原理 汇编语言基本概念简介 mycode

  10. uniapp H5 打包并部署到 nginx

    个人也是了百度了挺久的了,花费的时间( 俩个半小时 ) uniapp 的打包首先要先配置,配置好了才能去进行打包,如图所示. 这只是第一步. 注意: 1.运行基础路径最好用 ./ ,如果配置了其他请自 ...