How can i get the same accuracy in last few lines of epochs

username: Akshita1
cooked: <p>step1)<br>
import torch.nn.functional as F</p>
<p>dropout_value = 0.1</p>
<p>class Net(nn.Module):</p>
<pre><code>def __init__(self):

    super(Net, self).__init__()

    # Input Block

    self.convblock1 = nn.Sequential(

        nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),

        nn.ReLU(),

        nn.BatchNorm2d(16),

        nn.Dropout(dropout_value)

    ) # output_size = 26

    # CONVOLUTION BLOCK 1

    self.convblock2 = nn.Sequential(

        nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),

        nn.ReLU(),

        nn.BatchNorm2d(16),

        nn.Dropout(dropout_value)

    ) # output_size = 24

    # TRANSITION BLOCK 1

    self.convblock3 = nn.Sequential(

        nn.Conv2d(in_channels=16, out_channels=10, kernel_size=(1, 1), padding=0, bias=False),

    ) # output_size = 24

    self.pool1 = nn.MaxPool2d(2, 2) # output_size = 12

    # CONVOLUTION BLOCK 2

    self.convblock4 = nn.Sequential(

        nn.Conv2d(in_channels=10, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),

        nn.ReLU(),            

        nn.BatchNorm2d(16),

        nn.Dropout(dropout_value)

    ) # output_size = 10

    self.convblock5 = nn.Sequential(

        nn.Conv2d(in_channels=16, out_channels=10, kernel_size=(3, 3), padding=0, bias=False),

        nn.ReLU(),            

        nn.BatchNorm2d(10),

        nn.Dropout(dropout_value)

    ) # output_size = 8

    self.convblock6 = nn.Sequential(

        nn.Conv2d(in_channels=10, out_channels=10, kernel_size=(3, 3), padding=0, bias=False),

        nn.ReLU(),            

        nn.BatchNorm2d(10),

        nn.Dropout(dropout_value)

    ) # output_size = 6

    self.convblock7 = nn.Sequential(

        nn.Conv2d(in_channels=10, out_channels=10, kernel_size=(3, 3), padding=1, bias=False),

        nn.ReLU(),            

        nn.BatchNorm2d(10),

        nn.Dropout(dropout_value)

    ) # output_size = 6

    

    # OUTPUT BLOCK

    self.gap = nn.Sequential(

        nn.AvgPool2d(kernel_size=6)

    ) # output_size = 1

    self.convblock8 = nn.Sequential(

        nn.Conv2d(in_channels=10, out_channels=10, kernel_size=(1, 1), padding=0, bias=False),

        # nn.BatchNorm2d(10),

        # nn.ReLU(),

        # nn.Dropout(dropout_value)

    ) 

    self.dropout = nn.Dropout(dropout_value)

def forward(self, x):

    x = self.convblock1(x)

    x = self.convblock2(x)

    x = self.convblock3(x)

    x = self.pool1(x)

    x = self.convblock4(x)

    x = self.convblock5(x)

    x = self.convblock6(x)

    x = self.convblock7(x)

    x = self.gap(x)        

    x = self.convblock8(x)

    x = x.view(-1, 10)

    return F.log_softmax(x, dim=-1)
</code></pre>
<p>step 2)<br>
from tqdm import tqdm</p>
<p>train_losses = []</p>
<p>test_losses = []</p>
<p>train_acc = []</p>
<p>test_acc = []</p>
<p>def train(model, device, train_loader, optimizer, epoch):</p>
<p><span class="hashtag">#liveloss</span> = PlotLosses()</p>
<p>model = model.to(device)</p>
<p>model.train()</p>
<p>pbar = tqdm(train_loader)</p>
<p>correct = 0</p>
<p>processed = 0</p>
<p>for batch_idx, (data, target) in enumerate(pbar):</p>
<pre><code># get samples

data, target = data.to(device), target.to(device)

# Init

optimizer.zero_grad()

# In PyTorch, we need to set the gradients to zero before starting to do backpropragation because PyTorch accumulates the gradients on subsequent backward passes. 

# Because of this, when you start your training loop, ideally you should zero out the gradients so that you do the parameter update correctly.

# Predict

y_pred = model(data)

# Calculate loss

loss = F.nll_loss(y_pred, target)

train_losses.append(loss)

# Backpropagation

loss.backward()

optimizer.step()

# Update pbar-tqdm



pred = y_pred.argmax(dim=1, keepdim=True)  # get the index of the max log-probability

correct += pred.eq(target.view_as(pred)).sum().item()

processed += len(data)

pbar.set_description(desc= f'Loss={loss.item()} Batch_id={batch_idx} Accuracy={100*correct/processed:0.2f}')

train_acc.append(100*correct/processed)
</code></pre>
<p>def test(model, device, test_loader):</p>
<pre><code>model.eval()

test_loss = 0

correct = 0

with torch.no_grad():

    for data, target in test_loader:

        data, target = data.to(device), target.to(device)

        output = model(data)

        test_loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss

        pred = output.argmax(dim=1, keepdim=True)  # get the index of the max log-probability

        correct += pred.eq(target.view_as(pred)).sum().item()

test_loss /= len(test_loader.dataset)

test_losses.append(test_loss)

print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(

    test_loss, correct, len(test_loader.dataset),

    100. * correct / len(test_loader.dataset)))



test_acc.append(100. * correct / len(test_loader.dataset))
</code></pre>
<p>step3)<br>
from torch.optim.lr_scheduler import StepLR</p>
<p>model =  Net().to(device)</p>
<p>optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)</p>
<p>scheduler = StepLR(optimizer, step_size=6, gamma=0.1)</p>
<p>EPOCHS = 15</p>
<p>for epoch in range(EPOCHS):</p>
<pre><code>print("EPOCH:", epoch)

train(model, device, train_loader, optimizer, epoch)

# scheduler.step()

test(model, device, test_loader)</code></pre>

username: ptrblck
cooked: <p>I don’t completely understand the question. Could you describe the issue a bit and what behavior you are seeing now and what you would expect?</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>