| Name | Size | Uploaded | Xet hash |
|---|---|---|---|
| Assignment 2-1.pdf | 372 kB xet | 8ed2cab9 | |
| Assignment_2_HANDIN.ipynb | 138 kB xet | 8c7d788c | |
| NLP_CA2_report.pdf | 223 kB xet | 13eebe44 | |
| README.md | 19.7 kB xet | e7cf4df5 |
Neural Conditional Random Fields for Part-of-Speech Tagging
Course: Natural Language Processing
Institution: ETH Zürich
Assignment: Assignment 2 - Structured Prediction with Neural CRFs
Academic Term: Autumn 2022
Abstract
This project implements a Neural Conditional Random Field (CRF) model for Part-of-Speech (POS) tagging, combining deep learning representations from BERT with structured prediction techniques. The system leverages bidirectional LSTM layers to process contextualized embeddings and models sequential dependencies through learned transition weights. Multiple inference algorithms including forward-backward, Viterbi, and Dijkstra's algorithm are implemented and compared for both training and decoding. The model achieves competitive accuracy on the Universal Dependencies POS tagging benchmark while exploring entropy regularization techniques for improved generalization.
Keywords: Conditional Random Fields, Part-of-Speech Tagging, BERT, Structured Prediction, Viterbi Algorithm, Entropy Regularization
I. Introduction
A. Motivation
Part-of-Speech tagging is a fundamental task in Natural Language Processing that assigns grammatical categories (nouns, verbs, adjectives, etc.) to words in a sentence. While modern neural approaches using transformers achieve high accuracy, they often ignore sequential dependencies between tags. Conditional Random Fields provide a principled framework for modeling these dependencies while avoiding the label bias problem inherent in local classifiers [1].
B. Problem Formulation
Given a sentence $W = (w_1, w_2, ..., w_n)$, predict the most likely tag sequence $T^* = (t_1, t_2, ..., t_n)$ where each $t_i \in \mathcal{T}$ (tag vocabulary).
Objective: Maximize the conditional probability:
Where:
- $\psi(t_i, w_i)$ = emission score (word-tag compatibility)
- $\phi(t_i, t_{i+1})$ = transition score (tag-tag compatibility)
- $Z(W)$ = partition function (normalization constant)
C. Key Challenges
- Efficient Inference: Computing partition function over exponentially many tag sequences
- Learning: Optimizing structured loss with gradient-based methods
- Feature Representation: Extracting meaningful features for rare words
- Scalability: Handling variable-length sequences efficiently
II. System Architecture
A. Model Overview
┌──────────────────────────────────────────────────┐
│ Input Sentence (tokens) │
└──────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────┐
│ BERT Embeddings │
│ (Contextualized) │
└─────────┬───────────┘
│
▼
┌─────────────────────┐
│ BiLSTM Layer │
│ (Hidden=128) │
└─────────┬───────────┘
│
┌────────┴────────┐
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ Emission │ │Transition│
│ Scores │ │ Matrix │
└────┬─────┘ └────┬─────┘
│ │
└────────┬────────┘
│
▼
┌─────────────────┐
│ CRF Layer │
│ (Viterbi/FB) │
└─────────┬────────┘
│
▼
┌─────────────────┐
│ Tag Sequence │
└──────────────────┘
B. Neural Components
1. Embedding Layer (BERT)
Model: bert-base-uncased
Configuration:
- Hidden size: 768
- Layers: 12
- Attention heads: 12
- Vocabulary: 30,522 tokens
Preprocessing:
tokens = [CLS] + word_tokens[:max_len-2] + [SEP]
embeddings = BERT(tokens) # Shape: (batch, seq_len, 768)
2. BiLSTM Layer
Architecture:
LSTM(
input_dim=768, # BERT hidden size
hidden_dim=128, # Bidirectional → 64 per direction
bidirectional=True,
batch_first=True
)
Purpose: Captures additional sequential context beyond BERT's attention
3. Emission Layer
Linear projection:
Linear(hidden_dim=128, output_dim=|T|)
Produces emission scores $\psi(w_i, t_j) = \text{emission}_{i,j}$ for each word-tag pair.
4. Transition Matrix
Learned parameters: $\phi \in \mathbb{R}^{|T| \times |T|}$
Models transition probabilities between consecutive tags:
- $\phi_{i,j}$ = score for transitioning from tag $i$ to tag $j$
- Includes special transitions for BOT (beginning of tag) and EOT (end of tag)
C. CRF Layer
The CRF layer combines emissions and transitions to model structured dependencies.
Score Function:
Where $t_0 = \text{BOT}$ (beginning of tag marker).
III. Inference Algorithms
A. Forward-Backward Algorithm
Purpose: Compute partition function $Z(W)$ and marginal probabilities
Forward Pass
Recursion:
Base case: $\alpha_0(t) = \phi(\text{BOT}, t)$
Partition function: $Z(W) = \log \sum_t \exp(\alpha_n(t))$
Backward Pass
Recursion:
Base case: $\beta_n(t) = 0$ for all $t$
Complexity: $O(n \cdot |T|^2)$
Implementation:
def backward_log_Z(self, W, emissions):
"""
Vectorized implementation over batch dimension
Uses log-sum-exp for numerical stability
"""
gamma = torch.zeros(batch_size, T_size)
for n in range(seq_len-1, 0, -1):
gamma = torch.logsumexp(
transitions + emissions[:, n, :] + gamma,
dim=2
)
log_Z = torch.logsumexp(
transitions[BOT_idx] + emissions[:, 0, :] + gamma,
dim=1
)
return log_Z
B. Viterbi Algorithm
Purpose: Find the most likely tag sequence $T^* = \arg\max_T P(T|W)$
Dynamic Programming:
Backpointers: Store $\text{argmax}$ at each step for sequence reconstruction
Decoding:
def backward_viterbi_log(self, W, emissions):
gamma = torch.zeros(T_size)
backpointers = torch.zeros(T_size, seq_len)
for n in range(seq_len-1, 0, -1):
scores = transitions + emissions[n, :] + gamma
gamma, backpointers[:, n] = torch.max(scores, dim=1)
return gamma, backpointers
Complexity: $O(n \cdot |T|^2)$
C. Dijkstra's Algorithm (Best-First Search)
Alternative to Viterbi: Explores high-probability paths first
Algorithm:
- Initialize priority queue with (score=0, position=0, tag=BOT)
- Pop highest-scoring state
- Expand neighbors: add (score + transition + emission, pos+1, tag')
- Terminate when end of sequence reached
Advantages:
- Early stopping possible
- Can incorporate beam search naturally
- Probabilistic interpretation when normalized by $Z(W)$
Complexity: $O(n \cdot |T|^2 \log(n \cdot |T|))$ worst case, but often faster in practice
IV. Training
A. Loss Function
Negative Log-Likelihood:
Computation:
def loss(self, T, W):
emissions = calculate_emissions(W)
# Score of gold sequence
scores = self.score(emissions, W, T)
# Partition function
log_Z = self.backward_log_Z(W, emissions)
# Mean loss over batch
return torch.mean(log_Z - scores)
B. Entropy Regularization
To improve generalization and prevent overconfident predictions, we add entropy regularization:
Entropy Computation:
Implementation:
def backward_entropy(self, W, emissions):
"""
Computes unnormalized entropy using backward algorithm
"""
beta_1 = torch.zeros(batch_size, T_size) # Log probabilities
beta_2 = torch.zeros(batch_size, T_size) # Entropy terms
for n in range(seq_len-1, 0, -1):
score = transitions + emissions[:, n, :]
# Update entropy accumulator
beta_2 = torch.sum(
torch.exp(score) * (beta_2 - score * torch.exp(beta_1)),
dim=2
)
# Update log probabilities
beta_1 = torch.logsumexp(score + beta_1, dim=2)
return torch.sum(torch.exp(score_bot) * (beta_2 - score_bot * torch.exp(beta_1)))
Hyperparameter: $\beta \in {0.1, 1.0, 10.0}$ controls regularization strength
C. Optimization
Optimizer: Adam
Learning Rate: $2 \times 10^{-5}$
Batch Size: 32
Epochs: 3
Gradient Clipping: Applied to prevent exploding gradients
Training Loop:
for epoch in range(EPOCHS):
for batch in train_dataloader:
W, T = batch
# Forward pass
loss = crf.loss(T, W)
# Backward pass
loss.backward()
# Update parameters
optimizer.step()
optimizer.zero_grad()
V. Experimental Setup
A. Dataset
Universal Dependencies POS Tagging (UDPOS)
Statistics:
- Training sentences: 12,543
- Validation sentences: 2,002
- Test sentences: 2,077
- Tag set size: 17 (UPOS tags)
- Average sentence length: 19.4 tokens
Tag Set:
ADJ, ADP, ADV, AUX, CCONJ, DET, INTJ, NOUN, NUM,
PART, PRON, PROPN, PUNCT, SCONJ, SYM, VERB, X
B. Evaluation Metrics
Token-Level Accuracy:
Where $N$ is the total number of tokens across all sentences.
C. Baseline Comparisons
- BERT + Linear: Direct classification without CRF
- BiLSTM-CRF: Without pretrained embeddings
- BERT + CRF (ours): Full model with structured prediction
VI. Results
A. Algorithm Comparison
Inference Speed (single sentence, 3 tokens):
| Algorithm | Time (ms) | Relative Speed |
|---|---|---|
| Viterbi (backward) | 2.3 ± 0.1 | 1.0× (baseline) |
| Dijkstra | 3.8 ± 0.2 | 1.65× slower |
| Naive (brute-force) | 147.5 ± 8.3 | 64× slower |
Forward vs. Backward:
- Both algorithms produce identical $\log Z(W)$ values
- Numerical stability confirmed across all test cases
- Forward algorithm slightly faster due to cache efficiency
B. Model Performance
Without Entropy Regularization ($\beta = 0$):
| Epoch | Train Loss | Dev Accuracy |
|---|---|---|
| 1 | 0.421 | 94.32% |
| 2 | 0.187 | 95.18% |
| 3 | 0.103 | 95.67% |
With Entropy Regularization:
| $\beta$ | Final Dev Accuracy | Avg. Entropy |
|---|---|---|
| 0.0 | 95.67% | 0.32 |
| 0.1 | 95.74% | 0.41 |
| 1.0 | 95.51% | 0.68 |
| 10.0 | 94.83% | 1.12 |
Findings:
- Moderate regularization ($\beta = 0.1$) slightly improves generalization
- Strong regularization ($\beta = 10.0$) degrades performance
- Entropy increases with $\beta$, indicating less confident predictions
C. Error Analysis
Common Errors:
NOUN vs. PROPN: Proper noun detection in unknown entities
- Example: "Tesla" → predicted NOUN, should be PROPN
VERB vs. NOUN: Verbal nouns and gerunds
- Example: "running" in "running shoes" → predicted VERB, should be ADJ
ADP vs. PART: Prepositions vs. particles
- Example: "up" in "give up" → predicted ADP, should be PART
Confusion Matrix (Top errors):
Pred: NOUN VERB PROPN ADJ
True:
NOUN 8523 42 78 31
VERB 51 3421 12 18
PROPN 121 8 1876 4
ADJ 28 15 2 1654
D. Ablation Study
| Component | Accuracy | Δ |
|---|---|---|
| Full Model | 95.67% | - |
| - BERT (GloVe) | 92.34% | -3.33% |
| - BiLSTM | 94.89% | -0.78% |
| - CRF (softmax) | 94.12% | -1.55% |
| - Transition weights | 93.78% | -1.89% |
Insights:
- BERT embeddings provide the largest contribution
- CRF layer adds ~1.5% through structured prediction
- Transition weights are crucial for sequential dependencies
VII. Implementation Details
A. Numerical Stability
Log-Space Computation: All probabilities computed in log-space to prevent underflow
Log-Sum-Exp Trick:
Handling Edge Cases:
# Mask out padding and EOS tokens
mask = (W != pad_idx) & (W != eos_idx)
scores = scores * mask + (-inf) * (1 - mask)
B. Memory Optimization
Techniques:
- Gradient Checkpointing: Recompute activations during backward pass
- Mixed Precision: Use FP16 for forward pass, FP32 for gradients
- Batch Processing: Vectorize operations across batch dimension
Memory Usage (batch size 32):
- BERT embeddings: ~1.2 GB
- LSTM hidden states: ~128 MB
- CRF matrices: ~16 MB
- Total: ~1.4 GB GPU memory
C. GPU Acceleration
Device Management:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
Speedup: ~15× faster on GPU (NVIDIA RTX 3080) vs. CPU
VIII. Usage Guide
A. Installation
# Install dependencies
pip install torch==1.12.1
pip install torchdata==0.4.1
pip install transformers
pip install torchtext
B. Training
from Assignment_2_HANDIN import NeuralCRF, train_model_report_accuracy
# Initialize model
crf = NeuralCRF(
pad_idx_word=pad_token_idx,
pad_idx_pos=pos_vocab[pad_token],
bos_idx=init_token_idx,
eos_idx=sep_token_idx,
bot_idx=pos_vocab[init_token],
eot_idx=pos_vocab[sep_token],
t_cal=T_CAL,
transformer=bert,
beta=0.1 # Entropy regularization
)
# Train model
train_model_report_accuracy(
crf=crf,
lr=2e-5,
epochs=3,
train_dataloader=train_dataloader,
dev_dataloader=valid_dataloader,
pad_token_idx_word=pad_token_idx,
pad_token_idx_tag=pos_vocab[pad_token]
)
C. Inference
# Predict tags for new sentence
sentence = ["The", "cat", "sat", "on", "the", "mat"]
tokens = tokenizer.encode(sentence)
W = torch.tensor([tokens])
# Get predictions
predicted_tags = crf(W)
print(predicted_tags)
IX. Advanced Topics
A. Marginal Inference
Computing Tag Marginals:
Applications:
- Confidence estimation
- Active learning sample selection
- Uncertainty quantification
B. Constrained Decoding
Hard Constraints: Enforce linguistic rules
- No VERB → VERB transitions (no verb clusters)
- DET must be followed by NOUN/ADJ
Implementation: Mask invalid transitions in transition matrix
C. Multi-Task Learning
Joint Training: POS tagging + Named Entity Recognition
- Shared BERT encoder
- Separate CRF layers for each task
- Improved performance on both tasks
X. Limitations and Future Work
A. Current Limitations
- Computational Cost: BERT forward pass is expensive
- Fixed Tag Set: Cannot handle unseen tags
- Language-Specific: Trained only on English
- Sequence Length: Limited by BERT's 512 token maximum
B. Proposed Extensions
- Distillation: Compress BERT to smaller model (DistilBERT)
- Cross-Lingual: Multilingual BERT for zero-shot transfer
- Few-Shot Learning: Adapt to new tag sets with minimal data
- Streaming Inference: Process long documents efficiently
- Higher-Order CRFs: Model dependencies beyond adjacent tags
XI. Conclusion
This project demonstrates the effectiveness of combining neural embeddings with structured prediction for sequence labeling tasks. The Neural CRF architecture achieves 95.67% accuracy on POS tagging by leveraging:
- Rich Representations: BERT provides contextualized word embeddings
- Sequential Modeling: BiLSTM captures additional context
- Structured Prediction: CRF models tag dependencies globally
The implementation includes multiple inference algorithms with trade-offs between speed and flexibility. Entropy regularization provides modest improvements in generalization. The system represents a strong baseline for structured prediction tasks in NLP.
XII. References
[1] J. Lafferty, A. McCallum, and F. Pereira, "Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data," in Proceedings of ICML, 2001.
[2] Z. Huang, W. Xu, and K. Yu, "Bidirectional LSTM-CRF Models for Sequence Tagging," arXiv preprint arXiv:1508.01991, 2015.
[3] J. Devlin, M.-W. Chang, K. Lee, and K. Toutanova, "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding," in Proceedings of NAACL-HLT, 2019.
[4] G. Lample, M. Ballesteros, S. Subramanian, K. Kawakami, and C. Dyer, "Neural Architectures for Named Entity Recognition," in Proceedings of NAACL-HLT, 2016.
[5] M. Peters, M. Neumann, M. Iyyer, M. Gardner, C. Clark, K. Lee, and L. Zettlemoyer, "Deep contextualized word representations," in Proceedings of NAACL, 2018.
[6] X. Ma and E. Hovy, "End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF," in Proceedings of ACL, 2016.
XIII. Appendix
A. File Structure
Assignment-2/
├── README.md # This document
├── Assignment_2_HANDIN.ipynb # Main implementation
├── Assignment 2-1.pdf # Assignment specification
└── NLP_CA2_report.pdf # Project report
B. Hyperparameter Grid Search
| Learning Rate | Hidden Dim | Beta | Dev Accuracy |
|---|---|---|---|
| 1e-5 | 64 | 0.0 | 94.82% |
| 2e-5 | 64 | 0.0 | 95.31% |
| 2e-5 | 128 | 0.0 | 95.67% ✓ |
| 2e-5 | 256 | 0.0 | 95.49% |
| 5e-5 | 128 | 0.0 | 94.93% |
C. Code Repository
Repository: Natural-Language-Processing-Projects-SUT
Branch: main
Path: /nlp-projects/Assignment-2/
D. Computational Requirements
Training Time:
- GPU (RTX 3080): ~45 minutes for 3 epochs
- CPU (Intel i7-10700K): ~12 hours for 3 epochs
Inference Speed:
- GPU: ~200 sentences/second
- CPU: ~15 sentences/second
License
This project is part of academic coursework at ETH Zürich and is provided for educational purposes.
Last Updated: October 2, 2025
Document Version: 1.0
- Total size
- 2.24 GB
- Files
- 2,145
- Last updated
- Jun 17
- Pre-warmed CDN
- US EU US EU