Abstract
Traditional sequence labeling models are inherently tied to a fixed set of labels, requiring extensive retraining when task definitions change. We propose a label-agnostic approach that treats sequence labeling as a retrieval task. By learning to match input tokens to similar examples in a labeled database, our model can predict tags for entirely new tasks—such as transitioning from part-of-speech tagging to named-entity recognition—without any parameter updates. We further introduce a dynamic programming-based decoding strategy to encourage the copying of contiguous segments, enhancing both interpretability and accuracy. Our results demonstrate that this nearest-neighbor approach achieves performance comparable to standard fine-tuned models on in-domain tasks while vastly outperforming them in zero-shot transfer scenarios.
Introduction
Sequence labeling tasks like Named Entity Recognition (NER) and Part-of-Speech (POS) tagging are fundamental to natural language processing. However, standard neural architectures are "label-dependent": they learn a mapping to a fixed output space. This lack of flexibility is a significant bottleneck when schemas evolve or when applying a model to a related but differently labeled domain.
We address this by introducing a "label-agnostic" framework. Instead of learning to predict a specific tag, the model learns to identify similar tokens in a reference database. At inference time, the model retrieves the nearest neighbors for each input token and copies their associated labels. This shift from classification to matching allows for immediate adaptation to new label sets simply by swapping the reference database.
Methodology
Token-Level Similarity and Matching
The core of our approach is a matching function powered by BERT-based contextual embeddings. For an input token $x_i$, we compute its embedding and find the most similar tokens $x'_j$ in a labeled database. The similarity is calculated via a dot product. The probability of assigning a label $L$ to $x_i$ is proportional to the sum of similarities to all retrieved tokens that carry label $L$.
To optimize these embeddings for matching, we fine-tune BERT using a latent-variable objective. This training ensures that tokens with the same underlying label (regardless of the label's name) are clustered together in the embedding space, while tokens with different labels are pushed apart.
Contiguous Segment Decoding
Predicting labels independently for each token can lead to "jittery" outputs where the model switches between different neighbors for adjacent words. To enforce structural consistency, we implement a dynamic programming (DP) algorithm for decoding. This algorithm encourages the model to copy contiguous "chunks" from a single neighbor sequence. A hyperparameter $c$ is introduced to penalize the model for every transition between different retrieved segments. Higher values of $c$ result in longer, more coherent copied segments.
Experiments and Datasets
We evaluated our model on several benchmarks, including CoNLL 2003 (NER and Chunking), the Penn Treebank (POS and Universal POS), and OntoNotes. The primary focus was on transfer performance: training on one task (e.g., Chunking) and testing on another (e.g., NER) using only the target task's labeled database as a reference, without retraining the BERT encoder.
Results and Discussion
Performance and Transferability
On standard tasks where the training and testing schemas match, our Nearest Neighbor (NN) model performs nearly as well as a standard fine-tuned BERT classifier. For instance, on NER, the NN model achieved an F1 of 89.94 compared to the baseline's 90.76.
The true strength of the label-agnostic approach is revealed in transfer scenarios. In a "Chunking to NER" transfer test, a standard BERT model—which is locked into the chunking label set—achieved a negligible 8.03 F1. In contrast, our NN model achieved 71.74 F1, demonstrating its ability to generalize the concept of "entity-like" matching across different tasks.
Impact of Segment Penalty
The introduction of the segment penalty $c$ proved crucial. By increasing $c$, we force the model to copy longer spans, which aligns with the linguistic structure of entities and phrases. As shown in the performance plots, increasing $c$ initially improves F1 scores while reducing the average number of segments per sentence, leading to more interpretable and accurate predictions.
Conclusion
By reframing sequence labeling as a nearest-neighbor retrieval problem, we have developed a model that is both flexible and interpretable. The ability to perform zero-shot transfer between disparate labeling tasks marks a significant step toward more generalizable NLP systems. While the approach introduces some computational overhead due to retrieval, the gains in adaptability and the transparency of the "copying" mechanism provide a compelling alternative to traditional label-dependent architectures. Future work will focus on optimizing retrieval speed and exploring more sophisticated "retrieve-and-edit" mechanisms.
Summary updated