Object has no attribute 'weight'

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
    elif classname.find('BatchNorm2d') != -1:
        torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
        torch.nn.init.constant_(m.bias.data, 0.0)
class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.discriminator = nn.Sequential(
            nn.Conv2d(in_channels=256, out_channels=128, kernel_size=5, stride=1, padding=2),
            nn.ReLU(True),
            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=5, stride=1, padding=2),
            nn.AvgPool2d(kernel_size=2, stride=1, padding=0),
            nn.Conv2d(in_channels=128, out_channels=1, kernel_size=1, stride=1, padding=0),
            nn.Sigmoid()
        )

    def forward(self, input_feature, alpha):
        x = self.discriminator(input_feature)
        return x

network = Network().cuda()
...
network = network.train()
network.apply(weights_init)

When I apply weights_init()
AttributeError: 'Network' object has no attribute 'weight' error occurs.`
What is the wrong in my usage?

1 Like

Your code seems to work on my machine using 1.0.0.dev20181125.
However, the error is most likely thrown due to a wrong if condition.
If you call apply on your model, the function you are providing will be applied recursively on all children as well as the model itself.
Even though your code works for me, I would recommend to write the checks as:

if isinstance(m, nn.Conv2d):
    ...
# or
if type(m) == nn.Conv2d:
    ...
3 Likes

It’s strange. I have another model and It works fine.
Anyway, I will change the code you recommended. Thanks !

Hello ptrblck,
I’ve just faced the same error and following your idea it was fixed, thanks.