So, complete example with ignite will be
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchtext.data import Field, BucketIterator
from torchtext.datasets import IMDB

from ignite.engine import Events, Engine, create_supervised_evaluator
from ignite.metrics import CategoricalAccuracy, Loss


## define fields in data
TEXT = Field(lower=True)
LABEL = Field(sequential=False)

## import datasets
train, test = IMDB.splits(TEXT, LABEL, root="../data")
TEXT.build_vocab(train, min_freq=20)
LABEL.build_vocab(train)
train_loader, test_loader = BucketIterator.splits((train, test), 
                                                  batch_size=100,
                                                  repeat=False)

nwords = len(TEXT.vocab)
ntags = 2


class BOW(nn.Module):
    """Simple bag of words model"""
    def __init__(self, nwords, ntags):
        super(BOW, self).__init__()
        self.bias = nn.Parameter(torch.randn(ntags))
        self.embeddings = nn.Embedding(nwords, ntags)

    def forward(self, input):
        output = self.embeddings(input).sum(dim=1)
        output = output + self.bias
        return F.log_softmax(output, dim=1)


model = BOW(nwords, ntags).to("cuda")
opt = optim.SGD(model.parameters(), lr=0.001)
criterion = nn.NLLLoss()


def train_update(engine, batch):
    model.train()
    opt.zero_grad()
    x, y = batch.text.t(), batch.label - 1
    y_pred = model(x)
    loss = criterion(y_pred, y)
    loss.backward()
    opt.step()
    return loss.item()    


trainer = Engine(train_update)


def evaluate_update(engine, batch):
    model.eval()
    with torch.no_grad():
        x, y = batch.text.t(), batch.label - 1
        y_pred = model(x)
        return y_pred, y    


evaluator = Engine(evaluate_update)

metrics={'accuracy': CategoricalAccuracy(), 'loss': Loss(criterion)}

for name, metric in metrics.items():
    metric.attach(evaluator, name)

log_interval = 10    

@trainer.on(Events.ITERATION_COMPLETED)
def log_training_loss(engine):
    iter = (engine.state.iteration - 1) % len(train_loader) + 1
    if iter % log_interval == 0:
        print("Epoch[{}] Iteration[{}/{}] Loss: {:.2f}"
              "".format(engine.state.epoch, iter, len(train_loader), engine.state.output))


@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(engine):
    evaluator.run(train_loader)
    metrics = evaluator.state.metrics
    avg_accuracy = metrics['accuracy']
    avg_nll = metrics['loss']
    print("Training Results - Epoch: {}  Avg accuracy: {:.2f} Avg loss: {:.2f}"
          .format(engine.state.epoch, avg_accuracy, avg_nll))


@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results(engine):
    evaluator.run(test_loader)
    metrics = evaluator.state.metrics
    avg_accuracy = metrics['accuracy']
    avg_nll = metrics['loss']
    print("Validation Results - Epoch: {}  Avg accuracy: {:.2f} Avg loss: {:.2f}"
          .format(engine.state.epoch, avg_accuracy, avg_nll))
    

trainer.run(train_loader, max_epochs=5)