Deep learning 在VGG模型上用平均池层替换最大池层
我遵循并尝试实现此功能:Deep learning 在VGG模型上用平均池层替换最大池层,deep-learning,pytorch,Deep Learning,Pytorch,我遵循并尝试实现此功能: def replace_max_pooling(model): ''' The function replaces max pooling layers with average pooling layers with the following properties: kernel_size=2, stride=2, padding=0. ''' for layer in model.layers: if layer is max pooling:
def replace_max_pooling(model):
'''
The function replaces max pooling layers with average pooling layers with
the following properties: kernel_size=2, stride=2, padding=0.
'''
for layer in model.layers:
if layer is max pooling:
replace
但我在迭代中得到一个错误,它说:
ModuleAttributeError:“VGG”对象没有属性“layers”
如何正确执行此操作?Torchvision提供的VGG模型包含三个组件:
功能
子模块、avgpool
(自适应平均池)和分类器
。您需要查看网络的头部,即卷积层和池层所在的位置:功能
您可以使用在nn.模块的层上循环。然而,还有其他方法可以做到这一点。可以使用来确定图层是否为特定类型
在这个特定的模型中,层由索引命名。因此,为了在nn.Module
中找到合适的层并覆盖它们,我们可以将名称转换为int
事先设置好:
import torch
import torch.nn as nn
m = models.vgg16()
你试过了吗?我没有试过,因为我是新手,不知道如何开始。。。如何在层上执行迭代并替换它们?
import torch
import torch.nn as nn
m = models.vgg16()