Loss not changing for basic cnn model

joy1: <p>I am trying to revise pytorch. I am trying some examples which I could do before. But somehow now the model’s loss is not changing. It is not about the learning rate or anything. I need urgent help. Anyone who could help me?</p>
<p>import torch<br>
import torch.nn as nn<br>
import numpy as np<br>
from torch.utils.data import Dataset,DataLoader<br>
import torch.optim as optim<br>
import torchvision.transforms as transforms<br>
import torchvision.datasets as datasets<br>
import torch.nn.functional as F</p>
<p>transforms = transforms.Compose([transforms.ToTensor(),transforms.Normalize((1307,),(0.3801))])</p>
<p>train = datasets.MNIST(root=‘.’,train=True,transform=transforms,download=True)<br>
test = datasets.MNIST(root=‘.’,train=False,transform=transforms,download=True)</p>
<p>trainloader = DataLoader(dataset = train, batch_size=60000, shuffle=True)<br>
testloader = DataLoader(dataset= test, batch_size=10000, shuffle=False)</p>
<p>class Model(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Model,self).<strong>init</strong>()<br>
self.cnn1 = nn.Conv2d(1,8,3,1,1)<br>
self.batchnorm1 = nn.BatchNorm2d(8)<br>
self.relu = nn.ReLU()<br>
self.maxpool1 = nn.MaxPool2d(2,2)<br>
self.cnn2 = nn.Conv2d(8,32,3,1,1)<br>
self.batchnorm2 = nn.BatchNorm2d(32)<br>
self.fc1 = nn.Linear(1568,600)<br>
self.fc2 = nn.Linear(600,10)<br>
self.dropout = nn.Dropout(0.5)<br>
self.softmax = nn.Softmax(dim=1)</p>
<pre><code>def forward(self,out):
    out = self.cnn1(out)
    out = self.batchnorm1(out)
    out = self.relu(out)
    out = self.maxpool1(out)
    out = self.cnn2(out)
    out = self.batchnorm2(out)
    out = self.relu(out)
    out = self.maxpool1(out)
    out = out.view(-1,1568)
    out = self.fc1(out)
    out = self.relu(out)
    out = self.dropout(out)
    out = self.softmax(self.fc2(out))
    # breakpoint()
    return out
</code></pre>
<p>model = Model()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(),lr=0.001)<br>
model = model.cuda()<br>
print(f"GPU is available is {torch.cuda.is_available()} statement")</p>
<p>for epochs in range(10):<br>
correct = 0<br>
model.train()<br>
for (x,y) in trainloader:<br>
x = x.float().cuda()<br>
y = y.cuda()<br>
output = model(x)<br>
optimizer.zero_grad()<br>
loss = criterion(output,y)<br>
breakpoint()<br>
loss.backward()<br>
optimizer.step()<br>
with torch.no_grad():<br>
_,predicted = torch.max(output,1)<br>
correct += (predicted==y).sum()<br>
print(f"epoch:{epochs} train_acc {100*correct/len(train)}")</p>
<pre><code>model.eval()
correct=0
for (x,y) in testloader:
    x,y=x.cuda(),y.cuda()
    output = model(x)
    _,predicted = torch.max(output,1)
    correct += (predicted==y).sum()
print(f"epoch:{epochs+1} test_acc {100*correct/len(test)}")
</code></pre>

ptrblck: <p><code>nn.CrossEntropyLoss</code> expects raw logits so remove the <code>nn.Softmax(dim=1)</code> from your model and rerun your training.</p>

joy1: <p>import torch<br>
import torch.nn as nn<br>
import numpy as np<br>
from torch.utils.data import Dataset,DataLoader<br>
import torch.optim as optim<br>
import torchvision.transforms as transforms<br>
import torchvision.datasets as datasets<br>
import torch.nn.functional as F</p>
<p>transforms = transforms.Compose([transforms.ToTensor(),transforms.Normalize((1307,),(0.3081))])</p>
<p>train = datasets.MNIST(root=‘.’,train=True,transform=transforms,download=True)<br>
test = datasets.MNIST(root=‘.’,train=False,transform=transforms,download=True)</p>
<p>trainloader = DataLoader(dataset = train, batch_size=64, shuffle=True)<br>
testloader = DataLoader(dataset= test, batch_size=64, shuffle=False)</p>
<p>class Model(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Model,self).<strong>init</strong>()<br>
self.cnn1 = nn.Conv2d(1,8,3,1,1)<br>
self.batchnorm1 = nn.BatchNorm2d(8)<br>
self.relu = nn.ReLU()<br>
self.maxpool1 = nn.MaxPool2d(2,2)<br>
self.cnn2 = nn.Conv2d(8,32,3,1,1)<br>
self.batchnorm2 = nn.BatchNorm2d(32)<br>
self.fc1 = nn.Linear(1568,600)<br>
self.fc2 = nn.Linear(600,10)<br>
self.dropout = nn.Dropout(0.5)</p>
<pre><code>def forward(self,x):
    out = self.cnn1(x)
    out = self.batchnorm1(out)
    out = self.relu(out)
    out = self.maxpool1(out)
    out = self.cnn2(out)
    out = self.batchnorm2(out)
    out = self.relu(out)
    out = self.maxpool1(out)
    out = out.view(out.size(0), -1)
    out = self.fc1(out)
    out = self.relu(out)
    out = self.dropout(out)
    out = self.fc2(out)
    # breakpoint()
    return out
</code></pre>
<p>model = Model()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(),lr=0.001)<br>
model = model.cuda()<br>
print(f"GPU is available is {torch.cuda.is_available()} statement")</p>
<p>for epochs in range(10):<br>
correct = 0<br>
model.train()<br>
for (x,y) in trainloader:<br>
x = x.float().cuda()<br>
y = y.cuda()<br>
optimizer.zero_grad()<br>
output = model(x)<br>
loss = criterion(output,y)<br>
# breakpoint()<br>
loss.backward()<br>
optimizer.step()<br>
with torch.no_grad():<br>
_,predicted = torch.max(output,1)<br>
correct += (predicted==y).sum().item()<br>
print(f"epoch:{epochs} train_acc {100*correct/60000}")</p>
<pre><code>model.eval()
correct_test=0
for (x,y) in testloader:
    x,y=x.cuda(),y.cuda()
    output = model(x)
    _,predicted = torch.max(output,1)
    correct_test += (predicted==y).sum().item()
print(f"epoch:{epochs+1} test_acc {100*correct_test/10000}")
</code></pre>
<p>I have changed it. Still no luck</p>