nlp-averaged-meta-embedding
Element-wise average of multiple pretrained embedding matrices as a parameter-free meta-embedding
SKILL.mdskills/nlp/averaged-meta-embedding/SKILL.md
GitHubOverview
Concatenating embedding matrices (GloVe + Paragram + FastText) doubles or triples the input dimension, inflating model size. An unweighted mean of the matrices — Dynamic Meta Embedding (DME) — preserves the original dimension while combining the semantic signal from each source. It's parameter-free, adds no inference cost, and often performs comparably to weighted blends.
Quick Start
import numpy as np
def load_glove(word_index, embed_size):
# ... returns (vocab_size, embed_size)
...
def load_paragram(word_index, embed_size):
# ... returns (vocab_size, embed_size)
...
embedding_matrix_1 = load_glove(word_index, 300)
embedding_matrix_2 = load_paragram(word_index, 300)
# Unweighted DME: element-wise mean, same shape as inputs
embedding_matrix = np.mean([embedding_matrix_1, embedding_matrix_2], axis=0)
# Shape: (vocab_size, 300) — NOT (vocab_size, 600)
Workflow
- Build embedding matrices from each pretrained source using the same word→index mapping
- Stack them and take
np.mean(..., axis=0)— result has the same shape as each source - Pass the averaged matrix to your
Embeddinglayer - Train the model normally — no special architecture changes required
Key Decisions
- Dim compatibility: All matrices must share the same dimension. Project mismatched sources with a linear layer or drop them.
- vs. concat: Concat doubles input dim, increases first-layer parameters. Mean keeps dim fixed, no parameter growth.
- vs. weighted mean: Simple mean is often within 0.5% of weighted blends and needs no hyperparameter search.
- Init alignment: Ensure OOV rows are handled consistently across sources before averaging, or the mean becomes noisy.