Replacing a max pooling layer with an average pooling layer on a VGG model
I'm following this article and I try to implement this function:
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

But I get an error on the iteration that says:

ModuleAttributeError: 'VGG' object has no attribute 'layers'...

How can I do this correctly?

The VGG model provided by Torchvision contains three components: the features sub-module, avgpool (the adaptive average pool), and the classifier. You need to be looking into the head of the network, where the convolution and pool layers are located: features.
You can loop over the layers of a nn.Module with named_children(). However there are other ways of going about this. You can use isinstance to determine if the layer is of a particular type.
In this particular model, layers are named by there index. So in order to locate the appropriate layers in the nn.Module and overwrite them, we can convert the names to  ints.
for i, layer in m.features.named_children():
    if isinstance(layer, torch.nn.MaxPool2d):
        m.features[int(i)] = nn.AvgPool2d(kernel_size=2, stride=2, padding=0)


Having setup beforehand:
import torch
import torch.nn as nn
m = models.vgg16()

