Labs · BERT, part by part

Pooler and next-sentence head

The [CLS] row through a dense layer and tanh — the pooler — then two logits: is sentence B the one that followed A?

p=softmax ⁣(tanh⁡(h[CLS]Wpool+bpool) WNSP+bNSP)p = \mathrm{softmax}\!\left(\tanh(h_{[\mathrm{CLS}]} W_{\text{pool}} + b_{\text{pool}})\, W_{\text{NSP}} + b_{\text{NSP}}\right)Opens at 2 pairs · d_model = 8 · W_pool 8 × 8 · W_NSP 8 × 2

In plain words: BERT's second training task, next-sentence prediction (NSP), shows it two sentences, A and B, and asks whether B really came next. It answers from one vector — the final row of the [CLS] token, the extra token at the start of every input. That row goes through the pooler: one dense layer (a weight matrix W_pool and a bias), then tanh, which squeezes every number into −1…1. A two-way classifier (W_NSP and a bias) turns the pooled vector into two scores (logits), and a softmax into P(IsNext) and P(NotNext). The pooler is not the same thing as pooling: pooling averages or picks rows as they are; the pooler is a layer on top that reads the [CLS] row alone. In BERT that layer is trained by this task; in this lab no preset trains it — W_pool is a random draw. The encoder rows here are test data — random numbers, normalised as an encoder's output is — not computed from the words shown.

Written in row-vector form, as the platform's numpy trainer computes it: h · W_pool, so W_pool here is the transpose of the W in worked example 03's pooled = tanh(W · h + b), and W_NSP is 8 × 2 where papers write 2 × 8. d_model = 8 is how many numbers each row holds; the seed fixes the random draws.

Constructed, not trained: W_pool is a random draw, and the classifier W_NSP is solved from the two pooled vectors so the IsNext pair lands on IsNext and the NotNext pair on NotNext. Edit a [CLS] row and watch the decision move.

[CLS] → pooler → IsNext or NotNext

Reads /api/labs/pooler-nsp — loading

Computing…

Pooler and next-sentence head — Labs — TransformerLab