Classifying documents with existing subject labels
Source:vignettes/articles/hypergat-classification.Rmd
hypergat-classification.RmdA document classifier learns from documents whose subjects have already been assigned. Its usefulness is measured by the subjects it predicts for other documents with known labels. The R8 news corpus has eight existing subject labels, including earnings, acquisitions and trade, and a fixed training and test split. HyperGAT learns from the training articles and classifies the test articles with the same fitted network.
Existing labels
R8 is a subset of Reuters newswire articles with one of eight subject
labels. The corpus
text and label
file come from a fixed revision of the official HyperGAT
implementation. Download both files into the working directory before
running the code. Fitting requires the torch R package and
its runtime.
The tables and figures below retain a fitted reference run with
seed = 1. Package builds use these saved results. The
displayed code reproduces the analysis; numerical results can vary
across torch versions and hardware.
The files retain sentence punctuation and the original subject labels. The training and test articles are kept separate. The test labels are used to evaluate predictions after fitting.
text <- enc2utf8(readLines(con = "R8_corpus.txt", encoding = "latin1",
warn = FALSE))
meta <- read.delim(file = "R8_labels.txt", header = FALSE,
col.names = c("row", "split", "label"),
colClasses = "character", quote = "")
articles <- transform(`_data` = meta,
node = sprintf("R8_%05d", seq_along(text)), text = text)
training <- subset(x = articles, subset = split == "train",
select = c(node, text, label))
testing <- subset(x = articles, subset = split == "test",
select = c(node, text, label))
xtabs(formula = ~ label + split, data = articles) split
label test train
acq 696 1596
crude 121 253
earn 1083 2840
grain 10 41
interest 81 191
money-fx 87 222
ship 36 108
trade 76 250
There are 5501 training articles and 2190 test articles. Earnings accounts for 51.6% of the training set. A classifier that always chooses the most common training subject provides a simple comparison for overall accuracy. Balanced accuracy also measures how well the smaller subjects are recovered.
Fit once
HyperGAT represents each article as a hypergraph. Its nodes are the unique words, and each sentence is a hyperedge containing its words. The network learns word representations and aggregates them through these hyperedges to predict a subject. The default construction uses sentences. Topic hyperedges are optional.
fit <- hg_hypergat(x = training, column = "text", id = "node",
labels = "label", min_count = 5, seed = 1)
print(x = fit)HyperGAT classifier: 5501 documents, 8 classes, 5501 labelled
No held-out evaluation. predict() reuses the fitted model.
The fitted object retains its network and vocabulary. The vocabulary
comes from the known training labels. The default validation split
selects the training epoch; it supplies no estimate of performance on
the test set. For a corpus without a supplied test split,
holdout = 0.2 provides an internal evaluation using a
stratified share of the known labels.
plot(x = fit, type = "history")
Predict independent documents
predict() applies the fitted network to the test
articles. Supplying their existing label column evaluates the
predictions. These labels do not update the network or vocabulary.
Classification (hg_hypergat prediction): 2190 documents, 2190 held out (100% of the labelled)
Held-out accuracy 0.963, balanced accuracy 0.898 (chance 0.125)
class n_test correct recall precision
acq 696 678 0.9741379 0.9854651
crude 121 113 0.9338843 0.8968254
earn 1083 1066 0.9843029 0.9897864
grain 10 9 0.9000000 0.9000000
interest 81 65 0.8024691 0.8552632
money-fx 87 78 0.8965517 0.8297872
ship 36 26 0.7222222 0.7428571
trade 76 74 0.9736842 0.9024390
The accuracy is 96.3% on all 2190 test articles, compared with 49.5% for always choosing earn. Balanced accuracy is 89.8%. The model can score 2188 of the test articles. Articles with no word in the fitted vocabulary retain their rows and count as errors. These results measure recovery of the eight existing subjects in this test set.
plot(x = evaluation)
Read the errors
The confusion plot shows which subjects are confused. Precision and recall give the performance for each subject. The document table returns the text together with its observed and predicted subject, so an error can be inspected directly.
hg_get(x = evaluation, what = "classes")| class | n_test | correct | recall | precision |
|---|---|---|---|---|
| acq | 696 | 678 | 0.9741379 | 0.9854651 |
| crude | 121 | 113 | 0.9338843 | 0.8968254 |
| earn | 1083 | 1066 | 0.9843029 | 0.9897864 |
| grain | 10 | 9 | 0.9000000 | 0.9000000 |
| interest | 81 | 65 | 0.8024691 | 0.8552632 |
| money-fx | 87 | 78 | 0.8965517 | 0.8297872 |
| ship | 36 | 26 | 0.7222222 | 0.7428571 |
| trade | 76 | 74 | 0.9736842 | 0.9024390 |
hg_get(x = evaluation, what = "documents", correct = FALSE, top = 1)| node | label | predicted | score | margin | split | correct | text |
|---|---|---|---|---|---|---|---|
| R8_05513 | money-fx | interest | 0.8790182 | 0.7759423 | test | FALSE | LAWSON SEES NO CHANGE IN U.K. MONETARY POLICY. British |
Conoco and Atlantic Richfield Co
A new article without a label is classified with
predict(object = fit, newdata = new_articles). The output
contains its predicted subject, winning softmax score and margin over
the next subject. The score is a model output rather than a calibrated
probability that the subject is correct. Every scorable article receives
one of the fitted subjects. These labels are appropriate for documents
belonging to this subject system; this evaluation does not establish
accuracy for other corpora or subjects.
Attention as a diagnostic
Attention weights determine internal aggregation. Word attention sums to one over the words of a hyperedge. Edge attention sums to one over the hyperedges containing a word. A word occurring in only one hyperedge therefore assigns that hyperedge a weight of one automatically. The mean of these edge weights can be high because many words are exclusive to a sentence.
The diagnostic table includes the mean uniform weight implied by the same memberships and the fraction of exclusive words. These values make the normalization visible. The illustrated article is a short earnings report with a headline and reported results. Its sentences have distinct word sets, so the two hyperedges and their overlap can be read. It is selected for readability, independently of confidence or correctness.
first_article <- "R8_01282"The hypergraph plot shows the words and the sentence hyperedges that the network consumes for this article.
plot(x = fit, type = "hyperedges", node = first_article, edge_labels = TRUE,
label_size = 3, titles = FALSE)
hg_get(x = fit, what = "hyperedges", node = first_article)| node | label | predicted | hyperedge | kind | text | weight | top_word | n_words | uniform_weight | exclusive_fraction |
|---|---|---|---|---|---|---|---|---|---|---|
| R8_01282 | earn | earn | sentence 2 | sentence | Shr 2-1/5 cts vs nil Net 156,726 vs 11,989 Sales | |||||
| 1,157,883 vs 890,138 | 0.9166800 | nil | 6 | 0.9166667 | 0.8333333 | |||||
| R8_01282 | earn | earn | sentence 1 | sentence | CCR VIDEO 1ST QTR NOV 30 NET | 0.9166533 | qtr | 6 | 0.9166667 | 0.8333333 |
plot(x = fit, type = "attention", node = first_article)
Crosses show the uniform normalization baseline and bars show the mean learned edge attention. These internal weights do not measure a sentence’s contribution to the predicted subject. Classification errors are reviewed from the document text and the evaluation tables.
References
Ding, K., Wang, J., Li, J., Li, D., & Liu, H. (2020). Be more with less: Hypergraph attention networks for inductive text classification. Proceedings of EMNLP 2020, 4927–4936. Paper. The sentence-preserving corpus and existing labels are distributed in the official implementation.