Interactive engineering lab

Embedding Pooling: Watch Padding Change a Sentence Vector

Embedding Pooling: Watch Padding Change a Sentence Vector. Embed a query alone and in a padded batch under six pooling rules and see when its vector and top article change.

By Sandeep Belgavi · Published 2026-11-16

Synthetic fixtures computed locally. No model, encoder, orchestrator or production service is contacted.

Experiment workspace

Decision trace

    The question this experiment answers

    Dinky's help-desk search at Belgavi's Book Store embeds customer questions in batches. A short question in a batch with a longer one is padded to the longer length. Does the pooled sentence vector, and therefore the retrieved help article, change because of what else is in the batch? This lab pools synthetic token vectors for a query embedded alone and in a batch, under six pooling rules and both padding sides, and scores four help articles.

    Change one assumption at a time

    Start from mean_unmasked_right: an 8-token query, a partner 8 tokens longer, mean pooling over every position. The query vector alone and in the batch have cosine 0.768123 and the top article changes from returns to tracking. Switch pooling to mean_masked and the two vectors become identical. Shorten the partner to see the error shrink without disappearing. Try last_fixed with right and then left padding, and first_fixed with left padding, then their mask-aware versions. Set pad magnitude to 0 and turn normalisation off to see that even zero pad vectors change an unnormalised mean.

    Expected outcomes

    mean_unmasked_right: 8 pad positions, cosine 0.768123, top article returns alone and tracking in the batch. mean_masked_right: cosine 1, batch invariant. mean_unmasked_short_partner: 4 pads, cosine 0.91463, the top article survives but the scores move. last_fixed_right: cosine -0.079972, top article changes. last_mask_aware_right and last_fixed_left: batch invariant. first_fixed_left: cosine 0.21819, top article changes; first_mask_aware_left: batch invariant. mean_unmasked_zero_pads: batch invariant after normalisation. gift_card_mean_unmasked: 12 pads, cosine 0.427986, top article changes from gift_cards to tracking.

    Reading the decision trace

    The first line restates the setup. The next lines give the token counts and the batch row length, how many pad positions were added and on which side, and which positions the chosen pooling rule reads, marked when they include pad positions. Then each help article is listed in order of its score in the batch, with its score and rank alone and in the batch. The last line summarises whether the query vector is batch invariant. Use Next step, Show all or the slider to move through it.

    What this teaches

    Encoders that apply the attention mask give real tokens the same hidden states with or without padding, but nothing constrains what the pad positions hold. Pooling must use the mask too: a masked mean, or the first or last position whose mask is 1. Fixed-position pooling breaks on the padded side only, and an unmasked mean breaks on either side, more with more padding. The test that catches all of these is to embed each text alone and in a batch with a long neighbour and require the same vector. The companion article covers the library implementations and the test.

    Limits of this model

    Synthetic six-dimensional token vectors (one topic weight plus small deterministic noise per word), not a trained encoder. They are not contextual, so first and last-token rankings mean little here; only the comparison between alone and in-batch is meaningful. Pad positions get one fixed vector scaled by the pad magnitude; real encoders produce different values at pad positions, so the size of the error is illustrative. Whitespace tokens, no special tokens, one batch partner, documents always embedded alone with masked mean pooling. Values are rounded to six decimal places.

    Views are my own and do not represent any employer, past or present.