in source/sagemaker/src/package/model/inference.py [0:0]
def calculate_metrics(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
model.eval()
all_predictions = np.array([])
all_labels = np.array([])
with torch.no_grad():
for batch in iterator:
predictions = model(batch.review).squeeze(1)
loss = criterion(predictions, batch.sentiment)
acc = binary_accuracy(predictions, batch.sentiment)
epoch_loss += loss.item()
epoch_acc += acc.item()
all_predictions = np.concatenate(
(all_predictions, torch.sigmoid(predictions).detach().cpu().numpy()), axis=0)
all_labels = np.concatenate(
(all_labels, batch.sentiment.detach().cpu().clone().numpy()), axis=0)
fpr, tpr, thresholds = roc_curve(all_labels, all_predictions, pos_label=1)
prec = precision_score(all_labels, np.round(all_predictions, 0))
rec = recall_score(all_labels, np.round(all_predictions, 0))
return epoch_loss / iterator.iterations, epoch_acc / iterator.iterations, fpr, tpr, prec, rec