This repository presents an analysis of different strategies for combining ELMo's contextual layer outputs and compares ELMo with traditional word embedding techniques on a classification task.
In ELMo, each word representation is computed as a weighted sum of outputs from multiple layers. The weights (λ₁, λ₂, λ₃) determine the relative contribution of the token layer, the first LSTM layer, and the second LSTM layer.
Configuration:
- λ values initialized uniformly: λ₁ = λ₂ = λ₃ = 0.3
- Optimized during training via backpropagation
- Softmax-like normalization applied to maintain a convex combination
Observed Behavior:
-
Layer Specialization:
- λ₂ (middle layer) became dominant: +1.1668
- λ₃ (top layer) contributed negatively: –0.8541
- λ₁ (token embeddings) remained small but positive: +0.0824
-
Training Correlation:
- Gradual divergence of λ values aligned with 98% reduction in loss
- Final accuracy: 71.92%
Advantages:
- Learns task-specific layer importance
- Adjusts dynamically based on input and supervision
Limitations:
- Sensitive to initialization
- May exhibit instability in low-resource settings
Configuration:
- Manually defined: λ₁ = 0.45, λ₂ = 0.15, λ₃ = 0.40
- Coefficients remain constant during training
Performance:
- Faster early convergence: +6.3% accuracy in first three epochs
- Final accuracy: 71.62%
- Layer contributions fixed throughout
Use Case Suitability:
- Reproducible baselines
- Scenarios with limited computational budget
- Applications prioritizing deterministic behavior
Architecture:
nn.Sequential(
nn.Linear(3 * hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim)
)Performance:
- Final accuracy: 71.64%
- Outperformed fixed λs
- Enhanced class-specific learning (e.g., +111 correct Class 3 predictions vs trainable λs)
Trade-offs:
- Higher computational cost
- Greater architectural tuning complexity
| Method | Accuracy | F1 Score | Peak Epoch | Test Loss |
|---|---|---|---|---|
| Trainable λs | 71.92% | 0.7183 | 10 | 0.9171 |
| Fixed λs | 71.62% | 0.7059 | 7 | 0.9554 |
| Learnable MLP | 71.64% | 0.7164 | 9 | 0.9136 |
- Trainable λs: Effective separation of Classes 0 and 1
- MLP Fusion: Superior recall on Class 3
- Fixed λs: Balanced misclassification across classes
| Method | Loss Fluctuation | Overfitting Onset |
|---|---|---|
| Trainable λs | Low | Epoch 8 |
| Fixed λs | Medium | Epoch 5 |
| Learnable MLP | Medium | Epoch 7 |
| Embedding Technique | Accuracy | Δ vs ELMo | Training Speed | Contextual Awareness |
|---|---|---|---|---|
| ELMo (Trainable) | 71.92% | – | Moderate | Yes |
| CBOW | 42.34% | –29.58% | Fast | No |
| SVD | 42.59% | –29.33% | Fast | No |
ELMo outperforms static methods by over 30%, attributed to its use of contextual representations.
-
CBOW/SVD (Static):
- One embedding per word
- No position or context awareness
- Struggles with polysemy and long-range semantics
-
ELMo (Contextual):
- Dynamic word representation based on context
- Captures hierarchical linguistic features from stacked LSTM layers
| Technique | Primary Error Pattern |
|---|---|
| CBOW | Uniform misclassification |
| SVD | Isolated misclassification spikes |
| ELMo | Class-boundary confusion |
| Objective | Recommended Approach |
|---|---|
| Maximum classification accuracy | Trainable λs |
| Stable and interpretable baseline | Fixed λs (e.g., 0.45, 0.15, 0.40) |
| Class-sensitive representation | MLP-based layer fusion |
-
Contextual embeddings such as ELMo should be prioritized in NLP pipelines
-
Static embeddings may be used in:
- Baseline evaluations
- Low-compute environments
- Situations where interpretability and reproducibility are critical
- ELMo with Trainable λs — highest performance
- ELMo with MLP Fusion — flexible, class-aware modeling
- ELMo with Fixed λs — efficient and stable
- Static Embeddings (CBOW, SVD) — fallback option