liveBook · Manning

Evaluation dataset

As explained in Natural Language Processing in Action, Second Edition

A validation set is a held-out portion of data used to compare model configurations during development.

An evaluation dataset is a held-out collection of examples used to assess a model on data it did not train on. A validation set serves this role during development, helping compare configurations and monitor training. A separate test set can be reserved for a later assessment. section 4.2.1 section 9.3.4

The distinction matters because a model can score highly on familiar examples without performing well on new ones. In a toxicity-classification experiment, a linear discriminant analysis model scored about 99.9% on its training data but only 55.4% on held-out comments. section 4.2.1

Listing 4.6 LDA model performance with train/test split
>>> from sklearn.model_selection import train_test_split
>>> X_train, X_test, y_train, y_test = train_test_split(tfidf_docs,\
...     comments.toxic.values, test_size=0.5, random_state=271828)
>>> lda_tfidf = LinearDiscriminantAnalysis(n_components=1)
>>> lda = lda_tfidf.fit(X_train, y_train)       #1
>>> round(float(lda.score(X_train, y_train)), 3)
0.999
>>> round(float(lda.score(X_test, y_test)), 3)
0.554

See the scikit-learn split, model fitting, and scores on training and held-out data. section 4.2.1

Evaluating a toxicity classifier

For a multilabel BERT classifier, split the labeled comments into training and validation sets. The example holds out 20%, producing 127,656 training rows and 31,915 evaluation rows. Train on the first set and evaluate on the second; the reported evaluation includes loss and a ROC AUC score. section 9.3.4

Listing 9.32 Splitting data into training and validation sets
>>> from sklearn.model_selection import train_test_split
>>> random_state=42
>>> labels = ['toxic', 'severe', 'obscene', 'threat', 'insult', 'hate']
>>> X = df[['comment_text']]
>>> y = df[labels]
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.2,
...     random_state=random_state)    #1

Inspect the reproducible split of comment text and six toxicity labels. section 9.3.4

Validation can also guide training. The BERT example passes the evaluation data to the training routine with evaluation during training and early stopping, then evaluates the saved best model. Because that validation set helps select the model, keep a separate test set for a later assessment. section 9.3.4

Listing 9.39 Loading the pretrained model and fine-tuning with early stopping
>>> model = MultiLabelClassificationModel(
...     model_type, model_name, num_labels=len(labels),
...     args=model_args)
>>> model.train_model(
...     train_df=train_df, eval_df=eval_df, acc=roc_auc_score,
...     show_running_loss=False, verbose=False)

See how training receives both training and evaluation data. section 9.3.4

Read scores in context

A name-nationality RNN achieved 57% training accuracy and 29% validation accuracy, compared with 4% for random guesses across 25 categories. The labels are ambiguous: a name can have different nationalities, including across the split. The validation score therefore needs interpretation alongside how the data was labeled and divided. section 8.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Walks through splitting, evaluation metrics, and validation during training, including early stopping.
Examples
Compares toxicity-classifier training and test scores and interprets a name-nationality RNN’s validation accuracy.
What you can do after
Apply it via scikit-learn and Simple Transformers to split data, evaluate a classifier, and monitor training.
How it connects
Connects validation sets with generalization, classification accuracy, ROC AUC, and early stopping.

Also explained in

Compare how 28 books explain evaluation dataset →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.