Any trouble with net.eval() mode in PyTorch?

I am running into some issues about net.eval() inconsistency in PyTorch. I’ve trained a model which has batchnorm and dropout, then I’ve evaluated model performance on validation set but every time I am getting a different result. I was wondering if anyone else faced a similar problem. I shared my problem here as well

But hope and believe you guys can help me better understand my problem.

def evaluate(net, valid_dl):
val_true = []
val_pred = []
for i, data in enumerate(valid_dl, 0):
    #get the inputs
    inputs, labels = data

    #wrap them in Variable
    inputs, labels = V(inputs.float()), V(labels)
    outputs = net(inputs)

    #get cross entropy loss and accuracy

#define validation variables
val_pred_concat = V(
val_true_concat = V(
#validation cross entropy loss
val_loss = criterion(val_pred_concat, val_true_concat).data[0]
#validation accuracy
_, class_pred = torch.max(val_pred_concat, 1)
val_acc = sum((class_pred == val_true_concat).data) / len(class_pred)
#confusion matrix
cmat = confusion_matrix(,
return val_loss, val_acc, cmat

Without Batchnorm and Dropoout


Ok it’s solved, a silly mistake. For others who may do the same thing, is that my dropout training argument was hard-coded.

Bad Mistake
x = F.dropout(x, p=0.5, training=True)

Correct way
x = F.dropout(x, p=0.5,

Thanks !


FYI if you use nn.Dropout() it handles this automatically.