Skip to content
maxzqqPublic

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

DynClean

Training-Dynamics-Based Noise Filtering for Distantly-Supervised Named Entity Recognition

DynClean is a research codebase for cleaning the noisy labels produced by distant supervision (DS) on NER datasets. It trains a span-based NER model, records per-sample training dynamics (TD) and Area Under the Margin (AUM) statistics across epochs, and uses those signals to identify and prune mislabeled spans before re-training. The repo also bundles baselines (DS-only, GS, Clean-DS), negative sampling (NS) variants, an iterative self-cleaning loop, and a self-training NER pipeline.

The method targets two failure modes of distant supervision:

  • Incomplete annotations — true entities the knowledge base failed to match (labelled as O).
  • Inaccurate annotations — wrong entity-type assignments produced by gazetteer matching.

Hard / low-confidence non-entity spans surfaced by TD or AUM correlate strongly with these errors, so dropping them before the final training run yields cleaner supervision.


Repository layout

DynClean/
├── run_entity.py              # Main span-based NER trainer (BERT/RoBERTa + negative sampling)
├── iterative_run.py           # Iterative TD-filter-then-retrain loop
├── self_run.py                # Self-training NER variant with TD/AUM filtering
├── dm_run_entity.py           # Data-Map (DM) confidence-based filtering trainer
├── run_self_training_ner.py   # DSST-style self-training NER baseline
├── compute_aum.py             # Computes AUM from saved per-epoch logits
├── compute_different_td.py    # Computes TD metrics (confidence, variability, correctness)
├── td_sample.py               # Samples low-confidence non-entity spans → cleaned dataset
├── td_utilities.py            # TD metric implementations (confidence / variability / correctness)
├── td_stat.py                 # Statistics over TD outputs
├── aum.py                     # AUM calculator (adapted from asappresearch/aum)
├── dynamics.py                # Training-dynamics record dataclasses
├── dataset_gen.py             # Generates filtered datasets from TD rankings
├── cleaning_ds.py             # Drops DS spans whose label disagrees with GS
├── rm_ann_error.py            # Annotation-error stats vs. GS reference
├── compute_different_td.py    # TD metric driver for multi-epoch logs
├── shared/                    # Dataset / label-map / constants
│   ├── const.py
│   └── data_structures.py
├── entity/                    # Span model + dataloader utilities
│   ├── models.py
│   └── utils.py
├── data_processing/           # AUM/DM dataset construction notebooks + helpers
├── data/                      # Per-task data folders (conll03, bc5cdr, twitter, wiki,
│                              #   webpage, onto, onto2, gs_data)
└── TD-DS-NER/                 # Earlier snapshot of the project (notebooks, figures, scripts)

Supported tasks

--task accepts: conll03, bc5cdr, twitter, wiki, webpage, onto, onto2, ontonotes.

Each data/<task>/ directory is expected to contain span-level JSON files:

  • train-ds-samples.json — distantly-supervised training spans
  • train-gs-samples.json — gold-standard reference (used by cleaning / evaluation scripts)
  • dev-samples.json, test-samples.json — evaluation splits
  • Generated variants (*-removed-noise-*.json, *-top-remove-*.json, etc.) are produced by the filtering scripts.

Installation

# Python 3.7–3.8 recommended (matches allennlp 0.9 / torch 1.10)
pip install -r TD-DS-NER/requirements.txt

Core dependencies (see TD-DS-NER/requirements.txt):

torch==1.10.0
transformers==4.9.1
allennlp==0.9.0
overrides==3.1.0
tqdm
requests==2.25.1

A CUDA build of PyTorch is recommended for training:

pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 \
    -f https://download.pytorch.org/whl/torch_stable.html

Pipeline overview

The end-to-end DynClean workflow is three stages:

  1. Train a base model and record training dynamics — run run_entity.py (or dm_run_entity.py) with --dm to dump per-epoch logits to <output_dir>/dy_log/dynamics_epoch_*.jsonl.
  2. Compute TD / AUM signals — compute_different_td.py (TD metrics) or compute_aum.py (AUM) consume those logs and produce per-span scores.
  3. Filter and re-train — td_sample.py / dataset_gen.py drop the lowest-confidence non-entity spans to write a cleaned train-ds-samples-*.json, then re-run run_entity.py with --dm_filter and the chosen --data_mode for the final reported model.

iterative_run.py packages the filter→retrain step into an iterative loop driven by --num_iteration, --pos_rate, and --neg_rate.

--data_mode cheatsheet

Mode What it trains on
DS Raw distantly-supervised labels
GS Gold-standard labels (oracle upper bound)
Clean-DS DS with GS-disagreeing spans removed (oracle filter)
NS-DS DS + negative sampling on non-entity spans
TD-NS-DS DS + NS + TD-based noise filtering
TD-NS DS + NS + TD filtering (no separate DS pass)
TD DS with TD-only filtering

