This project implements the t-vMF (truncated von Mises-Fisher) similarity function as described in Takumi Kobayashi's paper. The paper introduced t-vMF similarity as a regularization technique for intra-class feature distribution in machine learning. I hope to explore the utility of t-vMF for other applications, such as text data (ie. comparing BERT vectors).
- Python 3.7 or higher
- pip (Python package installer)
-
Clone the repository:
git clone <repository-url> cd t-vMF
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
On Linux/macOS:
source venv/bin/activateOn Windows:
venv\Scripts\activate
-
Install required packages:
pip install -r requirements.txt
-
Start Jupyter Notebook:
jupyter notebook
-
Open and run the demo:
- Navigate to
demo.ipynbin the Jupyter interface - Run all cells to see the t-vMF similarity implementation and visualization
- Navigate to
When you're done working with the project:
deactivateThe main implementation (from the paper) includes:
-
tvMF_similarity(w, x, k): Core function that computes t-vMF similarityw: classifier weight vectorx: input feature vectork: kappa parameter (controls concentration)
-
Visualization: 2D polar plots showing how t-vMF similarity varies with different kappa values
-
Examples: Demonstration with various input vectors and kappa parameters
The t-vMF similarity is computed as:
φ = (1 + cosine) / (1 + κ(1 - cosine)) - 1
Where:
- cosine = dot(normalize(x), normalize(w))
- κ (kappa) controls the concentration parameter
- Higher κ values create sharper concentration around the classifier weight vector