当前位置:首页 » python教程 » 正文

Pytorch 实现冻结指定卷积层的参数

看: 912次  时间:2020-12-16  分类 : python教程

python代码

    for i, para in enumerate(self._net.module.features.parameters()):
      if i < 16:
        para.requires_grad = False
      else:
        para.requires_grad = True
    # Solver.
    # self._solver = torch.optim.SGD(
    #   self._net.parameters(), lr=self._options['base_lr'],
    #   momentum=0.9, weight_decay=self._options['weight_decay'])
    self._solver = torch.optim.SGD(
      self._net.module.parameters(), lr=self._options['base_lr'],
      momentum=0.9, weight_decay=self._options['weight_decay'])

分析

通过for循环将需要冻结的layer的requires_grad属性设置为False

以上这篇Pytorch 实现冻结指定卷积层的参数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!