Use --NO_NS to disable negative sampling, --save_ns to persist sampled negative GUIDs, --dm to record training dynamics, and --dm_filter to apply a previously-computed TD/AUM filter at training time.


Example commands

1. Baseline DS training

# CoNLL03 with BERT
python run_entity.py --do_train --do_eval --eval_test \
    --task conll03 --data_dir data/conll03 \
    --model bert-base-cased --output_dir out/conll03 \
    --num_epoch 20 --data_mode DS --NO_NS

# BC5CDR with BioBERT
python run_entity.py --do_train --do_eval --eval_test \
    --task bc5cdr --data_dir data/bc5cdr \
    --model dmis-lab/biobert-base-cased-v1.1 --output_dir out/bc5cdr \
    --num_epoch 5 --data_mode NS-DS --save_ns --NO_NS

2. Record training dynamics

python run_entity.py --do_train --do_eval --eval_test \
    --task conll03 --data_dir data/conll03 \
    --model bert-base-cased --output_dir out/conll03 \
    --num_epoch 20 --data_mode NS-DS --save_ns --dm

This writes out/conll03/dy_log/dynamics_epoch_*.jsonl.

3. Compute AUM / TD metrics from logged dynamics

python compute_aum.py            # AUM scores per sample
python compute_different_td.py   # TD metrics (confidence / variability / correctness)

Note: compute_aum.py, td_sample.py, dataset_gen.py, cleaning_ds.py, and rm_ann_error.py currently contain hard-coded absolute paths (e.g. /home/qi/Projects/NER/ds_ner/...). Edit the paths near the top of each script to match your local layout before running.

4. Filter the dataset and re-train

# Generates train-ds-samples-top-<rate>-hard.json under data/<task>/
python td_sample.py

# Train on the filtered dataset
python run_entity.py --do_train --do_eval --eval_test \
    --task conll03 --data_dir data/conll03 \
    --model bert-base-cased --output_dir out/conll03_clean \
    --num_epoch 20 --data_mode NS-DS --save_ns --dm_filter

5. Iterative TD-based filtering

python iterative_run.py --do_train --do_eval --eval_test \
    --task conll03 --data_dir data/conll03 \
    --model bert-base-cased --output_dir out/conll03_iter \
    --pos_rate 0.02 --neg_rate 0.001 \
    --num_epoch 5 --num_iteration 15 \
    --data_mode NS-DS --dm

6. Self-training NER

python run_self_training_ner.py --do_train --do_eval --eval_test \
    --task conll03 --output self_out/conll03 \
    --model roberta-base --data_mode DSST \
    --num_epoch 20 --self_training_begin_step 900 \
    --self_training_period 1800 --self_training_label_mode soft \
    --self_training_hp_label 5.9

Key CLI flags

Common to run_entity.py, iterative_run.py, self_run.py, dm_run_entity.py:

Flag Default Purpose
--task required Dataset selector (see Supported tasks)
--data_dir required Path to the per-task data folder
--output_dir entity_output Where checkpoints, predictions, and dy_log/ are written
--model bert-base-uncased HF model name or local path
--max_span_length 8 Maximum span width considered
--num_epoch 5 Training epochs
--num_iteration 5 Outer iterations (iterative_run / self_run)
--learning_rate 1e-5 Encoder learning rate
--task_learning_rate 5e-4 Span-classifier head learning rate
--train_batch_size 16
--eval_batch_size 32
--rate 0.95 Negative-sampling rate
--pos_rate 0.02 Per-iteration positive pruning rate
--neg_rate 0.001 Per-iteration negative pruning rate
--dm flag Record training dynamics
--dm_filter flag Apply a precomputed TD/AUM filter during training
--aum flag Use AUM-based filtering branch
--save_ns flag Save negative-sampling GUIDs each iteration
--NO_NS flag Disable negative sampling
--data_mode required One of the modes in the cheatsheet above

Run python <script>.py --help for the full list.


Outputs

Each run typically produces, under --output_dir:

<output_dir>/
├── ent_pred_dev.json / ent_pred_test.json   # span predictions
├── ckpt/                                    # model checkpoints
├── dy_log/                                  # per-epoch logits (when --dm is set)
│   └── dynamics_epoch_<n>.jsonl
├── aum_<epoch>/aum_values.csv               # AUM scores (after compute_aum.py)
└── ns_guids/                                # saved negative-sampling GUIDs (when --save_ns)

Notes and caveats

  • This is research code: several utility scripts (compute_aum.py, td_sample.py, dataset_gen.py, cleaning_ds.py, rm_ann_error.py) contain hard-coded paths and dataset names that must be edited before use.
  • TD-DS-NER/ is an earlier snapshot of the project that includes the original notebooks, figures, and the BC5CDR / WNUT data-generation scripts (gen_bc5cdr.py, wiki_gen.py, etc.). Prefer the top-level scripts for new runs.
  • aum.py is adapted from asappresearch/aum with minor modifications.
  • The span-NER trainer and entity/ module follow the architecture popularised by PURE (Princeton's span-based entity model).

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages