Pytorch neural network giving the same output always

Hi All,

I have defined a neural network class like below
class MLP(nn.Module):

def __init__(self,n_classes=5):
    super(MLP,self).__init__()
    self.fc1=nn.Linear(784,256)
    self.fc2=nn.Linear(256,64)
    self.fc3=nn.Linear(64,16)
    
    self.clf=nn.Linear(16,n_classes)
    
def forward(self,x):
    x=x.view(-1,784)
    x=F.relu(self.fc1(x))
    x=F.relu(self.fc2(x))
    x=F.relu(self.fc3(x))
    out=self.clf(x)
    
    return out

I have trained this model and stored in a location.

Now I am trying to predict the class for a new image.

model_MLP=MLP(5)
model_MLP.load_state_dict(torch.load("<model.pt>"))

x_data contains the 28x28 pixel image
images = x_data.float()
output=model_MLP(images)

The output class I get for the first time after training is the same output that I get always if I try with other images. I see that the output values don’t change at all for the new images I provide as input to the class.

How can I resolve this issue?

Regards.
Philip

There is nothing wrong with the code. Can you provide the exact inference code you are using. Along with output values.

I have exactly same issue. Solution plss