Skip to content
Back to skills

Weighted Layer Pooling

ASecurity

Learns a weighted combination of CLS embeddings across all transformer layers instead of using only the last layer.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
developmentpython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill weighted-layer-pooling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Weighted Layer Pooling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Weighted Layer Pooling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-weighted-layer-pooling/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-weighted-layer-pooling)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-weighted-layer-pooling
description: >
  Learns a weighted combination of CLS embeddings across all transformer layers instead of using only the last layer.
---
# Weighted Layer Pooling

## Overview

Different transformer layers capture different linguistic features — lower layers for syntax, higher for semantics. Instead of using only the final layer, weighted layer pooling learns a soft weight per layer and computes a weighted mean. This typically improves regression tasks where multiple levels of language understanding matter.

## Quick Start

```python
import torch
import torch.nn as nn

class WeightedLayerPooling(nn.Module):
    def __init__(self, num_hidden_layers, layer_start=4):
        super().__init__()
        self.layer_start = layer_start
        self.num_layers = num_hidden_layers - layer_start + 1
        self.layer_weights = nn.Parameter(torch.ones(self.num_layers))

    def forward(self, all_hidden_states):
        layers = torch.stack(all_hidden_states[self.layer_start:])  # (L, B, S, H)
        weights = self.layer_weights.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
        weights = weights.expand(layers.size())
        weighted = (weights * layers).sum(dim=0) / self.layer_weights.sum()
        return weighted[:, 0]  # CLS token
```

## Workflow

1. Set `output_hidden_states=True` in model config
2. Collect all hidden states from the transformer
3. Apply learnable weights across selected layers
4. Use weighted output as input to regression/classification head

## Key Decisions

- **layer_start**: Skip early layers (0-3) which capture low-level token features
- **Initialization**: Start with uniform weights; the model learns the optimal mix
- **Alternatives**: Concatenation (4x hidden size) or LSTM pooling (more params)

## References

- Feedback Prize - English Language Learning (Kaggle)
- Source: [utilizing-transformer-representations-efficiently](https://www.kaggle.com/code/rhtsingh/utilizing-transformer-representations-efficiently)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…