Lesson 4 Further Research 2, accuracy is not increasing for any epoch

I tried to manually create the 10 digit classification of MNIST images. The accuracy for the first epoch is 57.84%, even after 40 epochs the accuracy is still 57.84%. There must be some bug, I am not able to identify it. Please help me.

`!pip install -Uqq fastbook
from fastbook import *
from fastai.vision.all import *

path = untar_data(URLs.MNIST)
Path.BASE_PATH = path

train_img = []
train_lbl = []
valid_img = []
valid_lbl = []

for count in range(0,10):
for imag in (path/‘training’/str(count)).ls().sorted():
train_img.append(tensor(Image.open(imag)))
train_lbl.append(count)
for imag in (path/‘testing’/str(count)).ls().sorted():
valid_img.append(tensor(Image.open(imag)))
valid_lbl.append(count)

stacked_train_img = [torch.stack(train_img).float()/255]
stacked_valid_img = [torch.stack(valid_img).float()/255]
train_x = torch.cat(stacked_train_img).view(-1,2828)
valid_x = torch.cat(stacked_valid_img).view(-1,28
28)
train_y = tensor(train_lbl).unsqueeze(1)
valid_y = tensor(train_lbl).unsqueeze(1)
len(train_img) , len(train_lbl) , len(valid_img) , len(valid_lbl)

dset = list(zip(train_x, train_y))
valid_dset = list(zip(valid_x, valid_y))

dl = DataLoader(dset, batch_size= 256)
valid_dl = DataLoader(valid_dset, batch_size= 256)
dls = DataLoaders(dl, valid_dl)

def init_params(size, std = 1.0):
return (torch.randn(size) * std).requires_grad_()

def linear2(xb):
z1 = xb@weights + bias
return torch.exp(z1) / torch.exp(z1).sum(dim=1, keepdim=True)

def mnist_loss(predictions, targets):
idx = range(len(targets))
return 1 - torch.sum(predictions[idx,targets])

def train_epoch(dl,model,params,lr):
for xb,yb in dl:
preds = model(xb) # step 2 : predict
loss = mnist_loss(preds,yb) # step 3: calculate loss
loss.backward()
for p in params: # step 4 & 5: calculate gradients and step
p.data -= lr * p.grad.data
p.grad = None

def batch_accuracy(preds,yb):
correct = 0
total = len(yb)
for cnt,pred in enumerate(preds):
if(torch.argmax(pred) == yb[cnt]):
correct += 1
return correct/total * 100

def validate_epoch(model):
accs = tensor([batch_accuracy(model(xb),yb) for xb,yb in valid_dl])
return accs.mean()

def my_learn(model, dl, params, lr, num_of_epoch):
for i in range(0,num_of_epoch):
train_epoch(dl,model,params,lr)
print(i,validate_epoch(model))

weights = init_params((28*28,10))
bias = init_params(10)
my_learn(linear2,dl,(weights,bias),0.1,40)
`

Hi,

My guess without actually running the code and debugging is …

weights = init_params((28*28,10)) <== Weights is Global
bias = init_params(10) <== bias is Global

and you pass weights, bias to your functions…
my_learn(linear2,dl,(weights,bias),0.1,40)

But when you use your model linear2

def linear2(xb):
z1 = xb@weights + bias
return torch.exp(z1) / torch.exp(z1).sum(dim=1, keepdim=True)

Since it’s not getting passed the weights, bias is using the global weights and bias.

Global = Bad! Move every thing into a class = better :slight_smile:

Thank you :smiley: for debugging my code and finding out the mistake.

How should I, move the bias and weights into the class.
And I also do not understand the difference between global weights and class weights. Could you please explain them.