Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@
# {fact [email protected] defects=1}
def pytorch_miss_call_to_eval_noncompliant(model):
import torch
# Noncompliant: miss call to `eval()` after load.
model.load_state_dict(torch.load("model.pth"))
# Noncompliant: miss call to `eval()` after load.
Copy link

@linghuiluo linghuiluo May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss call to eval() before evaluating the model.

classes = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
# {/fact}


# {fact [email protected] defects=0}
def pytorch_miss_call_to_eval_compliant(model):
import torch
model.load_state_dict(torch.load("model.pth"))
classes = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
# Compliant: `eval()` is called after load.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval() is called before evaluating the model.

model.eval()
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
# {/fact}