{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Embedding Visualization\n", "\n", "## Overview\n", "\n", "This notebook demonstrates how to generate embeddings, optimize them, and visualize using t-SNE, PCA, and UMAP dimensionality reduction techniques.\n", "\n", "### Learning Objectives\n", "\n", "- Generate embeddings for documents\n", "- Optimize embeddings for better quality\n", "- Visualize embeddings using t-SNE\n", "- Visualize embeddings using PCA\n", "- Visualize embeddings using UMAP\n", "\n", "---\n", "\n", "## Workflow\n", "\n", "**Generate Embeddings → Optimize → Visualize**\n", "\n", "Visualization helps understand the structure and relationships in embedding spaces.\n", "\n", "---\n", "\n", "## Step 1: Generate Embeddings\n", "\n", "Start by generating embeddings for your documents.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.embeddings import EmbeddingGenerator\n", "import numpy as np\n", "\n", "documents = [\n", " \"Machine learning algorithms\",\n", " \"Deep neural networks\",\n", " \"Natural language processing\",\n", " \"Computer vision\",\n", " \"Reinforcement learning\",\n", "]\n", "\n", "generator = EmbeddingGenerator()\n", "\n", "try:\n", " embeddings = generator.generate(documents)\n", " print(\"✓ Embeddings generated\")\n", " print(f\" Documents: {len(documents)}\")\n", " print(f\" Embedding dimension: {embeddings.shape[1] if hasattr(embeddings, 'shape') else 'N/A'}\")\n", " \n", "except Exception as e:\n", " print(f\"✗ Error generating embeddings: {e}\")\n", " embeddings = np.random.rand(len(documents), 1536).astype(np.float32)\n", " print(\" Using demo embeddings\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Optimize Embeddings\n", "\n", "Optimize embeddings to improve their quality and reduce noise.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.embeddings import EmbeddingOptimizer\n", "\n", "optimizer = EmbeddingOptimizer()\n", "\n", "try:\n", " optimized_embeddings = optimizer.optimize(embeddings)\n", " print(\"✓ Embeddings optimized\")\n", " print(f\" Optimized embeddings ready for visualization\")\n", " \n", "except Exception as e:\n", " print(f\"✗ Error optimizing embeddings: {e}\")\n", " optimized_embeddings = embeddings\n", " print(\" Using original embeddings\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Visualize with t-SNE\n", "\n", "Use t-SNE (t-Distributed Stochastic Neighbor Embedding) to visualize embeddings in 2D space.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.visualization import EmbeddingVisualizer\n", "\n", "visualizer = EmbeddingVisualizer()\n", "\n", "labels = [f\"Doc {i+1}\" for i in range(len(documents))]\n", "\n", "try:\n", " visualizer.visualize_tsne(optimized_embeddings, labels)\n", " print(\"✓ t-SNE visualization complete\")\n", " print(\" Note: t-SNE shows local structure and clusters in embedding space\")\n", " \n", "except Exception as e:\n", " print(f\"✗ Error visualizing with t-SNE: {e}\")\n", " print(\" Note: t-SNE reduces high-dimensional embeddings to 2D for visualization\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Visualize with PCA\n", "\n", "Use PCA (Principal Component Analysis) to visualize embeddings, preserving global structure.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " visualizer.visualize_pca(optimized_embeddings, labels)\n", " print(\"✓ PCA visualization complete\")\n", " print(\" Note: PCA preserves global structure and variance\")\n", " \n", "except Exception as e:\n", " print(f\"✗ Error visualizing with PCA: {e}\")\n", " print(\" Note: PCA reduces dimensions while preserving maximum variance\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5: Visualize with UMAP\n", "\n", "Use UMAP (Uniform Manifold Approximation and Projection) for a balance between local and global structure.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " visualizer.visualize_umap(optimized_embeddings, labels)\n", " print(\"✓ UMAP visualization complete\")\n", " print(\" Note: UMAP balances local and global structure preservation\")\n", " print(\"\\n✓ Embedding visualization complete\")\n", " print(\" All visualization methods demonstrate different aspects of embedding space\")\n", " \n", "except Exception as e:\n", " print(f\"✗ Error visualizing with UMAP: {e}\")\n", " print(\" Note: UMAP provides a good balance between t-SNE and PCA\")\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }