Skip to content

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.

Advanced ~11 min7 steps

What runs in CodeSplash

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

Read first

01

Replace hand-made features with an encoder

Read

A linear-chain CRF needs emission scores Et,kE_{t,k}: how well label kk fits position tt. 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, H=fθ(x)H=f_\theta(x), and a linear layer turns each position's representation into emissions, E=HW+bE=HW+b.

The CRF stays on top: transitions AA and the global normalization over whole label sequences. The encoder supplies context; the CRF keeps the labels consistent with each other.

02

One loss, trained end to end

Read

Training minimizes the same sequence loss, L=log⁡Z(E,A)−S(E,A,y)\mathcal L=\log Z(E,A)-S(E,A,y), but now gradients flow into the encoder. The gradient for one emission has a clean form:

∂L∂Etk=p(yt=k∣x)−1[yt=k].\frac{\partial\mathcal L}{\partial E_{tk}}=p(y_t=k\mid x)-\mathbf 1[y_t=k].

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.

03

Is the CRF layer worth it?

Predict

Predict first

A neural CRF reports a strong span F1. Which baselines must you compare it with to know what each part bought?

Only a fixed-feature CRFA token-by-token softmax and a fixed-feature CRF
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.

04

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.

05

Where it stands in CodeSplash

Read
EntryIn 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
Reference · UNC002 · Needs a data type CodeSplash doesn't load yetNeural conditional random field (BiLSTM–CRF family)Combine learned contextual token representations with globally normalized structured decoding. The neural encoder replaces hand-designed CRF input features; the CRF head preserves label-transition interactions.
06

Check your understanding

Checkpoint

Check 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 EtkE_{tk} is the marginal p(yt=k∣x)p(y_t=k\mid x) minus 1[yt=k]\mathbf 1[y_t=k]. 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.

07

What comes next

Recap

The 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