NLP datasets¶
This module contains the TextDataset class, which is the main dataset you should use for your NLP tasks. It automatically does the preprocessing steps described in text.transform. It also contains all the functions to quickly get a TextDataBunch ready.
Quickly assemble your data¶
You should get your data in one of the following formats to make the most of the fastai library and use one of the factory methods of one of the TextDataBunch classes:
- raw text files in folders train, valid, test in an ImageNet style,
- a csv where some column(s) gives the label(s) and the following one the associated text,
- a dataframe structured the same way,
- tokens and labels arrays,
- ids, vocabulary (correspondence id to word) and labels.
If you are assembling the data for a language model, you should define your labels as always 0 to respect those formats. The first time you create a DataBunch with one of those functions, your data will be preprocessed automatically. You can save it, so that the next time you call it is almost instantaneous.
Below are the classes that help assembling the raw data in a DataBunch suitable for NLP.
All the texts in the datasets are concatenated and the labels are ignored. Instead, the target is the next word in the sentence.
All the texts are grouped by length (with a bit of randomness for the training set) then padded so that the samples have the same length to get in a batch.
Factory methods (TextDataBunch)¶
All those classes have the following factory methods.
The folders are scanned in path with a train, valid and maybe test folders. Text files in the train and valid folders should be placed in subdirectories according to their classes (not applicable for a language model). tokenizer will be used to parse those texts into tokens.
You can pass a specific vocab for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). kwargs will be split between the TextDataset function and to the class initialization, you can precise there parameters such as max_vocab, chunksize, min_freq, n_labels (see the TextDataset documentation) or bs, bptt and pad_idx (see the sections LM data and classifier data).
This method will look for csv_name, and optionally a test csv file, in path. These will be opened with header, using delimiter. You can specify which are the text_cols and label_cols; by default a single label column is assumed to come before a single text column. If your csv has no header, you must specify these as indices. If you're training a language model and don't have labels, you must specify the text_cols. If there are several text_cols, the texts will be concatenated together with an optional field token. If there are several label_cols, the labels will be assumed to be one-hot encoded and classes will default to label_cols (you can ignore that argument for a language model). label_delim can be used to specify the separator between multiple labels in a column.
You can pass a tokenizer to be used to parse the texts into tokens and/or a specific vocab for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). Otherwise you can specify parameters such as max_vocab, min_freq, chunksize for the Tokenizer and Numericalizer (processors). Other parameters (e.g. bs, val_bs and num_workers, etc.) will be passed to LabelLists.databunch() documentation) (see the LM data and classifier data sections for more info).
This method will use train_df, valid_df and optionally test_df to build the TextDataBunch in path. You can specify text_cols and label_cols; by default a single label column comes before a single text column. If you're training a language model and don't have labels, you must specify the text_cols. If there are several text_cols, the texts will be concatenated together with an optional field token. If there are several label_cols, the labels will be assumed to be one-hot encoded and classes will default to label_cols (you can ignore that argument for a language model).
You can pass a tokenizer to be used to parse the texts into tokens and/or a specific vocab for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). Otherwise you can specify parameters such as max_vocab, min_freq, chunksize for the default Tokenizer and Numericalizer (processors). Other parameters (e.g. bs, val_bs and num_workers, etc.) will be passed to LabelLists.databunch() documentation) (see the LM data and classifier data sections for more info).
This function will create a DataBunch from trn_tok, trn_lbls, val_tok, val_lbls and maybe tst_tok.
You can pass a specific vocab for the numericalization step (if you are building a classifier from a language model you fine-tuned for instance). kwargs will be split between the TextDataset function and to the class initialization, you can precise there parameters such as max_vocab, chunksize, min_freq, n_labels, tok_suff and lbl_suff (see the TextDataset documentation) or bs, bptt and pad_idx (see the sections LM data and classifier data).
Texts are already preprocessed into train_ids, train_lbls, valid_ids, valid_lbls and maybe test_ids. You can specify the corresponding classes if applicable. You must specify a path and the vocab so that the RNNLearner class can later infer the corresponding sizes in the model it will create. kwargs will be passed to the class initialization.
Load and save¶
To avoid losing time preprocessing the text data more than once, you should save and load your TextDataBunch using DataBunch.save and load_data.
Example¶
Untar the IMDB sample dataset if not already done:
path = untar_data(URLs.IMDB_SAMPLE)
path
Since it comes in the form of csv files, we will use the corresponding text_data method. Here is an overview of what your file should look like:
pd.read_csv(path/'texts.csv').head()
And here is a simple way of creating your DataBunch for language modelling or classification.
data_lm = TextLMDataBunch.from_csv(Path(path), 'texts.csv')
data_clas = TextClasDataBunch.from_csv(Path(path), 'texts.csv')
The TextList input classes¶
Behind the scenes, the previous functions will create a training, validation and maybe test TextList that will be tokenized and numericalized (if needed) using PreProcessor.
vocab contains the correspondence between ids and tokens, pad_idx is the id used for padding. You can pass a custom processor in the kwargs to change the defaults for tokenization or numericalization. It should have the following form:
tokenizer = Tokenizer(SpacyTokenizer, 'en')
processor = [TokenizeProcessor(tokenizer=tokenizer), NumericalizeProcessor(max_vocab=30000)]
To use sentencepiece instead of spaCy (requires to install sentencepiece separately) you would pass
processor = SPProcessor()
See below for all the arguments those tokenizers can take.
tokenizer is used on bits of chunksize. If mark_fields=True, add field tokens between each parts of the texts (given when the texts are read in several columns of a dataframe). Depending on include_bos and include_eos, BOS and EOS will be automatically added at the beginning or the end of each text. See more about tokenizers in the transform documentation.
Uses vocab for this (if not None), otherwise create one with max_vocab and min_freq from tokens.
pre_rules and post_rules default to defaults.text_pre_rules and defaults.text_post_rules respectively, vocab_sz defaults to the minimum between max_vocab_sz and one quarter of the number of words in the training texts (rounded to the nearest multiple of 8). model_type is passed to sentencepiece, so can be unigram (default), bpe, char, or word. Other sentencepiece parameters are langm max_sentence_len and char_coverage (default to 1. for European languages and 0.99 for others).
mark_fields=True will add fields tokens between each text columns (if they are in several columns of a dataframe) and depending on include_bos and include_eos, BOS and EOS will be automatically added at the beginning or the end of each text. The sentencepiece model used for tokenization will be saved in path/tmp_dir where path will be given by the data this processor is applied to.
If you already have a trained tokenizer, you can passa long the model and vocab files with sp_model and sp_vocab.
Language Model data¶
A language model is trained to guess what the next word is inside a flow of words. We don't feed it the different texts separately but concatenate them all together in a big array. To create the batches, we split this array into bs chunks of continuous texts. Note that in all NLP tasks, we don't use the usual convention of sequence length being the first dimension so batch size is the first dimension and sequence length is the second. Here you can read the chunks of texts in lines.
path = untar_data(URLs.IMDB_SAMPLE)
data = TextLMDataBunch.from_csv(path, 'texts.csv')
x,y = next(iter(data.train_dl))
example = x[:15,:15].cpu()
texts = pd.DataFrame([data.train_ds.vocab.textify(l).split(' ') for l in example])
texts
This is all done internally when we use TextLMDataBunch, by wrapping the dataset in the following pre-loader before calling a DataLoader.
LanguageModelPreLoader is an internal class used for training a language model. It takes the sentences passed as a jagged array of numericalised sentences in dataset and returns contiguous batches to the pytorch dataloader with batch size bs and a sequence length bptt.
lengthscan be provided for the jagged training data else lengths is calculated internallybackwards=Truewill reverse the sentences.shuffle=True, will shuffle the order of the sentences, at the start of each epoch - except the first
The following description is usefull for understanding the implementation of LanguageModelPreLoader:
idx: instance of CircularIndex that indexes items while taking the following into account 1) shuffle, 2) direction of indexing, 3) wraps around to head (reading forward) or tail (reading backwards) of the ragged array as needed in order to fill the last batch(s)
ro: index of the first rag of each row in the batch to be extract. Returns as index to the next rag to be extracted
ri: Reading forward: index to the first token to be extracted in the current rag (ro). Reading backwards: one position after the last token to be extracted in the rag
overlap: overlap between batches is 1, because we only predict the next token
Classifier data¶
When preparing the data for a classifier, we keep the different texts separate, which poses another challenge for the creation of batches: since they don't all have the same length, we can't easily collate them together in batches. To help with this we use two different techniques:
- padding: each text is padded with the
PADtoken to get all the ones we picked to the same size - sorting the texts (ish): to avoid having together a very long text with a very short one (which would then have a lot of
PADtokens), we regroup the texts by order of length. For the training set, we still add some randomness to avoid showing the same batches at every step of the training.
Here is an example of batch with padding (the padding index is 1, and the padding is applied before the sentences start).
path = untar_data(URLs.IMDB_SAMPLE)
data = TextClasDataBunch.from_csv(path, 'texts.csv')
iter_dl = iter(data.train_dl)
_ = next(iter_dl)
x,y = next(iter_dl)
x[-10:,:20]
This is all done internally when we use TextClasDataBunch, by using the following classes:
This pytorch Sampler is used for the validation and (if applicable) the test set.
This pytorch Sampler is generally used for the training set.
This will collate the samples in batches while adding padding with pad_idx. If pad_first=True, padding is applied at the beginning (before the sentence starts) otherwise it's applied at the end.