简体   繁体   中英

Custom weight initialisation causing error - pytorch

%reset -f

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch.utils.data as data_utils
import torch.nn as nn
import torch.nn.functional as F

num_epochs = 20

x1 = np.array([0,0])
x2 = np.array([0,1])
x3 = np.array([1,0])
x4 = np.array([1,1])

num_epochs = 200

x = torch.tensor([x1,x2,x3,x4]).float()
y = torch.tensor([0,1,1,0]).long()

train = data_utils.TensorDataset(x,y)
train_loader = data_utils.DataLoader(train , batch_size=2 , shuffle=True)

device = 'cpu'

input_size = 2
hidden_size = 100 
num_classes = 2

learning_rate = .0001

torch.manual_seed(24)

def weights_init(m):
    m.weight.data.normal_(0.0, 1)

class NeuralNet(nn.Module) : 
    def __init__(self, input_size, hidden_size, num_classes) : 
        super(NeuralNet, self).__init__()
        self.fc1 = nn.Linear(input_size , hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size , num_classes)

    def forward(self, x) : 
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

model = NeuralNet(input_size, hidden_size, num_classes).to(device)
model.apply(weights_init)

criterionCE = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for i in range(0 , 1) :

        total_step = len(train_loader)
        for epoch in range(num_epochs) : 
            for i,(images , labels) in enumerate(train_loader) : 
                images = images.to(device)
                labels = labels.to(device)

                outputs = model(images)
                loss = criterionCE(outputs , labels)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

        outputs = model(x)

        print(outputs.data.max(1)[1])

I'm using to initialize the weights :

def weights_init(m):
    m.weight.data.normal_(0.0, 1)

But following error is thrown :

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    533                 return modules[name]
    534         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 535             type(self).__name__, name))
    536 
    537     def __setattr__(self, name, value):

AttributeError: 'ReLU' object has no attribute 'weight'

Is this the correct method to initialize the weights ?

Also, should object be of type nn.Module , not Relu ?

You are trying to set the weights of a weight-free layer (ReLU).

Inside weights_init , you should check the type of layers before initializing weights. For instance:

def weights_init(m):
    if type(m) == nn.Linear:
        m.weight.data.normal_(0.0, 1)

See How to initialize weights in PyTorch? .

In addition to what Fabio mentioned about checking the layer type and ReLU being an activation and not a trainable layer, as it is about initialization, you can do the weight initialization in the __init__ method itself like its done here:

https://github.com/pytorch/vision/blob/master/torchvision/models/vgg.py

def __init__(self, features, num_classes=1000,...):
        ----snip---
    self._initialize_weights()

def _initialize_weights(self):
    if isinstance(m, nn.Linear):
        m.weight.data.normal_(0.0, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM