# 11th Place Solution: Nested Transformers

Overview
We called our network architecture "Nested Transformers", since it contains two levels of transformers:
Cell transformer: A transformer encoder to encode cell tokens, this can be a typical transformer for NLP.
Notebook transformer: A transformer encoder with cell*cell self-attention to learn the interaction among cells.
![Nested Transformers](assets/17ae91690ac1aad0216dbb1c.png)
Source codes:
[https://github.com/ShinSiangChoong/kaggle_ai4code_nested_transformers](https://github.com/ShinSiangChoong/kaggle_ai4code_nested_transformers)
What Worked
Modified Format of the Input to Cell Transformer
The input is in the following format:
<code><sep><source_tokens>
OR
<mark><sep><source_tokens>
Thanks
[@cabbage972](https://www.kaggle.com/cabbage972)
for verifying the usefulness of this idea. Please refer to this
[comment](https://www.kaggle.com/competitions/AI4Code/discussion/343680#1907307)
.
Cell Features:
The code cell order is known, while the markdown order is unknown, hence a straightforward positional embedding scheme in the notebook transformer won't work. The cell features aim to inform the model about the known code cell positions/order.
We use 2 features for each cell, i.e. cell type (code -> 1, markdown -> 0) and percentile rank among the code cells: If the cell is a markdown cell, this feature = 0
The 2 features are "expanded" by FC layers, such that they are represented as an embedding that has the same dimension with that of the cell token embeddings.
These cell feature embeddings are then concatenated with the token embeddings and passed into the Aggregation Attention Layer together.
Aggregation Attention
We believe different tokens (and the cell features) have different importance to represent the cell. An aggregation attention layer is used to learn the weights of each token embedding (and the cell feature embedding).
The weights are normalized by a Softmax layer, such that the final cell embedding is a weighted average of its token and feature embeddings.
Thanks
[@cabbage972](https://www.kaggle.com/cabbage972)
for verifying the usefulness of this idea. Please refer to this
[comment](https://www.kaggle.com/competitions/AI4Code/discussion/343680#1909812)
.
Double Head Architecture
Pointwise Head: output the percentile ranks of each cell. L1 Loss is applied.
Pairwise Head: For each pair of cells A & B, predict whether cell B is the next cell of cell A. Cross-entropy loss is applied.
We only use the pointwise head for inference, i.e. the cells are ranked by the predicted percentile rank.
The pairwise head is not used during inference, however, in our experiment, inclusion of the pairwise head helps in boosting the validation score by around 0.03.
Code Order Correction
Since our model predicts a percentile rank for each cell (including code cells), the code cell order (which is given) may be violated.
If this happens, we swap the code cells based on the given order.
This idea only marginally boosts the score. The model actually learns the code order pretty well given the cell features.
What Didn't Work
Make use of the pairwise head prediction in a greedy manner while fulfilling the code order constraint, i.e. keep determining the next cell by selecting the cell with the highest prediction score.
Make use of the pairwise head prediction as a distance matrix, then use LKH to solve a Sequential Ordering Problem (TSP with precedence constraints)
Train 2 cell transformer backbones together end to end, rely on the Aggregation Attention to aggregate the token embeddings from the 2 backbones.
Additional Context
The final model is an ensemble of 4 different cell transformer backbones, i.e. CodeBERT, Deberta-large, Deberta-base, and DistilBERT.
For the notebook transformer, we used 6 transformer encoder layers here, each with 8 self-attention heads. This layer "adjusts" the embedding of each cell based on the embeddings of other cells using the self-attention mechanism.
Acknowledgements
We would like to thank the competition host(s) and Kaggle for organizing this interesting competition.
Team Members
[@css919](https://www.kaggle.com/css919)
[@erniechiew](https://www.kaggle.com/erniechiew)
[@hexomiter](https://www.kaggle.com/hexomiter)