2026ML · NLP

In-Context Learning vs. Fine-Tuning for NER

Fine-tuned BERT compared with LLM in-context learning on CoNLL-2003 dataset for named entity recognition.

In-Context Learning vs. Fine-Tuning for NER screenshot

Overview

This research project compares two approaches to Named Entity Recognition (NER) on the English part of the CoNLL-2003 dataset:

  • In-context learning (ICL): a large language model (DeepSeek-V4-Flash, accessed through the OpenRouter API) receives the task description and a few labelled examples directly in the prompt, without any additional training.
  • Fine-tuning: a bert-base-cased model is trained for token classification with Hugging Face Transformers.

The goal was to check whether a general-purpose language model guided only by a prompt can match a model trained specifically for the task. Both approaches assign a BIO label to every token, using four entity classes: persons (PER), organisations (ORG), locations (LOC) and miscellaneous (MISC).

The project was carried out by a team of three as part of a university course at Wrocław University of Science and Technology.

Research questions

  1. How does the number of examples in the prompt affect ICL quality? (k ∈ {0, 1, 2, 4, 8})
  2. How does the strategy for selecting examples affect ICL quality? (random, entity-rich, similar, diverse)
  3. How does reducing the number of entity classes affect predictions, both when a single class is omitted and when only one class is kept? (ICL and BERT)
  4. Which configuration performs best overall?

Method

Fine-tuning

BERT is fine-tuned on the full training set (learning rate 2e-5, batch size 16, 4 epochs) and evaluated on the full test set of 3453 sentences. For the class-reduction experiments, separate models are trained with the tokens of the removed classes relabelled as O.

In-context learning

The model receives a fixed instruction that lists the BIO tags and requires a JSON array of labels of the same length as the input. Labelled sentences from the training set are inserted as examples, chosen by one of four strategies:

  • Random: sentences drawn at random.
  • Entity-rich: sentences with the most entity tokens.
  • Similar: sentences that share the most words with the analysed sentence, selected separately for each query.
  • Diverse: sentences chosen so that every entity class is represented.

Invalid responses are retried, first with a larger token limit and then with reasoning enabled. Because of API costs, the ICL experiments use a subset of 200 test sentences. All configurations are scored with entity-level micro F1 on the original four-class labels.

Results

Number of examples

With randomly chosen examples, adding more of them gives unstable results. A single example lowers the score compared with zero-shot prompting, and even eight examples improve it only slightly.

ICL quality vs. number of examples

Example selection

The selection strategy matters more than the number of examples. With k=4, examples similar to the analysed sentence raise micro F1 from 0.906 to 0.948, which is the best result of all configurations.

ICL quality vs. example selection strategy

Reducing the entity classes

BERT is predictable: removing a class from the training data affects only that class, and the remaining classes keep their scores. In ICL, removing a class from the prompt also changes predictions for the other classes, most visibly for MISC. The model also did not always respect the restricted label list and sometimes produced classes that had been excluded from the prompt.

F1 of the remaining classes when one class is removed

Best configurations

ConfigurationMicro F1
ICL, k=4, similar examples0.948
ICL, k=4, diverse examples0.943
ICL, k=8, random examples0.916
BERT fine-tuning (all classes)0.911

The best ICL configuration outperforms fine-tuned BERT by about 4 percentage points. BERT, however, scores higher on the ORG class than any ICL configuration (0.891 vs. at most 0.847), only the best ICL configuration exceeds its macro F1 of 0.896, and its behaviour is more stable across experiments.

View code on GitHub
Next project Neural Network Visualization →