HiPool: Modeling Long Documents Using Graph Neural Networks
Encoding long sequences in Natural Language Processing (NLP) is a challenging
problem. Though recent pretraining language models achieve satisfying
performances in many NLP tasks, they are still restricted by a pre-defined
maximum length, making them challenging to be extended to longer sequences. So
some recent works utilize hierarchies to model long sequences. However, most of
them apply sequential models for upper hierarchies, suffering from long
dependency issues. In this paper, we alleviate these issues through a
graph-based method. We first chunk the sequence with a fixed length to model
the sentence-level information. We then leverage graphs to model intra- and
cross-sentence correlations with a new attention mechanism. Additionally, due
to limited standard benchmarks for long document classification (LDC), we
propose a new challenging benchmark, totaling six datasets with up to 53k
samples and 4034 average tokens' length. Evaluation shows our model surpasses
competitive baselines by 2.6% in F1 score, and 4.8% on the longest sequence
dataset. Our method is shown to outperform hierarchical sequential models with
better performance and scalability, especially for longer sequences.
Irene Li, Aosong Feng, Dragomir Radev, Rex Ying
# HiPool: Modeling Long Documents Using Graph Neural Networks

###### Abstract

Encoding long sequences in Natural Language Processing (NLP) is a challenging problem. Though recent pretraining language models achieve satisfying performances in many NLP tasks, they are still restricted by a pre-defined maximum length, making them challenging to be extended to longer sequences. So some recent works utilize hierarchies to model long sequences. However, most of them apply sequential models for upper hierarchies, suffering from long dependency issues. In this paper, we alleviate these issues through a graph-based method. We first chunk the sequence with a fixed length to model the sentence-level information. We then leverage graphs to model intra- and cross-sentence correlations with a new attention mechanism. Additionally, due to limited standard benchmarks for long document classification (LDC), we propose a new challenging benchmark, totaling six datasets with up to 53k samples and 4034 average tokens' length. Evaluation shows our model surpasses competitive baselines by 2.6% in F1 score, and 4.8% on the longest sequence dataset. Our method is shown to outperform hierarchical sequential models with better performance and scalability, especially for longer sequences.

## 1 Introduction

Transformer-based models like BERT Vaswani et al. (2017) and RoBERTa Zhuang et al. (2021) have achieved satisfying results in many Natural Language Processing (NLP) tasks thanks to large-scale pretraining Vaswani et al. (2017). However, they usually have a fixed length limit, due to the quadratic complexity of the dense self-attention mechanism, making it challenging to encode long sequences.

One way to solve this problem is to adapt Transformers to accommodate longer inputs and optimize the attention from BERT Feng et al. (2022); Jaszczur et al. (2021). BigBird Zaheer et al. (2020) applies sparse attention that combines random, global, and sliding window attention in a long sequence, reducing the quadratic dependency of full attention to linear. Similarly, Longformer Beltagy et al. (2020) applies an efficient self-attention with dilated windows that scale linearly to the window length. Both models can take up to 4096 input tokens. Though it is possible to train even larger models for longer sequences, they are restricted by a pre-defined maximum length with poor scalability. More importantly, they fail to capture high-level structures, such as relations among sentences or paragraphs, which are essential to improving NLP system performance Zhang et al. (2018); Zhu et al. (2019).

Another way is to apply a hierarchical structure to process adjustable input lengths with chunking representations for scalability on long sequences. Hi-Transformer Wu et al. (2021) encodes both sentence-level and document-level representations using Transformers. ToBERT Pappagari et al. (2019) applies a similar approach that stacks a sentence-level Transformer over a pretrained BERT model. While most of the existing work models upper-level hierarchy using _sequential structures_, such as multiple layers of LSTMs Hochreiter and Schmidhuber (1997) or Transformers, this may still bring the long dependency issue when the sequence gets longer. To alleviate this, we investigate graph modeling as a novel hierarchy for upper levels. Besides, we also consider inter-hierarchy relationships using a new attention mechanism.

Our key insight is to replace the sequence-based model with a hierarchical attentional graph for long documents. We first apply a basic pretrained language model, BERT or RoBERTa, to encode local representation on document chunks with a fixed length. The number of chunks could be extended for longer sequences for better scalability. Different from other works, we apply a graph neural network (GNN) Zhou et al. (2018) to model the
upper-level hierarchy to aggregate local sentence information. This is to alleviate the long dependency issue of the sequential model. Moreover, within such a graph structure, we propose a new heterogeneous attention mechanism to consider intra- and cross- sentence-level correlations.

