Skip to content

Instantly share code, notes, and snippets.

@aravindpai
Created January 27, 2020 13:49
Show Gist options
  • Save aravindpai/574586f16f328d890baa8f5864311412 to your computer and use it in GitHub Desktop.
Save aravindpai/574586f16f328d890baa8f5864311412 to your computer and use it in GitHub Desktop.
def evaluate(model, iterator, criterion):
#initialize every epoch
epoch_loss = 0
epoch_acc = 0
#deactivating dropout layers
model.eval()
#deactivates autograd
with torch.no_grad():
for batch in iterator:
#retrieve text and no. of words
text, text_lengths = batch.text
#convert to 1d tensor
predictions = model(text, text_lengths).squeeze()
#compute loss and accuracy
loss = criterion(predictions, batch.label)
acc = binary_accuracy(predictions, batch.label)
#keep track of loss and accuracy
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment