Hi,

I am working on the further research part of chapter 4 to build the full MNIST classifier without using the fastai ready to use functions. However even after training the model for 60 epochs, my accuracy doesn’t increase more that 10% (0.10).

Here is my code. Looking for some help/perspective on what I am missing here. Thanks in advance!

```
# function to calculate loss
def mnist_loss(pred, actual):
l = nn.CrossEntropyLoss()
return l(pred, actual.squeeze())
# function to calculate gradient
def calc_grad(xb, yb, model):
pred = model(xb)
loss = mnist_loss(pred, yb)
loss.backward()
return loss
# function to define accuracy
def batch_accuracy(pred, actual):
digit_pred = pred.max(dim=1)[1]
return (digit_pred==actual).float().mean()
#function to train 1 epoch and print average batch loss
def train_epoch(model):
batch_loss = []
for xb,yb in train_dl:
batch_loss.append(calc_grad(xb, yb, model))
opt.step()
opt.zero_grad()
print('Average batch loss: ', tensor(batch_loss).mean())
#Optimizer
class BasicOptim:
def __init__(self,params,lr): self.params,self.lr = list(params),lr
def step(self, *args, **kwargs):
for p in self.params: p.data -= p.grad.data * self.lr
def zero_grad(self, *args, **kwargs):
for p in self.params: p.grad = None
simple_net = nn.Sequential(
nn.Linear(28*28,100),
nn.ReLU(),
nn.Linear(100,30),
nn.ReLU(),
nn.Linear(30,10)
)
opt = BasicOptim(simple_net.parameters(), lr=0.04)
def train_model(model,epochs):
for i in range(epochs):
train_epoch(model)
print('epoch', i, ': ', batch_accuracy(model(valid_x),valid_y))
train_model(simple_net, 60)
```

Note: valid_x, valid_y are the validation tensors. train_dl is the training DataLoader with batch_size 64.