Neural CRFs: a learned encoder with a structured output layer
A BiLSTM–CRF lets a network learn the emission scores and keeps the CRF on top. How the gradient links the two, which baselines it must beat, and the leaks and constraints that flatter results.
What runs in CodeSplash
- UNC002Neural conditional random field (BiLSTM–CRF family) Needs a data type CodeSplash doesn't load yet
- Conditional random field (sequence labeling) Verified in Python and R
You will be able to
- Describe the encoder-plus-CRF architecture and what each part contributes
- Read the emission gradient as "marginal minus truth"
- Name the two baselines a neural CRF must beat
- Spot hard-constraint, tokenization and leakage problems in sequence-labeling results
Replace hand-made features with an encoder
ReadA linear-chain CRF needs emission scores : how well label fits position . In the classic setup they come from features you design: the word, its suffix, capitalization, the neighbors. A neural CRF lets a network learn them. A bidirectional LSTM (or a transformer) reads the whole sequence, , and a linear layer turns each position's representation into emissions, .
The CRF stays on top: transitions and the global normalization over whole label sequences. The encoder supplies context; the CRF keeps the labels consistent with each other.
One loss, trained end to end
ReadTraining minimizes the same sequence loss, , but now gradients flow into the encoder. The gradient for one emission has a clean form:
The model's marginal minus the truth: forward–backward marginals become the error signal for backpropagation. In the reference's three-token example the loss is 1.39 and one emission's gradient −0.400, matching a finite-difference check.
Is the CRF layer worth it?
PredictPredict first
A neural CRF reports a strong span F1. Which baselines must you compare it with to know what each part bought?
Reveal
Both. A token-by-token softmax on the same encoder shows what the CRF layer adds; a fixed-feature CRF shows what the learned encoder adds. Without both, you can't tell which part did the work.
Ways results mislead
Read- Hard constraints. Forbidding transitions (say, outside → inside) by setting their score to minus infinity can silently delete the only valid path when tokenization or labels disagree. Count how often decoding hits that.
- Tokenization. Gold labels must align with the model's tokens; subword splits need a stated rule.
- Leakage. Random token splits let the encoder memorize sentences; pretrained encoders may have seen the benchmark. Split by document, and check for overlap.
- Streaming. A bidirectional encoder reads the future, so it can't label a live stream without a causal variant.
A low loss isn't calibrated span probability: token marginals and span confidence need different summaries.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| Neural CRF, BiLSTM–CRF (UNC002) | Needs a data type CodeSplash doesn't load yet: an encoder trained on token-level text. The linear-chain part runs in Conditional random field (sequence labeling) with feature columns you provide |
Check your understanding
CheckpointCheck yourself
In a BiLSTM–CRF, what does the BiLSTM provide?
Show the answer
Answer: a
The encoder replaces hand-designed features. Transitions and global normalization still come from the CRF layer.
Check yourself
The gradient of the loss with respect to an emission is the marginal minus . When is it zero?
Show the answer
Answer: b
The gradient pushes marginals toward the truth. That's how forward–backward feeds backpropagation.
Check yourself
You forbid the transition outside → inside, but some gold sequences contain it because of a tokenization quirk. What happens?
Show the answer
Answer: b
Hard constraints remove paths entirely. Check label alignment and count sequences the constraints rule out.
Check yourself
Why can a random token-level split inflate a neural CRF's score?
Show the answer
Answer: a
Split by document or sentence, and check pretrained encoders for benchmark overlap.
What comes next
RecapThe last unit turns to causal questions: learning a causal graph from data, and estimating effects with machine learning without fooling yourself.
Concepts in this lesson