torch.nn.Module.requires_grad_(requires_grad=True)

参考链接: torch.nn.Module.requires_grad_(requires_grad=True)

在这里插入图片描述
原文及翻译:

requires_grad_(requires_grad=True)
方法: requires_grad_(requires_grad=True)
    Change if autograd should record operations on parameters in this module.
    在当前这个网络模块中对自动梯度机制进行修改,设置是否需要对模块中参数的
    操作进行记录.
    This method sets the parameters’ requires_grad attributes in-place.
    该方法对参数的requires_grad属性进行原地(in-place)修改.

    This method is helpful for freezing part of the module for finetuning or training parts of a model individually (e.g., GAN training).
    这个方法很有用,尤其是在冻结模块的部分结构进行微调的时候或者在单独训练模
    型的各个部分的时候,在这些情形下这个方法很有用.
    
    Parameters  参数
        requires_grad (bool) – whether autograd should record 
        operations on parameters in this module. Default: True.
        requires_grad (布尔类型) – 该变量指明自动梯度是否需要记录当前
        模块的参数的操作.默认值是True.

    Returns  函数返回
        self  自身self
        
    Return type  返回类型
        Module  模块类型Module

PyTorch源代码:

在这里插入图片描述

代码实验展示:

import torch 
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.conv1=torch.nn.Sequential(  # 输入torch.Size([64, 1, 28, 28])
                torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
                torch.nn.ReLU(),  # 输出torch.Size([64, 64, 28, 28])
                torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),  # 输出torch.Size([64, 128, 28, 28])
                torch.nn.ReLU(),
                torch.nn.MaxPool2d(stride=2,kernel_size=2)  # 输出torch.Size([64, 128, 14, 14])
        )

        self.dense=torch.nn.Sequential(  # 输入torch.Size([64, 14*14*128])
                    torch.nn.Linear(14*14*128,1024),  # 输出torch.Size([64, 1024])
                    torch.nn.ReLU(),
                    torch.nn.Dropout(p=0.5),
                    torch.nn.Linear(1024,10)  # 输出torch.Size([64, 10])        
        )
        self.layer4cxq1 = torch.nn.Conv2d(2,33,4,4)
        self.layer4cxq2 = torch.nn.ReLU()
        self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)
        self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)
        self.layer4cxq5 = torch.nn.Dropout(p=0.8)
        self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))
        self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0]))    
        self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))
        self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))

    def forward(self,x):  # torch.Size([64, 1, 28, 28])
        x = self.conv1(x)  # 输出torch.Size([64, 128, 14, 14])
        x = x.view(-1,14*14*128)  # torch.Size([64, 14*14*128])
        x = self.dense(x)  # 输出torch.Size([64, 10])
        return x

print('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)

model = Model() #.cuda()

print('调用方法requires_grad_(False)之前'.center(100,'-'))
print(model.layer4cxq1.weight.requires_grad)


model.requires_grad_(False)
print('调用方法requires_grad_(False)之后'.center(100,'-'))
print(model.layer4cxq1.weight.requires_grad)


model.requires_grad_()
print('调用方法requires_grad_()之后'.center(100,'-'))
print(model.layer4cxq1.weight.requires_grad)

控制台输出结果:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

加载个人及系统配置文件用了 943 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>  & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '50225' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test2.py'
cuda(GPU)是否可用: True
torch的版本: 1.2.0+cu92
------------------------------------调用方法requires_grad_(False)之前-------------------------------------
True
------------------------------------调用方法requires_grad_(False)之后-------------------------------------
False
---------------------------------------调用方法requires_grad_()之后---------------------------------------
True
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> 
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值