transformer / index.html
manmonth's picture
Add 2 files
d05af11 verified
Raw History Blame Contribute Delete
28.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transformer Translation Visualization</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@keyframes pulse {
0%, 100% { opacity: 0.5; }
50% { opacity: 1; }
}
@keyframes attention-beam {
0% { width: 0; opacity: 0; }
50% { opacity: 1; }
100% { width: 100%; opacity: 0; }
}
@keyframes vector-transform {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
.pulse-animation {
animation: pulse 2s infinite;
}
.attention-beam {
position: absolute;
height: 2px;
background: linear-gradient(90deg, transparent, #3b82f6, transparent);
animation: attention-beam 1.5s forwards;
}
.vector-transform {
animation: vector-transform 1s ease-in-out;
}
.position-encoding {
background: repeating-linear-gradient(
45deg,
rgba(59, 130, 246, 0.1),
rgba(59, 130, 246, 0.1) 10px,
rgba(99, 102, 241, 0.1) 10px,
rgba(99, 102, 241, 0.1) 20px
);
}
.vector-box {
transition: all 0.5s ease;
}
.cursor-blink {
animation: blink 1s step-end infinite;
}
@keyframes blink {
from, to { opacity: 0; }
50% { opacity: 1; }
}
</style>
</head>
<body class="bg-gray-50 min-h-screen font-sans">
<div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold text-center text-blue-800 mb-8">Transformer Translation Visualization</h1>
<p class="text-center text-gray-600 mb-8">Watch how the Transformer translates "我喜欢吃中国菜" to "I like Chinese food"</p>
<div class="bg-white rounded-xl shadow-lg p-6 mb-8">
<!-- Progress Controls -->
<div class="flex justify-center space-x-4 mb-6">
<button id="prevBtn" class="px-4 py-2 bg-gray-200 rounded-lg hover:bg-gray-300 disabled:opacity-50">
<i class="fas fa-arrow-left mr-2"></i>Previous
</button>
<button id="nextBtn" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
Next Step<i class="fas fa-arrow-right ml-2"></i>
</button>
<button id="resetBtn" class="px-4 py-2 bg-red-100 text-red-700 rounded-lg hover:bg-red-200">
<i class="fas fa-redo mr-2"></i>Reset
</button>
</div>
<!-- Progress Bar -->
<div class="w-full bg-gray-200 rounded-full h-2.5 mb-6">
<div id="progressBar" class="bg-blue-600 h-2.5 rounded-full" style="width: 0%"></div>
</div>
<!-- Step Indicator -->
<div class="text-center mb-6">
<span id="stepIndicator" class="px-4 py-2 bg-blue-100 text-blue-800 rounded-full text-sm font-medium">Step 0: Initial Chinese Sentence</span>
</div>
<!-- Visualization Area -->
<div class="relative min-h-64 border-2 border-gray-200 rounded-lg p-4 mb-4">
<!-- This is where the visualization will be rendered -->
<div id="visualization" class="h-full">
<!-- Initial state will be rendered here by JavaScript -->
</div>
</div>
<!-- Explanation -->
<div id="explanation" class="bg-blue-50 p-4 rounded-lg">
<p class="text-blue-800">The animation will start when you click "Next Step". Follow along to see how the Transformer model processes the translation.</p>
</div>
</div>
</div>
<script>
// Animation steps data
const steps = [
{
title: "Initial Chinese Sentence",
description: "The Chinese sentence to be translated: 我喜欢吃中国菜 (Wǒ xǐhuān chī Zhōngguó cài - I like to eat Chinese food)",
render: function() {
return `
<div class="flex justify-center items-center h-48">
<div class="text-center">
<div class="text-3xl font-bold text-gray-800 mb-4">我喜欢吃中国菜</div>
<div class="flex justify-center space-x-2">
<div class="w-16 h-16 bg-red-100 rounded-lg flex items-center justify-center text-red-800">我</div>
<div class="w-16 h-16 bg-green-100 rounded-lg flex items-center justify-center text-green-800">喜欢</div>
<div class="w-16 h-16 bg-blue-100 rounded-lg flex items-center justify-center text-blue-800">吃</div>
<div class="w-16 h-16 bg-purple-100 rounded-lg flex items-center justify-center text-purple-800">中国菜</div>
</div>
</div>
</div>
`;
}
},
{
title: "Step 1: Word Embeddings",
description: "Each Chinese word is converted to a numerical vector (word embedding) that captures its initial meaning.",
render: function() {
return `
<div class="flex flex-col items-center h-48">
<div class="text-xl text-gray-700 mb-4">Converting words to vectors</div>
<div class="flex justify-center space-x-4">
<div class="flex flex-col items-center">
<div class="w-12 h-12 bg-red-100 rounded-lg mb-2 flex items-center justify-center text-red-800">我</div>
<div class="vector-box w-16 h-24 bg-gradient-to-b from-red-200 to-red-300 rounded-lg vector-transform"></div>
</div>
<div class="flex flex-col items-center">
<div class="w-12 h-12 bg-green-100 rounded-lg mb-2 flex items-center justify-center text-green-800">喜欢</div>
<div class="vector-box w-16 h-24 bg-gradient-to-b from-green-200 to-green-300 rounded-lg vector-transform"></div>
</div>
<div class="flex flex-col items-center">
<div class="w-12 h-12 bg-blue-100 rounded-lg mb-2 flex items-center justify-center text-blue-800">吃</div>
<div class="vector-box w-16 h-24 bg-gradient-to-b from-blue-200 to-blue-300 rounded-lg vector-transform"></div>
</div>
<div class="flex flex-col items-center">
<div class="w-12 h-12 bg-purple-100 rounded-lg mb-2 flex items-center justify-center text-purple-800">中国菜</div>
<div class="vector-box w-16 h-24 bg-gradient-to-b from-purple-200 to-purple-300 rounded-lg vector-transform"></div>
</div>
</div>
</div>
`;
}
},
{
title: "Step 2: Positional Encoding",
description: "Positional encoding is added to each word vector to give the model information about word order in the sentence.",
render: function() {
return `
<div class="flex flex-col items-center h-48">
<div class="text-xl text-gray-700 mb-4">Adding positional information</div>
<div class="flex justify-center space-x-4">
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-red-200 to-red-300 rounded-lg position-encoding relative overflow-hidden">
<div class="absolute inset-0 flex items-center justify-center text-xs">Pos 1</div>
</div>
</div>
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-green-200 to-green-300 rounded-lg position-encoding relative overflow-hidden">
<div class="absolute inset-0 flex items-center justify-center text-xs">Pos 2</div>
</div>
</div>
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-blue-200 to-blue-300 rounded-lg position-encoding relative overflow-hidden">
<div class="absolute inset-0 flex items-center justify-center text-xs">Pos 3</div>
</div>
</div>
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-purple-200 to-purple-300 rounded-lg position-encoding relative overflow-hidden">
<div class="absolute inset-0 flex items-center justify-center text-xs">Pos 4</div>
</div>
</div>
</div>
</div>
`;
}
},
{
title: "Step 3-4: Self-Attention Mechanism",
description: "The model calculates relationships between words. '喜欢' strongly relates to '我' (subject) and '吃' (action). '吃' strongly relates to '中国菜' (object).",
render: function() {
return `
<div class="flex flex-col items-center h-64">
<div class="text-xl text-gray-700 mb-4">Calculating word relationships</div>
<div class="flex justify-center space-x-6 relative">
<!-- 我 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-red-200 to-red-300 rounded-lg relative" id="vector-wo">
<div class="absolute inset-0 flex items-center justify-center text-xs">我</div>
</div>
</div>
<!-- 喜欢 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-green-200 to-green-300 rounded-lg relative" id="vector-xihuan">
<div class="absolute inset-0 flex items-center justify-center text-xs">喜欢</div>
</div>
</div>
<!-- 吃 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-blue-200 to-blue-300 rounded-lg relative" id="vector-chi">
<div class="absolute inset-0 flex items-center justify-center text-xs">吃</div>
</div>
</div>
<!-- 中国菜 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-purple-200 to-purple-300 rounded-lg relative" id="vector-food">
<div class="absolute inset-0 flex items-center justify-center text-xs">中国菜</div>
</div>
</div>
</div>
<div class="mt-4 text-sm text-gray-600">
<div class="mb-1"><span class="font-semibold">喜欢</span> attends to <span class="font-semibold">我</span> and <span class="font-semibold">吃</span></div>
<div><span class="font-semibold">吃</span> attends to <span class="font-semibold">中国菜</span></div>
</div>
</div>
`;
},
afterRender: function() {
// Add attention beams after elements are rendered
setTimeout(() => {
// 喜欢 -> 我
createAttentionBeam('vector-xihuan', 'vector-wo', 'strong');
// 喜欢 -> 吃
createAttentionBeam('vector-xihuan', 'vector-chi', 'strong');
// 吃 -> 中国菜
createAttentionBeam('vector-chi', 'vector-food', 'strong');
}, 500);
}
},
{
title: "Step 5: Multi-Head Attention",
description: "Multiple attention heads analyze relationships from different perspectives simultaneously, capturing richer context.",
render: function() {
return `
<div class="flex flex-col items-center h-64">
<div class="text-xl text-gray-700 mb-4">Multiple attention perspectives</div>
<div class="flex justify-center space-x-6 relative">
<!-- 我 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-red-200 to-red-300 rounded-lg relative" id="mh-vector-wo">
<div class="absolute inset-0 flex items-center justify-center text-xs">我</div>
</div>
</div>
<!-- 喜欢 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-green-200 to-green-300 rounded-lg relative" id="mh-vector-xihuan">
<div class="absolute inset-0 flex items-center justify-center text-xs">喜欢</div>
</div>
</div>
<!-- 吃 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-blue-200 to-blue-300 rounded-lg relative" id="mh-vector-chi">
<div class="absolute inset-0 flex items-center justify-center text-xs">吃</div>
</div>
</div>
<!-- 中国菜 -->
<div class="flex flex-col items-center">
<div class="vector-box w-16 h-24 bg-gradient-to-b from-purple-200 to-purple-300 rounded-lg relative" id="mh-vector-food">
<div class="absolute inset-0 flex items-center justify-center text-xs">中国菜</div>
</div>
</div>
</div>
<div class="mt-4 text-sm text-gray-600">
Different colored beams represent different attention heads analyzing relationships
</div>
</div>
`;
},
afterRender: function() {
// Add multi-head attention beams after elements are rendered
setTimeout(() => {
// Head 1 (blue)
createAttentionBeam('mh-vector-xihuan', 'mh-vector-wo', 'blue');
createAttentionBeam('mh-vector-xihuan', 'mh-vector-chi', 'blue');
createAttentionBeam('mh-vector-chi', 'mh-vector-food', 'blue');
// Head 2 (red)
setTimeout(() => {
createAttentionBeam('mh-vector-xihuan', 'mh-vector-wo', 'red');
createAttentionBeam('mh-vector-chi', 'mh-vector-food', 'red');
}, 300);
// Head 3 (green)
setTimeout(() => {
createAttentionBeam('mh-vector-xihuan', 'mh-vector-wo', 'green');
createAttentionBeam('mh-vector-xihuan', 'mh-vector-chi', 'green');
}, 600);
}, 500);
}
},
{
title: "Step 6: Encoder Output",
description: "The encoder completes its work, generating a contextual representation containing the meaning of the entire Chinese sentence.",
render: function() {
return `
<div class="flex flex-col items-center justify-center h-48">
<div class="text-xl text-gray-700 mb-4">Encoder contextual representation</div>
<div class="w-32 h-32 bg-gradient-to-br from-blue-400 to-indigo-600 rounded-lg flex items-center justify-center text-white font-bold text-sm text-center p-2 vector-transform">
Contextual Representation
</div>
</div>
`;
}
},
{
title: "Step 7: Decoder Generation",
description: "The decoder uses the encoder's understanding and generates English translation word by word.",
render: function() {
return `
<div class="flex flex-col items-center h-64">
<div class="text-xl text-gray-700 mb-4">Generating English translation</div>
<div class="flex w-full h-full">
<!-- Encoder side -->
<div class="w-1/2 flex flex-col items-center justify-center border-r border-gray-200">
<div class="text-sm text-gray-500 mb-2">Encoder Output</div>
<div class="w-24 h-24 bg-gradient-to-br from-blue-400 to-indigo-600 rounded-lg flex items-center justify-center text-white text-xs text-center p-1">
Context
</div>
</div>
<!-- Decoder side -->
<div class="w-1/2 flex flex-col items-center justify-center relative">
<div class="text-sm text-gray-500 mb-2">Decoder Output</div>
<div id="decoder-output" class="text-2xl font-bold text-gray-800 h-12 flex items-center">
<span id="decoder-text"></span>
<span id="decoder-cursor" class="cursor-blink bg-blue-600 w-1 h-6 ml-1"></span>
</div>
</div>
</div>
</div>
`;
},
afterRender: function() {
const decoderText = document.getElementById('decoder-text');
const words = ['I', 'like', 'Chinese food'];
let currentWord = 0;
const typeWriter = () => {
if (currentWord < words.length) {
// Add word with typing effect
const word = words[currentWord];
let i = 0;
const typing = setInterval(() => {
if (i < word.length) {
decoderText.textContent += word.charAt(i);
i++;
} else {
clearInterval(typing);
decoderText.textContent += ' ';
currentWord++;
setTimeout(typeWriter, 500);
}
}, 100);
}
};
setTimeout(typeWriter, 500);
}
},
{
title: "Translation Complete",
description: "The Transformer has successfully translated the Chinese sentence to English: I like Chinese food",
render: function() {
return `
<div class="flex flex-col items-center justify-center h-48">
<div class="text-3xl font-bold text-gray-800 mb-6">我喜欢吃中国菜</div>
<div class="flex items-center justify-center mb-6">
<i class="fas fa-arrow-down text-2xl text-gray-400 mx-4"></i>
</div>
<div class="text-3xl font-bold text-blue-600">I like Chinese food</div>
</div>
`;
}
}
];
// Current step index
let currentStep = 0;
// DOM elements
const visualizationEl = document.getElementById('visualization');
const explanationEl = document.getElementById('explanation');
const stepIndicatorEl = document.getElementById('stepIndicator');
const progressBarEl = document.getElementById('progressBar');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const resetBtn = document.getElementById('resetBtn');
// Initialize
renderStep();
// Event listeners
nextBtn.addEventListener('click', nextStep);
prevBtn.addEventListener('click', prevStep);
resetBtn.addEventListener('click', reset);
// Functions
function renderStep() {
// Update UI
stepIndicatorEl.textContent = `Step ${currentStep}: ${steps[currentStep].title}`;
explanationEl.innerHTML = `<p class="text-blue-800">${steps[currentStep].description}</p>`;
progressBarEl.style.width = `${(currentStep / (steps.length - 1)) * 100}%`;
// Update button states
prevBtn.disabled = currentStep === 0;
nextBtn.disabled = currentStep === steps.length - 1;
// Render visualization
visualizationEl.innerHTML = steps[currentStep].render();
// Call afterRender if defined
if (steps[currentStep].afterRender) {
steps[currentStep].afterRender();
}
}
function nextStep() {
if (currentStep < steps.length - 1) {
currentStep++;
renderStep();
}
}
function prevStep() {
if (currentStep > 0) {
currentStep--;
renderStep();
}
}
function reset() {
currentStep = 0;
renderStep();
}
function createAttentionBeam(fromId, toId, strength) {
const fromEl = document.getElementById(fromId);
const toEl = document.getElementById(toId);
if (!fromEl || !toEl) return;
const fromRect = fromEl.getBoundingClientRect();
const toRect = toEl.getBoundingClientRect();
const visualizationRect = visualizationEl.getBoundingClientRect();
// Calculate positions relative to visualization container
const fromX = fromRect.left + fromRect.width / 2 - visualizationRect.left;
const fromY = fromRect.top + fromRect.height / 2 - visualizationRect.top;
const toX = toRect.left + toRect.width / 2 - visualizationRect.left;
const toY = toRect.top + toRect.height / 2 - visualizationRect.top;
// Calculate distance and angle
const dx = toX - fromX;
const dy = toY - fromY;
const distance = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx) * 180 / Math.PI;
// Create beam element
const beam = document.createElement('div');
beam.className = 'attention-beam';
// Set beam styles based on strength
if (strength === 'strong') {
beam.style.height = '3px';
beam.style.background = 'linear-gradient(90deg, transparent, #3b82f6, transparent)';
} else if (strength === 'blue') {
beam.style.height = '2px';
beam.style.background = 'linear-gradient(90deg, transparent, #60a5fa, transparent)';
} else if (strength === 'red') {
beam.style.height = '2px';
beam.style.background = 'linear-gradient(90deg, transparent, #f87171, transparent)';
} else if (strength === 'green') {
beam.style.height = '2px';
beam.style.background = 'linear-gradient(90deg, transparent, #4ade80, transparent)';
}
// Position the beam
beam.style.width = `${distance}px`;
beam.style.left = `${fromX}px`;
beam.style.top = `${fromY}px`;
beam.style.transformOrigin = '0 0';
beam.style.transform = `rotate(${angle}deg)`;
// Add to visualization
visualizationEl.appendChild(beam);
// Remove beam after animation
setTimeout(() => {
beam.remove();
}, 1500);
}
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=manmonth/transformer" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>