def evaluate_epoch(self, iterator, criterion): epoch_loss = 0 epoch_acc = 0 self.eval() with torch.no_grad(): for batch in iterator: predictions = self.forward(batch.text, batch.num) loss = criterion(predictions, batch.label) acc = categorical_accuracy(predictions, batch.label) epoch_loss += loss.item() epoch_acc += acc.item() return epoch_loss / len(iterator), epoch_acc / len(iterator)
def train_epoch(self, iterator, optimizer, criterion): epoch_loss = 0 epoch_acc = 0 self.train() for batch in iterator: optimizer.zero_grad() predictions = self.forward(batch.text, batch.num) loss = criterion(predictions, batch.label) acc = categorical_accuracy(predictions, batch.label) loss.backward() optimizer.step() epoch_loss += loss.item() epoch_acc += acc.item() return epoch_loss / len(iterator), epoch_acc / len(iterator)