This repository contains the implementation for learning a shared semantic coordinate system from multiple embedding models.
The core idea is to represent each embedding model's semantic graph as
[ G \approx M S ]
where
- G : relation graph constructed from a common word set
- S : shared semantic coordinate system (shared signified basis)
- M : model-specific semantic transformation
The final goal is not S itself, but obtaining comparable M matrices across different embedding models.
semantic_alignment/
│
├── config.py # Experiment configuration
├── requirements.txt
├── README.md
│
├── data/
│ └── words.txt # Common word set W
│
├── scripts/
│ ├── run_ms.py # Main experiment
│ ├── analyze_m.py # Analyze learned M matrices
│ └── check_cuda.py # CUDA check
│
├── utils/
│ ├── embeddings.py
│ ├── graph.py
│ ├── factorization_ms.py
│ ├── metrics.py
│ ├── experiments_ms.py
│ ├── alignment.py
│ └── io.py
│
└── outputs/
├── csv/
├── models/
└── figures/
Python 3.11 is recommended.
Create a virtual environment.
Windows
python -m venv .venv
.venv\Scripts\activateLinux
python3 -m venv .venv
source .venv/bin/activatePyTorch should be installed before installing the remaining dependencies.
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121pip install torch torchvision torchaudiopip install -r requirements.txtpython scripts/check_cuda.pyExpected output
CUDA available: True
Device: cuda
GPU: NVIDIA GeForce GTX 1650
Tensor device: cuda:0
OK
If CUDA is unavailable, verify
- NVIDIA driver
- CUDA-compatible PyTorch installation
Run the complete MS factorization pipeline.
python scripts/run_ms.pyThe pipeline performs
- Load word set
- Generate embeddings
- Construct relation graph G
- Automatic relation temperature selection
- Learn
[ G \approx MS ]
- Save learned
- common S
- model-specific M
- pairwise comparison results
Typical outputs are
outputs/
common_S.pt
final_MS_pair_xxx.pt
selected_relation_temperatures.csv
same_G_summary.csv
cross_model_summary.csv
After factorization,
python scripts/analyze_m.pyThis computes
- Word-wise cosine similarity
- Factor-wise cosine similarity
- RMSE
- L1 difference
- Top-1 factor agreement
for every model pair.
For a common vocabulary
[ W={w_1,\ldots,w_n} ]
each embedding model produces embeddings
[ P ]
A semantic relation graph is constructed
\exp \left( \frac{\cos(p_i,p_j)}{T} \right) ]
where
- cosine similarity defines semantic relations
- T is the relation temperature.
We then solve
[ G \approx MS ]
where
- G : semantic relation graph
- S : shared semantic coordinate system
- M : model-specific semantic transformation
Each model automatically selects its own optimal temperature.
Candidate temperatures are
0.05
0.07
0.10
0.15
0.25
The selected temperature maximizes
- factorization stability
- shared semantic consistency
- reconstruction quality
using repeated random initializations.
The objective is not matrix factorization itself.
Instead, this work investigates whether different embedding models can be represented within a shared semantic coordinate system, enabling direct comparison of their semantic transformations through the learned matrices M.
If successful,
different embedding models become comparable in a common semantic basis.
Current experiments include
- sentence-transformers/all-MiniLM-L6-v2
- sentence-transformers/all-mpnet-base-v2
- BAAI/bge-base-en-v1.5
- intfloat/e5-base-v2
- thenlper/gte-base
Additional embedding models can be added by modifying
config.py
If this repository contributes to your research, please cite the accompanying paper (to be added).