-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCosineVisualization.py
More file actions
38 lines (30 loc) · 1.06 KB
/
Copy pathCosineVisualization.py
File metadata and controls
38 lines (30 loc) · 1.06 KB
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
import numpy as np
from embeddings import *
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
# Reduce 32 dimensions to 2 for visualization
words_to_plot = ["crime", "punishment", "murder", "guilt", "police", "love", "happy", "innocent", "death", "money"]
vectors = []
labels = []
for word in words_to_plot:
if word in word_to_idx:
vectors.append(input_embeddings[word_to_idx[word]])
labels.append(word)
vectors = np.array(vectors)
# PCA to reduce to 2D
pca = PCA(n_components=2)
reduced = pca.fit_transform(vectors)
# Plot
plt.figure(figsize=(10, 8))
plt.scatter(reduced[:, 0], reduced[:, 1], color='steelblue', s=100)
for i, label in enumerate(labels):
plt.annotate(label, (reduced[i, 0], reduced[i, 1]),
fontsize=12, ha='right')
plt.title("Word Embeddings Before Training (Random)\nWords are scattered randomly", fontsize=14)
plt.xlabel("PCA Dimension 1")
plt.ylabel("PCA Dimension 2")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("embeddings_before_training.png", dpi=300)
plt.show()
print("Plot saved.")