torchvision.models中模型编辑的requires_grad

在对torchvision已有模型进行编辑的时候会保存已有训练结果,只针对编辑过的层进行训练,可以通过对requires_grad的赋值实现

import torch
import torchvision
from torch import optim, nn

def InitMode(mode_name):
    if mode_name == 'resnet152':
        return torchvision.models.resnet152(weights=torchvision.models.ResNet152_Weights.DEFAULT)  
    elif mode_name == "resnet50":
        return torchvision.models.resnet50(weights=torchvision.models.ResNet50_Weights.DEFAULT)
    elif mode_name == "vgg16":
        return torchvision.models.vgg16(weights=torchvision.models.VGG16_Weights.DEFAULT)
    else:
        exit()


mymodel = InitMode("vgg16")

# 修改前
for name, param in mymodel.named_parameters():
    print(name, param.requires_grad)

print("=" * 30)

#  修改requires_grad
for param in mymodel.parameters():
    param.requires_grad = False

for name, param in mymodel.named_parameters():
    print(name, param.requires_grad)

print("=" * 30)

mymodel.classifier[6] = nn.Linear(4096, 10)

# 修改后
for name, param in mymodel.named_parameters():
    print(name, param.requires_grad)

exit()

结果如下:

D:\anaconda3\envs\pytorch_gpu\python.exe D:/project/python/pytorch_gpu/test.py
features.0.weight True
features.0.bias True
features.2.weight True
features.2.bias True
features.5.weight True
features.5.bias True
features.7.weight True
features.7.bias True
features.10.weight True
features.10.bias True
features.12.weight True
features.12.bias True
features.14.weight True
features.14.bias True
features.17.weight True
features.17.bias True
features.19.weight True
features.19.bias True
features.21.weight True
features.21.bias True
features.24.weight True
features.24.bias True
features.26.weight True
features.26.bias True
features.28.weight True
features.28.bias True
classifier.0.weight True
classifier.0.bias True
classifier.3.weight True
classifier.3.bias True
classifier.6.weight True
classifier.6.bias True

==============================
features.0.weight False
features.0.bias False
features.2.weight False
features.2.bias False
features.5.weight False
features.5.bias False
features.7.weight False
features.7.bias False
features.10.weight False
features.10.bias False
features.12.weight False
features.12.bias False
features.14.weight False
features.14.bias False
features.17.weight False
features.17.bias False
features.19.weight False
features.19.bias False
features.21.weight False
features.21.bias False
features.24.weight False
features.24.bias False
features.26.weight False
features.26.bias False
features.28.weight False
features.28.bias False
classifier.0.weight False
classifier.0.bias False
classifier.3.weight False
classifier.3.bias False
classifier.6.weight False
classifier.6.bias False

==============================
features.0.weight False
features.0.bias False
features.2.weight False
features.2.bias False
features.5.weight False
features.5.bias False
features.7.weight False
features.7.bias False
features.10.weight False
features.10.bias False
features.12.weight False
features.12.bias False
features.14.weight False
features.14.bias False
features.17.weight False
features.17.bias False
features.19.weight False
features.19.bias False
features.21.weight False
features.21.bias False
features.24.weight False
features.24.bias False
features.26.weight False
features.26.bias False
features.28.weight False
features.28.bias False
classifier.0.weight False
classifier.0.bias False
classifier.3.weight False
classifier.3.bias False
classifier.6.weight True
classifier.6.bias True

进程已结束,退出代码0
 

从结果来看,先对模型的 requires_grad 全部赋值到False,其结果从下载的缺省值True变为Flase。

当对某个层进行编辑后,这个层的requires_grad会自动变为True。

还不清楚是什么原因,记录一下

你可能感兴趣的:(人工智能,深度学习,python)