thanks @raufbhat-dev … I changed the last fc layer require_grad to True and the error fixed…  I dont know what is the difference between the code I post here an the actual code but this loss = my_loss(out,y) is correct in that and the problem was because of the last layer…
Is there any way to set last layer’s  require_grad =False and do not face that problem?