summaryrefslogtreecommitdiff
path: root/scripts/build_item_space.py
blob: c98238c0deca23645f128448a21f3bd9a4906ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""
Script to build Item Space (PCA Projection) from Memory Embeddings.
Inputs:
- data/corpora/memory_embeddings.npy (M x 4096)
Outputs:
- data/corpora/item_projection.npz (P, mean, V)
"""

import sys
import os
import numpy as np

# Add src to sys.path
sys.path.append(os.path.join(os.path.dirname(__file__), "../src"))

from personalization.user_model.features import ItemProjection

def main():
    emb_path = "data/corpora/memory_embeddings.npy"
    out_path = "data/corpora/item_projection.npz"
    
    if not os.path.exists(emb_path):
        print(f"Error: {emb_path} not found. Run migrate_preferences.py first.")
        sys.exit(1)
        
    print(f"Loading embeddings from {emb_path}...")
    E = np.load(emb_path)
    print(f"Loaded shape: {E.shape}")
    
    # Target dimension k=256
    k = 256
    print(f"Fitting PCA with k={k}...")
    
    proj = ItemProjection.from_pca(E, k=k)
    
    print("Transforming all embeddings to item space...")
    V = proj.transform_embeddings(E)
    print(f"Item vectors shape: {V.shape}")
    
    print(f"Saving projection to {out_path}...")
    np.savez(
        out_path,
        P=proj.P,
        mean=proj.mean,
        V=V
    )
    print("Done.")

if __name__ == "__main__":
    main()