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.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),

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

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

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

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:

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.