zoukankan      html  css  js  c++  java
  • pytorch固定部分参数

    pytorch固定部分参数

    不用梯度

    如果是Variable,则可以初始化时指定

    j = Variable(torch.randn(5,5), requires_grad=True)
    

    但是如果是m = nn.Linear(10,10)是没有requires_grad传入的

    for i in m.parameters():
        i.requires_grad=False
    

    另外一个小技巧就是在nn.Module里,可以在中间插入这个

    for p in self.parameters():
        p.requires_grad=False
        
     
    # eg  前面的参数就是False,而后面的不变
    class Net(nn.Module):
        def __init__(self):
            super(Net, self).__init__()
            self.conv1 = nn.Conv2d(1, 6, 5)
            self.conv2 = nn.Conv2d(6, 16, 5)
    
            for p in self.parameters():
                p.requires_grad=False
    
            self.fc1 = nn.Linear(16 * 5 * 5, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 10)
    
    def freeze(test_net):
        ct = 0
        for child in test_net.children():
            ct += 1
            if ct < 3:
                for param in child.parameters():
                    param.requires_grad = False
    

    过滤

    optimizer.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)
    
  • 相关阅读:
    Django入门
    初识json
    回来了
    python学习
    JavaScript 中获取元素样式
    浏览器检测与特征检测
    DOM 节点的类型及判定
    浏览器的控制台工具
    .htaccess 配置文件的使用
    workLog:07001:补充0829 前
  • 原文地址:https://www.cnblogs.com/icodeworld/p/12025075.html
Copyright © 2011-2022 走看看