Hi Taher!



 Taher_Anjary:

But then accuracy doesn’t change.


The short answer is that this line:


        correct = (y_pred == labels).sum().item()



is a mistake because it is performing an exact-equality test on
floating-point numbers.  (In general, doing so is a programming
bug except in certain special circumstances.)
(Note, this doesn’t affect your loss function, so your training
could be working.)
You don’t mention what sort of problem you’re working on or
what your data or model looks like, so we can’t really proceed
without some speculation.  But if speculation is what you need,
you’ve come to the right place!


    root_dir = 'C:\\Users\\t.anjary\\Desktop\\ML\\ML HW3\\images'



I speculate that you are working with images.  Based on this, I
speculate that you are working on an object classification or
object detection problem.


    model = FNet().float() #.cuda()



There is something floating around out there called “FNet” that is
used for face detection.  So maybe you are working on object
detection.


    criterion = torch.nn.MSELoss(reduction='sum')



Your loss could be the mean-squared-error between the predicted
locations of objects detected by your object detector, and their
known locations as given in your annotated dataset.  This could
make sense.


        labels = labels.float() #.cuda()
        y_pred = model(data)
        
        #loss
        loss = criterion(y_pred, labels)



Based on this, both your y_pred and labels are floats.


        correct = (y_pred == labels).sum().item()
        accuracy = 100 * correct / total



This is a problem.  Here you are performing an exact-equality test
of floating-point numbers.  Doing so is (almost) always a mistake.
Even if your labels are nicely discrete values (say, pixel locations)
such as 1.00 or 503.00, for which an exact-equality test could
make sense, your y_pred are not.  So the exact-equality test will
almost always fail, and the accuracy you calculate will always be
very low.
(Note, if you try to force your y_pred to be discrete, for example,
by rounding them to the nearest integral pixel location, you will
have performed a non-differentiable operation on them, so you
won’t be able to back-propagate usefully.)
As to whether the rest of your code makes sense, it’s hard to
say without knowing what you are trying to do.
Good luck.
K. Frank