concept training pipeline in category nlp

appears as: training pipeline, The training pipeline, Training pipeline, training pipelines
Real-World Natural Language Processing

This is an excerpt from Manning's book Real-World Natural Language Processing.

Figure 2.3: train/validation/test split and the training pipeline

The training pipeline completes when the Trainer instance is created and invoked with train(). You pass all the ingredients that you need for training—the model, optimizer, iterator, datasets, and a bunch of hyper-parameters.

4.6.3 Building the training pipeline

Once you build the dataset reader, the rest of the training pipeline looks very similar to that of the sentiment analyzer. In fact, we can reuse the LstmClassifier class we defined previously without any modification. The entire training pipeline is shown in listing 4.4. You can access the Google Colab notebook of the entire code from here: http://realworldnlpbook.com/ch4.html#langdetect

Listing 4.4: Training pipeline for the language detector
import torch
import torch.optim as optim
from allennlp.data.iterators import BucketIterator
from allennlp.modules.seq2vec_encoders import PytorchSeq2VecWrapper
from allennlp.modules.text_field_embedders import BasicTextFieldEmbedder
from allennlp.modules.token_embedders import Embedding
from allennlp.training.trainer import Trainer
 
from examples.sentiment.sst_classifier import LstmClassifier
 
EMBEDDING_DIM = 16
HIDDEN_DIM = 16
 
reader = TatoebaSentenceReader()
train_set = reader.read('data/mt/sentences.top10langs.train.tsv')
dev_set = reader.read('data/mt/sentences.top10langs.dev.tsv')
 
vocab = Vocabulary.from_instances(train_set,
                                  min_count={'tokens': 3})
token_embedding = Embedding(num_embeddings=vocab.get_vocab_size('tokens'),
                            embedding_dim=EMBEDDING_DIM)
word_embeddings = BasicTextFieldEmbedder({"tokens": token_embedding})
encoder = PytorchSeq2VecWrapper(
    torch.nn.LSTM(EMBEDDING_DIM, HIDDEN_DIM, batch_first=True))
 
model = LstmClassifier(word_embeddings,
                       encoder,
                       vocab,
                       positive_label='eng')
 
optimizer = optim.Adam(model.parameters())
 
iterator = BucketIterator(batch_size=32,
                          sorting_keys=[("tokens", "num_tokens")])
 
iterator.index_with(vocab)
 
trainer = Trainer(model=model,
                  optimizer=optimizer,
                  iterator=iterator,
                  train_dataset=train_set,
                  validation_dataset=dev_set,
                  num_epochs=10)
 
trainer.train()