How can I get a conv layer weight In the course of training?

I am a new pytorch user. when I run the mnist example of pytorch, I find the conv weight has not changed! how can I get the changed weight?

 class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)   
        
        self.keep=self.conv1.weight.data # which I added

    def forward(self, x,index):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)

        # which I added to test whether the weight has changed
        if (self.conv1.weight.data==self.keep).all(): print('same!')
             
        return F.log_softmax(x)

I find the print are all “same!”, how can I get the changed weight?

Which optimizer do you use?
I guess you do not apply optim.step(), which updates your weight while training

 class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)   
        
        self.keep=self.conv1.weight.data.clone() # modify here

    def forward(self, x,index):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)

        # which I added to test whether the weight has changed
        if (self.conv1.weight.data==self.keep).all(): print('same!')
        self.keep=self.conv1.weight.data.clone() #  added here
             
        return F.log_softmax(x)
2 Likes

yes, thanke you, it’s work!