torch 迁移学习 固定指定部分权重

文章讲述了在迁移学习中如何控制模型的训练过程,特别是如何通过设置`requires_grad`属性来决定模型的哪些部分参与训练。以ResNet18为例,展示了如何遍历模型的子模块,将前6层及第6层的第一个子模块的权重固定,使其不参与训练。这通常在使用预训练模型并微调特定部分时进行。
摘要由CSDN通过智能技术生成

在迁移学习中,如果想只训练其中一部分,那么需要固定指定部分的权重。
模型中,通过指定参数的requires_grad来确定是否需要训练。
指定requires_gradTrue时,参与训练,否则不参与训练。

现在需要知道模型中都有哪些module, 挑选出参与 / 不参与训练的部分,设置requires_grad即可。

通过model.children()获得child模块,再通过child.parameters()获得参数param,
通过param.requires_grad来设置。

以resnet18为例,先看看都有哪些模块。

child_counter = 0
for child in model.children():
    print(" child", child_counter)
    print(child)
    child_counter += 1
child 0
Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
child 1
BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
child 2
ReLU (inplace)
child 3
MaxPool2d (size=(3, 3), stride=(2, 2), padding=(1, 1), dilation=(1, 1))
child 4
Sequential (
  (0): BasicBlock (
    (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
    (relu): ReLU (inplace)
    (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
  )
  (1): BasicBlock (
    (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
    (relu): ReLU (inplace)
    (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
    (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
  )
)
...

如果有预训练好的模型,先load, 再设置requires_grad.
比如说想把child<6处的模块固定住,不参与训练。
同时还想把child=6处模块的第一个子模块固定住。

from torchvision import datasets, models, transforms

#有checkpoint的要先load
model = models.resnet18(pretrained = False)
checkpoint = torch.load(MODEL_PATH)
model.load_state_dict(checkpoint)

child_counter = 0
for child in model.children():
    if child_counter < 6:
        for param in child.parameters():
            param.requires_grad = False
    elif child_counter == 6:
        children_of_child_counter = 0
        for children_of_child in child.children():
            if children_of_child_counter < 1:
                for param in children_of_child.parameters():
                    param.requires_grad = False
            children_of_child_counter += 1
    child_counter += 1

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值