This project analyzes and compares text embedding spaces generated by different models and studies how fine-tuning reshapes embedding geometry.
It combines:
- Embedding comparison (BERT vs Contrastive model)
- Embedding improvement (before vs after fine-tuning)
The goal is to understand how embedding representations affect clustering, semantic similarity, and downstream task performance.
- Compare embedding quality of BERT and Sentence Transformers
- Visualize embeddings using:
- PCA
- t-SNE
- UMAP
- Study the effect of fine-tuning on embedding space
- Analyze:
- Cluster separability
- Anisotropy vs isotropy
- Evaluate embeddings using:
- Silhouette score
- Linear probe classification
- Source:
sklearn.datasets.fetch_20newsgroups - Categories used:
- rec.sport.baseball
- rec.sport.hockey
- sci.space
- comp.graphics
- Sample size: 500 texts
- Model:
bert-base-uncased - Approach: Mean pooling over token embeddings
- Model:
all-MiniLM-L6-v2 - Trained using contrastive learning for semantic similarity
To visualize high-dimensional embeddings:
- PCA → Linear projection
- t-SNE → Preserves local structure
- UMAP → Preserves local + global structure
- Model:
distilbert-base-uncased - Task: Text classification
- Dataset: Same 20 Newsgroups subset
- Output: Task-specific embeddings
- Extracted using [CLS] token representation
- Compared with original BERT embeddings
- PCA and UMAP plots before and after fine-tuning
- Silhouette Score
- Logistic Regression classifier trained on embeddings
- BERT embeddings are scattered and overlapping
- Contrastive embeddings form clear, separable clusters
- Indicates better semantic structure in contrastive models
- PCA: Basic separation (linear)
- t-SNE: Best visual cluster separation
- UMAP: Shows both structure and overlap between classes
- Embeddings become:
- More compact
- More separable
- Transition from:
- Anisotropic → More isotropic space
- Silhouette score increases
- Linear probe accuracy improves
- Pretrained BERT is not optimized for sentence similarity
- Contrastive learning significantly improves embedding geometry
- Fine-tuning aligns embeddings with task-specific objectives
- Better embedding structure → better downstream performance
This project demonstrates that:
- Embedding quality depends heavily on training objectives
- Contrastive models outperform vanilla BERT in semantic tasks
- Fine-tuning reshapes embedding space to be more structured and discriminative
- Visualization + metrics together provide a complete understanding of embedding behavior
- Python
- PyTorch
- Hugging Face Transformers
- Sentence Transformers
- Scikit-learn
- UMAP
- Matplotlib
uv init
uv sync