How a CNN reads DNA

Convolutional neural networks as learned motif scanners

Epigenetics

About this lecture

Machine learning from scratch fitted a logistic regression on features we chose (GC content, motif counts). A convolutional neural network (CNN) learns its own features directly from the DNA sequence. This lecture opens the box:

  • One-hot encoding: DNA as a matrix of numbers.
  • A convolutional filter is a PWM that slides along the sequence; ReLU and max pooling turn its scores into “is the motif there?”.
  • A dense layer with a sigmoid on top: logistic regression on learned features.
  • Training: loss, gradient descent, epochs, validation, early stopping, in pictures.
  • Real data: a small CNN that tells GM12878 CTCF peaks from GC-matched background, its test performance, and what its filters learned.

The task: is this 200 bp bound by CTCF?

  • Positives: 200 bp centred on each of the 44 217 GM12878 CTCF peak summits. Negatives: random windows with the same GC distribution, more than 1 kb from any peak.
  • Split by chromosome: train on 20, validate on chr7, test on chr8 + chr9, as in Machine learning from scratch.
  • Balanced classes: 44 217 positives, 43 586 negatives.

The baseline: scan with the CTCF PWM

  • Score each window by its best match to the JASPAR CTCF matrix (both strands): test AUC 0.93.
  • A strong baseline, but the distributions overlap: some peaks have only a weak match, and some background windows a good one.
  • Can a model that learns its motifs from the 76 000 training sequences do better?

DNA as numbers: one-hot encoding

  • Each base becomes a column with one 1 and three 0s: a 200-bp sequence is a 4 × 200 matrix.
  • No base is “bigger” than another (coding A = 1, C = 2, G = 3, T = 4 would invent an order).
  • This is the input to every sequence model in this lecture, from our 2 500-parameter CNN to AlphaGenome.

A convolutional filter is a PWM

  • A filter is a 4 × k matrix of weights, like a PWM. Multiply it element-wise with a one-hot window and add everything up.
  • Only the weight of the base that is present survives in each column: the result is the PWM score of the window.
  • Difference: a PWM is computed from known sites; a filter’s weights are learned from labelled sequences.

Sliding the filter: convolution

  • Slide the filter one base at a time and record the score at each position: an activation track. A width-19 filter on 200 bp gives 182 scores.
  • A match scores high (CCAG: 8); one mismatch scores less (CCTG: 5); unrelated sequence is negative.
  • A filter reads one strand. A site on the other strand needs another filter (the reverse complement), which the network learns too.

Bias, ReLU and max pooling

  • Add a bias (one per filter): a learned threshold. ReLU, max(0, x), keeps only the positions that pass it.
  • Max pooling keeps the largest value: with global max pooling, one number per filter says how well its motif is present anywhere in the window.
  • This is a PWM scan with a threshold, as in Gene regulation, except that threshold and weights are learned.

The whole network

  • 32 filters of width 19 → ReLU → global max pool → 32 numbers → a dense layer with a sigmoid → P(bound).
  • The last step is logistic regression, with the 32 filter outputs as its features. Training learns the filters and the regression weights together.
  • 2 497 parameters, trained on 76 015 sequences.

Deeper layers combine motifs

  • A second convolutional layer reads the activation tracks of the first layer: its filters respond to combinations of motifs at a given spacing and orientation.
  • That is the “motif grammar” a PWM cannot represent: pairs of TFs, helical spacing, a required partner.
  • More layers also see further: each layer widens the stretch of DNA that one output depends on (its receptive field).

How wrong is a prediction? The loss

  • The binary cross-entropy (log loss) from Machine learning from scratch: small when confident and right, very large when confident and wrong.
  • The network’s loss is the average over the sequences. Training = finding weights that make it small.
  • Reference values: 0.693 for always predicting 0.5; our trained CNN reaches about 0.20 on the validation chromosome.

Gradient descent

  • Compute the slope of the loss with respect to each weight (the gradient) and take a small step downhill. Repeat.
  • The learning rate η sets the step size: too small is slow, too large overshoots and never settles.
  • Backpropagation computes all 2 497 slopes in one pass backwards through the network: the chain rule, done by the computer.

