Pytorch根据layers的name冻结训练方式

yipeiwu_com6年前Python基础

使用model.named_parameters()可以轻松搞定,

model.cuda()
 
 
# ######################################## Froze some layers to fine-turn the model ########################
for name, param in model.named_parameters(): # 带有参数名的模型的各个层包含的参数遍历
  if 'out' or 'merge' or 'before_regress' in name: # 判断参数名字符串中是否包含某些关键字
    continue
  param.requires_grad = False
# #############################################################################################################
 
 
optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()),
           lr=opt.learning_rate * args.world_size, momentum=0.9, weight_decay=5e-4)

以上这篇Pytorch根据layers的name冻结训练方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python3标准库httpclient的使用详解

如下所示: import http.client, urllib.parse import http.client, urllib.parse import random USER...

儿童学习python的一些小技巧

以下是一些Python实用技巧和工具,希望能对大家有所帮助。 交换变量 x = 6 y = 5 x, y = y, x print x >>> 5 print...

实例讲解Python中浮点型的基本内容

1.浮点数的介绍 float(浮点型)是Python基本数据类型中的一种,Python的浮点数类似数学中的小数和C语言中的double类型; 2.浮点型的运算 浮点数和整数在计算机内部存...

python开发之str.format()用法实例分析

本文实例分析了python开发之str.format()用法。分享给大家供大家参考,具体如下: 格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看p...

Python3+django2.0+apache2+ubuntu14部署网站上线的方法

Python3+django2.0+apache2+ubuntu14部署网站上线的方法

自己尝试在本地搭建了 Django 项目后,想部署到自己云服务器上,经常多次尝试和多次踩坑(捂脸),总结如下: 环境:ubuntu14, django2.0, apache2。 1.首先...