|
| 1 | +""" |
| 2 | +Text Embeddings: Generation, Comparison & Visualization |
| 3 | +======================================================== |
| 4 | +Requirements: pip install sentence-transformers numpy matplotlib seaborn scikit-learn |
| 5 | +""" |
| 6 | +import numpy as np |
| 7 | +from sentence_transformers import SentenceTransformer |
| 8 | +import matplotlib; matplotlib.use('Agg') |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import seaborn as sns |
| 11 | +from sklearn.decomposition import PCA |
| 12 | +from sklearn.manifold import TSNE |
| 13 | +from sklearn.metrics.pairwise import cosine_similarity |
| 14 | +from rich.console import Console |
| 15 | +from rich.table import Table |
| 16 | +from rich.panel import Panel |
| 17 | + |
| 18 | +console = Console() |
| 19 | + |
| 20 | +# 50 sentences across 10 categories |
| 21 | +sentences = [ |
| 22 | + "The computer processed data at incredible speed", |
| 23 | + "Machine learning models require large amounts of training data", |
| 24 | + "Python is widely used for artificial intelligence applications", |
| 25 | + "Cloud computing enables scalable web services", |
| 26 | + "The algorithm optimized the search results efficiently", |
| 27 | + "The dog chased the ball across the green field", |
| 28 | + "Cats are independent creatures that enjoy solitude", |
| 29 | + "The majestic eagle soared high above the mountains", |
| 30 | + "Dolphins are highly intelligent marine mammals", |
| 31 | + "The tiger stalked its prey through the dense jungle", |
| 32 | + "The chef prepared a delicious Italian pasta dish", |
| 33 | + "Fresh ingredients make the best homemade meals", |
| 34 | + "The chocolate cake was rich and decadently sweet", |
| 35 | + "Grilling steak requires high heat and proper timing", |
| 36 | + "Japanese sushi demands precise knife skills and fresh fish", |
| 37 | + "The ancient ruins attracted tourists from around the world", |
| 38 | + "Paris is known as the city of love and romance", |
| 39 | + "The tropical beach had crystal clear turquoise water", |
| 40 | + "Mountain climbers reached the summit after days of effort", |
| 41 | + "The bustling city never sleeps with its vibrant nightlife", |
| 42 | + "She felt overwhelming joy when she received the good news", |
| 43 | + "Heartbreak can feel like a physical pain in your chest", |
| 44 | + "Their friendship had lasted through decades of ups and downs", |
| 45 | + "Pride swelled in his chest as he watched his daughter graduate", |
| 46 | + "Anxiety crept in as the deadline approached rapidly", |
| 47 | + "The scientist conducted experiments to test the hypothesis", |
| 48 | + "Mathematics is the language of the universe", |
| 49 | + "Quantum physics challenges our understanding of reality", |
| 50 | + "DNA contains the genetic blueprint of all living organisms", |
| 51 | + "The theory of evolution explains the diversity of life", |
| 52 | + "The soccer team celebrated their championship victory", |
| 53 | + "Swimming is an excellent full-body cardiovascular workout", |
| 54 | + "The marathon runner crossed the finish line exhausted but proud", |
| 55 | + "Basketball requires both athleticism and strategic thinking", |
| 56 | + "Yoga combines physical poses with breathing and meditation", |
| 57 | + "The painter captured the sunset in brilliant orange and red hues", |
| 58 | + "Music has the power to evoke deep emotional responses", |
| 59 | + "The novelist spent years crafting the perfect ending", |
| 60 | + "Dance allows expression beyond what words can convey", |
| 61 | + "Photography freezes a single moment for eternity", |
| 62 | + "The startup raised millions in venture capital funding", |
| 63 | + "Effective leadership requires both vision and empathy", |
| 64 | + "The company announced record profits for the fiscal year", |
| 65 | + "Remote work has transformed the modern workplace", |
| 66 | + "Negotiation skills are essential for closing major deals", |
| 67 | + "Regular exercise reduces the risk of heart disease", |
| 68 | + "The doctor prescribed antibiotics for the bacterial infection", |
| 69 | + "Mental health is just as important as physical health", |
| 70 | + "Vaccines have saved millions of lives throughout history", |
| 71 | + "A balanced diet provides essential nutrients for the body", |
| 72 | +] |
| 73 | +categories = (["Tech"]*5 + ["Animals"]*5 + ["Food"]*5 + ["Travel"]*5 + |
| 74 | + ["Emotions"]*5 + ["Science"]*5 + ["Sports"]*5 + ["Art"]*5 + |
| 75 | + ["Business"]*5 + ["Health"]*5) |
| 76 | + |
| 77 | +# Generate embeddings |
| 78 | +model = SentenceTransformer("all-MiniLM-L6-v2") |
| 79 | +embeddings = model.encode(sentences, convert_to_numpy=True, normalize_embeddings=True) |
| 80 | +console.print(f"[green]Embeddings: {embeddings.shape}[/green]") |
| 81 | + |
| 82 | +# PCA |
| 83 | +pca = PCA(n_components=2, random_state=42) |
| 84 | +e2d = pca.fit_transform(embeddings) |
| 85 | +cat_colors = {"Tech":"#3b82f6","Animals":"#10b981","Food":"#f59e0b","Travel":"#8b5cf6", |
| 86 | + "Emotions":"#ef4444","Science":"#06b6d4","Sports":"#f97316","Art":"#ec4899", |
| 87 | + "Business":"#6366f1","Health":"#14b8a6"} |
| 88 | +fig, ax = plt.subplots(figsize=(16,11)) |
| 89 | +for cat in sorted(set(categories)): |
| 90 | + mask = [c==cat for c in categories] |
| 91 | + ax.scatter(e2d[mask,0], e2d[mask,1], c=cat_colors[cat], label=cat, alpha=0.75, s=120, edgecolors='white') |
| 92 | +ax.legend(ncol=2); ax.set_title("PCA: Text Embeddings"); plt.tight_layout() |
| 93 | +plt.savefig('01_pca.png', dpi=150); plt.close() |
| 94 | + |
| 95 | +# t-SNE |
| 96 | +tsne = TSNE(n_components=2, perplexity=8, random_state=42, max_iter=1000) |
| 97 | +e2dt = tsne.fit_transform(embeddings) |
| 98 | +fig, ax = plt.subplots(figsize=(16,11)) |
| 99 | +for cat in sorted(set(categories)): |
| 100 | + mask = [c==cat for c in categories] |
| 101 | + ax.scatter(e2dt[mask,0], e2dt[mask,1], c=cat_colors[cat], label=cat, alpha=0.75, s=120, edgecolors='white') |
| 102 | +ax.legend(ncol=2); ax.set_title("t-SNE: Text Embeddings"); plt.tight_layout() |
| 103 | +plt.savefig('02_tsne.png', dpi=150); plt.close() |
| 104 | + |
| 105 | +# Heatmap |
| 106 | +idx = [0,5,10,15,20,25,30,35,40,45] |
| 107 | +sim = cosine_similarity(embeddings[idx]) |
| 108 | +fig, ax = plt.subplots(figsize=(14,12)) |
| 109 | +sns.heatmap(sim, annot=True, fmt=".2f", cmap="YlOrRd", vmin=0, vmax=1, ax=ax) |
| 110 | +ax.set_title("Cosine Similarity Heatmap"); plt.tight_layout() |
| 111 | +plt.savefig('03_heatmap.png', dpi=150); plt.close() |
| 112 | + |
| 113 | +# Semantic similarity demo |
| 114 | +pairs = [("The dog played in the park","A canine ran through the green field"), |
| 115 | + ("The dog played in the park","The stock market crashed yesterday"), |
| 116 | + ("I love eating pizza and pasta","Italian cuisine is my favorite food"), |
| 117 | + ("I love eating pizza and pasta","The spaceship launched into orbit")] |
| 118 | +for a,b in pairs: |
| 119 | + ea = model.encode([a], normalize_embeddings=True)[0] |
| 120 | + eb = model.encode([b], normalize_embeddings=True)[0] |
| 121 | + sim = float(np.dot(ea,eb)) |
| 122 | + rel = "SAME" if sim > 0.5 else "DIFF" |
| 123 | + console.print(f" [{rel}] {sim*100:.1f}% — {a[:40]} <-> {b[:40]}") |
| 124 | + |
| 125 | +console.print("[green]Analysis complete![/green]") |
0 commit comments