Is it possible to use ignite with torchtext similar to the MNIST example here? In this example, once you have constructed your DataLoader and model, the code is essentially
In this case, as far as I can tell, you can’t pass train_iter to trainer as written above because each batch from train_iter is not a tuple as is in the MNIST case. Is there a way to accommodate this difference with ignite?
It seems to me that the only option is to make use of the custom _update and _inference functions as outlined here, but that requires about as much code as not using ignite at all.
@jacobcvt12 I’m not very familiar with nlp tasks and torchtext, however I’ll try to explain the way we would like to use ignite. As this library is under active development, currently version 0.1.0, we can take a look how to adapt ignite to cover nlp problems too. As always PR are welcome.
At first, ignite offers modularity of the training loop and flexibilty to interact at different steps during the training. In addition, various handlers (ModelCheckpoint, EarlyStopping, metrics computation) are provided out of box to simplify user code.
Concerning the trainer (an instance of Engine), in general case we need to code _update function in order to work with a particular task. Helper functions as create_supervised_trainer are handy for vanilla image classification tasks and other similar task where batch is (batch_x, batch_y), single loss function etc.
Later I’ll take a look at this example and could provide a port with ignite to better estimate usefulness of ignite vs code as without ignite
Thanks for the reply @vfdev-5. I put together some code to show what a batch looks like in torchtext.
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchtext.data import Field, BucketIterator
from torchtext.datasets import IMDB
# load IMDB reviews and ratings data
# (built into torchtext similar to MNIST with torchvision)
## 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_iter, test_iter = BucketIterator.splits((train, test),
batch_size=100,
repeat=False)
# show what a batch looks like
for batch in train_iter:
break
data, target = batch.text.t(), batch.label - 1
At the very bottom of the code, I grab a batch from the iterator. As opposed to the batch in MNIST, this is not a tuple, and instead there are fields within the batch that need to be extracted.
Thanks, I see better this example and the problem you are facing. At the moment, we can not just use create_supervised_trainer with a custom prepare_batch function without a hack like replacing it by a custom one like :
ignite.engine._prepare_batch = _foo_prep_batch
Could you please provide a basic model and loss function for this example to see whether it is only prepare_batch that you need to change ?
In general case we suggest to define processing_function (as _update in create_supervised_trainer) for Engine, however we were already asked several times to have such possibility to reuse create_supervised_trainer with a custom _prepare_batch. Let us see if we could find a flexible solution…