Training: mini-batches and epochs

  • Each step uses a random mini-batch of 128 sequences: cheap, slightly noisy gradient steps. One epoch is one pass over all training sequences.
  • After each epoch, compute the loss on the validation chromosome; keep the best model; stop when it no longer improves (early stopping).
  • The test chromosomes are not touched until the very end.

Real data: learning curves

  • Left: with 76 015 training sequences the validation loss follows the training loss; best at epoch 13 (validation loss 0.197), stopped at epoch 16.
  • Right: the same network with only 500 training sequences memorises them (training loss → 0.02) while validation stalls at about 0.36: overfitting.
  • The gap between the curves, not the training loss, tells you how well the model will do on new DNA.

Choices you make (hyperparameters)

choice in our model what it controls
number of filters 32 how many motifs the first layer can learn
filter width 19 bp longest motif one filter can see (CTCF core: ~15–20 bp)
pooling global max presence anywhere vs position
layers 1 convolutional + 1 dense motif combinations, receptive field
learning rate, batch size 0.001, 128 speed and stability of training
regularisation early stopping dropout, weight decay are common alternatives
input length 200 bp how much context the model sees
  • Choose them on the validation set, never on the test set. The data (positives, negatives, splits) matter more than most of these.

Real data: test performance

  • On the held-out chr8 and chr9: CNN AUC 0.972, CTCF PWM 0.929, GC content 0.511.
  • At a 5% false positive rate the CNN recovers 87% of the peaks, the PWM 74%.
  • The CNN learns more than the core motif; the next slides show what.

Real data: the filters learned the CTCF motif

  • For each filter, collect the 19-bp windows where it fires strongly in the test sequences and draw them as a logo.
  • The filters with the largest output weights are the CTCF motif, on both strands, matched to JASPAR by correlation (as TOMTOM does).
  • The network was never told about CTCF: it found the motif from labelled sequences alone.

Real data: all 32 filters

  • 9 of 32 filters match CTCF or CTCFL (correlation ≥ 0.9), all with positive output weights: they are the evidence for “bound”.
  • Most others are short, GC-rich, low-information patterns with negative weights: “GC-rich, but no CTCF motif” points to background.
  • Their best JASPAR matches (ZIC, KLF, PLAGL2 …) are weak; with 879 matrices to compare, some match is always found.

Real data: where and which filters matter

  • Left: the strongest CTCF filter fires at the summit, like the motif scan in Gene regulation. The network was not given positions.
  • Right: the 9 CTCF-like filters alone give AUC 0.945, a little better than the JASPAR PWM; the other 23 together only 0.660.

What this model does not know

  • Cell type: trained on GM12878 only. CTCF binding is largely shared between cell types; most TFs’ binding is not.
  • Chromatin: it sees 200 bp of sequence, not accessibility or DNA methylation. Two copies of the same motif, one methylated, get the same score.
  • Context: nothing beyond ±100 bp; no information on where the window lies in the genome.
  • The labels: peaks above an IDR threshold of one experiment, and negatives chosen by us. It learns the boundary we drew, including our biases.
  • Prediction ≠ mechanism: high accuracy does not show that the learned features cause binding.

BPNet: predicting the profile, not just “bound”

  • BPNet predicts the number and positions of reads at every base (ChIP-nexus), not a yes/no label.
  • Dilated convolutions see 1 kb with few parameters. Interpreting the model revealed soft motif syntax, tested by CRISPR point mutations.
  • The same design, applied to ATAC-seq, is chromBPNet (next unit).

Summary

idea key fact
one-hot encoding 4 × L matrix, one 1 per position
convolutional filter 4 × k weights = a learned PWM; score of a window = sum of weights of its bases
bias + ReLU learned threshold: max(0, score + b)
max pooling “is the motif present?” (global) or rough position (local)
dense + sigmoid logistic regression on the pooled filter outputs
deeper layers motif combinations, spacing, longer receptive field
training log loss; gradient descent with backpropagation; mini-batches, epochs
validation choose epoch and hyperparameters; early stopping; test used once
our CTCF CNN test AUC 0.972 vs PWM 0.929 vs GC 0.511 (chr8 + chr9)
filters 9 of 32 learn the CTCF motif, both strands, at the summit

Key papers (optional further reading)

Not required reading and not examined. For those who want the primary literature.