Our contributions are two-fold: 1) We propose HiPool with multi-level hierarchies for long sequence tasks with a novel inter-hierarchy graph attention structure. Such heterogeneous graph attention is shown to outperform hierarchical sequential models with better performance and scalability, especially for longer sequences; 2) We benchmark the LDC (long document classification) task with better scaled and length-extended datasets. Evaluation shows that HiPool surpasses competitive baselines by 2.6% in F1 score, and 4.8% on the longest sequence dataset. Code is available at [https://github.com/IreneZihuiLi/HiPool](https://github.com/IreneZihuiLi/HiPool).

## 2 Model

We introduce the HiPool (**H**ierarchical **P**ooling) model for long document classification, illustrated in Fig. 1. It consists of an overlapping sequence encoder, a HiPool graph encoder, and a linear layer.

**Overlapping Sequence Encoder**. Given the input document \(S\), we first chunk the document into a number of shorter pieces with a fixed length \(L\), and we set the overlapping window size to be \(L_{olp}\). Overlapping encoding makes it possible for a chunk to carry information from its adjacent chunks but not isolated, differentiating our model from other hierarchical ones. Then each chunk is encoded with a pretrained Transformer model, i.e., BERT or RoBERTa; we choose the CLS token representation as the input to our HiPool layer: \(X=\mathrm{BERT}(S)\).

**HiPool Graph Encoder**. We apply a graph neural network to encode incoming word-level information. Such a model has shown its potential in some NLP tasks (Li et al., 2022, 2021). We construct a graph, defined by \(G(V,E)\), where \(V\) is a set of nodes, and \(E\) is a set of node connections. There are two node types: \(n\)_low-level nodes_ and \(m\)_high-level nodes_, and typically \(m<n\). In our experiment, we set \(m=n/p\), and \(p\geq 0\). The feedforward operation goes from low- to high-level nodes. In layer \(l\), low-level nodes are inputs from the previous layer \(l-1\), while high-level nodes at layer \(l\) are computed based on low-level ones. Moreover, these high-level nodes will be the input to the next layer \(l+1\), becoming the low-level nodes in that layer. We consider \(X\) the low-level nodes in the first HiPool layer, as shown in the figure.

In each HiPool layer, given node representation \(H^{l}\) and adjacency matrix \(A^{l}\) at layer \(l\), the task is to obtain \(H^{l+1}\):

\[H^{l+1}=\mathrm{HiPool}(H^{l},A^{l}). \tag{1}\]

Inspired by DiffPool (Ying et al., 2018), we conduct a clustering method to aggregate information. We assign node clusters with a fixed pattern based on their position. For example, adjacent low-level neighbors should map to the same high-level clustering node. So we first define a clustering adjacency matrix \(A_{self}\in\mathds{R}^{n\times m}\) that maps \(n\) nodes to \(m\) nodes, indicating the relations from low- to high- level nodes, marked as black arrows in the figure. Note that our approach allows overlapping, in which some nodes may belong to two clusters. We set the clustering sliding window to be \(2p\), with a stride to be \(p\). In the figure, we show the case of \(p=2\). We denote interactions between low-level nodes by the adjacency matrix \(A^{l}\),1 and we model it using a chain graph, according to the natural order of the document.2

Footnote 1: We eliminated the subscript of \(A_{low}\) for simplicity, and this also makes Eq. 1 more generalized as other GNNs.

Footnote 2: We tested with a complete graph and BigBird attention structures but found little differences.

Then, the relations between high-level nodes \(A^{l}_{high}\) and their node representations \(H^{l}_{high}\) are computed:

\[\begin{split} A^{l}_{high}&=A^{T}_{self}A^{l}A_{ self},\\ H^{l}_{high}&=A_{self}H^{l}.\end{split} \tag{2}\]

Figure 1: HiPool model illustration. It consists of a sequence encoder, HiPool graph encoder and a linear layer.
Besides, for each high-level node, to strengthen the connections across different clusters, we propose an attention mechanism to obtain cross-sentence information. We propose a new edge type that connects external cluster low-level nodes to each high-level node, and the adjacency matrix is simply \(A_{cross}=1-A_{self}\), marked by green in the figure. We update \(H^{l}_{high}\) as the following:

\[W_{score} =H^{l}_{self}W_{atten}(H^{l})^{T}, \tag{3}\] \[W_{score} =W_{score}A^{T}_{cross},\] \[H^{l}_{high} \gets W_{score}H^{l}+H^{l}_{high},\]

where \(W_{atten}\) is trainable, and \(W_{score}\) is a scoring matrix. We then apply a GNN to obtain \(H^{l+1}\). For example, a graph convolution network (GCN) Kipf and Welling (2016):

\[H^{l+1}=\text{GCN}(H^{l}_{high},A^{l}_{high}). \tag{4}\]

We run our experiments with two layers, and apply a sum aggregator to achieve document embeddings. More HiPool layers are also possible.

**Linear Layer**. Finally, a linear layer is connected and cross-entropy loss is applied during training.

## 3 Experiments

### LDC Benchmark

The LDC benchmark contains six datasets. We first choose four widely-used public datasets. **Hyperpartisan** (HYP) Kiesel et al. (2019) and **20News-Groups** (20NG) Lang (1995) are both news text datasets with different scales. **IMDB**Maas et al. (2011) is a movie review dataset for sentiment classification. **ILDC**Malik et al. (2021) is a large corpus of legal cases annotated with binary court decisions ("accepted"and "rejected").

**Limitation and new datasets**. However, 20News-Groups and IMDB cannot test the limit of models in encoding long documents since the average length of sentence is still relatively small; whereas Hyperpartisan only contains 645 examples and is thus prone to overfitting and not representative. ILDC is large and contains long texts, but it is mainly in the legal domain. Therefore, to enrich evaluation scenario, we select and propose two new benchmarks with longer documents based on an existing large-scale corpus, Amazon product reviews He and McAuley (2016), to conduct long document classification. **Amazon-512** (A-512) contains all reviews that are longer than 512 words from the _Electronics_ category; **Amazon-2048** (A-2048) contains 10,000 randomly sampled reviews that are longer than 2048 words from the _Books_ category. We randomly split 8/1/1 as train/dev/test sets for both datasets. The proposed datasets enable us to draw statistically significant conclusions on model performance as sequence lengths increase, as demonstrated in in Table 1.

### Evaluation

**Hyperparameters**. We list details in Appendix C.

**Baselines**. We select four pretrained models: BERT Devlin et al. (2019), RoBERTa Zhuang et al. (2021), BigBird Zaheer et al. (2020) and Longformer Beltagy et al. (2020). We also compare with a hierarchical Transformer model ToBERT Pappagari et al. (2019). Hi-Transformer Wu et al. (2021) failed to be reproduced as there is no code available. We evaluate two variations of our HiPool method by changing the sequence encoder model: HiPool-BERT and HiPool-RoBERTa. We report the Micro-F1 score in Tab. 2.

**Main Results**. Among the pretrained models, Longformer and BigBird perform better than BERT and RoBERTa. ToBERT can only surpass BERT as it is a hierarchical model that applies BERT as its text encoder. On average, HiPool-BERT improves significantly on BERT by 5.9% and on ToBERT by 3%. Compared to ToBERT, the superior performance of HiPool can be explained by the fact that sentence-level representations in ToBERT fails to capture cross-sentence information. HiPool surpasses baselines on A-512, A-2048 and ILDC that contain longer sequences. Notably, the best model, HiPool-RoBERTa, outperforms BigBird by 4.8% on ILDC. While our model applies a basic pretrained text encoder (the maximum length is 512), it can still surpass larger pretrained language models (i.e., the maximum length is 4096). Although HiPool is worse on HYP and IMDB, we note that HYP only has 65 examples in testing and is prone to overfitting. We further show that even in IMDB,

\begin{table}
\begin{tabular}{l c c c c c c} \hline \hline  & **HYP** & **20NG** & **IMDB** & **A-512** & **A-2048** & **ILDC** \\ \hline Mean & 741.44 & 587.56 & 301.14 & 879.62 & 2,915.03 & 4039.85 \\ Max & 5,368 & 144.592 & 3,152 & 17,988 & 14,120 & 501,091 \\ Min & 21 & 37 & 10 & 512 & 2,048 & 53 \\ Med. & 547 & 360 & 225 & 725 & 2,505 & 2,663 \\
95pt. & 2,030 & 1,229 & 771 & 1,696 & 5,216 & 11,416 \\ \hline Total & 645 & 18,846 & 50,000 & 53,471 & 10,000 & 34,816 \\ Class & 2 & 20 & 2 & 5 & 5 & 2 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Dataset statistics on LDC benchmark. Med. is the median value. 95pt. indicates 95th percentile. Class indicates the number of classes.
HiPool still out-performs the best model for long sequence in Appendix A.

**Hierarchy variations.** To further compare sequential and graph hierarchy, we keep the word encoder and replace the HiPool graph encoder with the following sequential modules: Simple linear summation over low-level nodes; CNN applies a 1-dimension convolution; Trans is to apply a Transformer on top of low-level nodes. Besides, we also look at multiple graph settings: Aggr-mean is to use a mean aggregator to obtain the final document representation; Aggr-std is to use a feature-wise standard deviation aggregator; finally, Aggr-pcp applies Principal Neighbourhood Aggregation (PNA) (Corso et al., 2020). We report results on Amazon-2048 in Tab. 3, as it has the longest sequence on average. An observation is that applying aggregators are better than simpler structures, while keeping a graph is still a better choice. HiPool also considers attention in message passing, so it is doing even better. We also test other variations in Appendix B.

### Ablation Study

**Effect of input length**. To better understand the effect of input length, in Fig. 2, we present an ablation study on the Amazon-2048 and ILDC, and compare three models: BigBird, Longformer, and HiPool. In general, the models benefit from longer input sequences in both datasets. Interestingly, when sequence is larger than 2048, Longformer and Bigbird could not improve and they are limited in maximum lengths. In contrast, as the input sequence gets longer, HiPool steadily improves, showing its ability to encode long documents in a hierarchical structure.

**Model component**. Next, we look at how each component of HiPool affects performance. As shown in Tab. 4, we first take the best model setting, HiPool-RoBERTa, and compare it with the following settings: 1) w/o RoBERTa is to replace RoBERTa with BERT, then the model becomes HiPool-BERT; 2) w/o HiPool is to remove the proposed HiPool module and replace with a simple CNN (Kim, 2014); 3) w/o Overlapping is to

