Skip to content

Instantly share code, notes, and snippets.

@aravindpai
Created Jan 27, 2020
Embed
What would you like to do?
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