\begin{table}
\begin{tabular}{l|c c c|c} \hline \hline  & **A-512** & **A-2048** & **ILDC** & **Avg.** \\ \hline HiPool-RoBERTa & **0.690** & **0.648** & **0.685** & **0.674** \\ w/o RoBERTa & 0.660 & 0.612 & 0.651 & 0.641 \\ w/o HiPool & 0.601 & 0.578 & 0.620 & 0.600 \\ w/o Overlapping & 0.587 & 0.560 & 0.611 & 0.586 \\ \hline \hline \end{tabular}
\end{table}
Table 4: The effect of sequence encoding layer, HiPool layer and overlapping modules.

\begin{table}
\begin{tabular}{l|c c c c c|c|c} \hline \hline  & **HYP** & **20NG** & **IMDB** & **A-512** & **A-2048** & **ILDC** & **Avg.** \\ \hline BERT & 0.857 & 0.853 & 0.913 & 0.592 & 0.503 & 0.556 & 0.712 \\ RoBERTa & 0.874 & 0.857 & 0.953 & 0.650 & 0.579 & 0.560 & 0.745 \\ BigBird & 0.922 & 0.823 & 0.952 & 0.674 & 0.636 & 0.637 & 0.774 \\ Longformer & **0.938** & 0.863 & **0.957** & 0.673 & 0.612 & 0.562 & 0.768 \\ ToBERT & 0.862 & 0.901 & 0.924 & 0.587 & 0.560 & 0.611 & 0.741 \\ \hline HiPool-BERT & 0.865\(\pm\)0.030 & **0.908\(\pm\)0.005** & 0.931\(\pm\)0.001 & 0.660\(\pm\)0.009 & 0.612\(\pm\)0.011 & 0.651\(\pm\)0.010 & 0.771 \\ HiPool-RoBERTa & 0.886\(\pm\)0.018 & 0.904\(\pm\)0.001 & 0.948\(\pm\)0.001 & **0.690\(\pm\)0.007** & **0.648\(\pm\)0.017** & **0.685\(\pm\)0.018** & **0.794** \\ \hline \hline \end{tabular}
\end{table}
Table 2: Main evaluation results on LDC benchmark. We underscore the best average of baselines, and bold the best overall models.

\begin{table}
\begin{tabular}{l|c c} \hline \hline
**Hierarchy** & **F1** & **Hierarchy** & **F1** \\ \hline _Sequential_ & & _Graph_ & \\ Simple & 0.618 & Aggr-mean & 0.621 \\ CNN & 0.608 & Aggr-std & 0.620 \\ Trans. & 0.560 & Aggr-pma & 0.633 \\  & & HiPool & **0.648** \\ \hline \hline \end{tabular}
\end{table}
Table 3: Comparison of multiple hierarchies.

Figure 2: Ablation study on the input text length. (X-axis shows input length.)
remove the overlapping word encoding. We could see that removing the HiPool Layer leads to a significant drop, indicating the importance of the proposed method. Moreover, the HiPool framework can work with many pretrained language models, as we can see that applying RoBERTa improves BERT. A complete result table can be found in Appendix.

## 4 Conclusion

In this paper, we proposed a hierarchical framework for long document classification. The evaluation shows our model surpasses competitive baselines.

## 5 Limitations and Potential Risks

**Limitations** The model we proposed is specifically for classification, while it is possible to be extended to other NLP tasks by changing the high-level task-specific layer. Besides, in the evaluation, we focused on English corpora. We plan to test on other languages in the future.

**Potential Risks** We make our code publicly available so that everyone can access our code. As the model is a classification model, it does not generate risky content. Users should also notice that the classification predictions may not be perfectly correct.

## 6 Acknowledgements

This paper is dedicated to the memory of Professor Dragomir Radev, who passed away while this paper was being peer-reviewed.
