给不懂大语言模型的人讲解人工智能,参照https://poloclub.github.io/transformer-explainer/,如何通过交互、可视化的方式讲解trandformer架构、注意力机制、文本生成体验,从而快速理解大模型的原理。如果需要调用大模型,使用硅基流动的api进行模型的调用。调用D3.js和Plotly.js 等相关进行演示 这个交互、可视化的网站部署环境是windows11,面向小白详细讲解可视化的交互教学网站的部署, - Initial Deployment
Browse files- README.md +7 -5
- index.html +742 -19
README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ai
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
tags:
|
| 9 |
+
- deepsite
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
|
@@ -1,19 +1,742 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="zh-CN">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Transformer架构交互式教学</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<script src="https://d3js.org/d3.v7.min.js"></script>
|
| 9 |
+
<script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script>
|
| 10 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
| 11 |
+
<script>
|
| 12 |
+
tailwind.config = {
|
| 13 |
+
theme: {
|
| 14 |
+
extend: {
|
| 15 |
+
colors: {
|
| 16 |
+
primary: '#4F46E5',
|
| 17 |
+
secondary: '#818CF8',
|
| 18 |
+
accent: '#F472B6',
|
| 19 |
+
dark: '#1E293B',
|
| 20 |
+
light: '#F1F5F9'
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
</script>
|
| 26 |
+
<style>
|
| 27 |
+
.attention-node {
|
| 28 |
+
transition: all 0.3s ease;
|
| 29 |
+
cursor: pointer;
|
| 30 |
+
}
|
| 31 |
+
.attention-node:hover {
|
| 32 |
+
stroke-width: 3px;
|
| 33 |
+
stroke: #F472B6;
|
| 34 |
+
}
|
| 35 |
+
.attention-line {
|
| 36 |
+
stroke-opacity: 0.3;
|
| 37 |
+
transition: stroke-opacity 0.3s ease;
|
| 38 |
+
}
|
| 39 |
+
.attention-line:hover {
|
| 40 |
+
stroke-opacity: 0.8;
|
| 41 |
+
}
|
| 42 |
+
.token-highlight {
|
| 43 |
+
background-color: rgba(79, 70, 229, 0.2);
|
| 44 |
+
transition: all 0.3s ease;
|
| 45 |
+
}
|
| 46 |
+
.fade-in {
|
| 47 |
+
animation: fadeIn 0.5s ease-in;
|
| 48 |
+
}
|
| 49 |
+
@keyframes fadeIn {
|
| 50 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 51 |
+
to { opacity: 1; transform: translateY(0); }
|
| 52 |
+
}
|
| 53 |
+
.architecture-diagram {
|
| 54 |
+
background-image: linear-gradient(to right, #F1F5F9 1px, transparent 1px),
|
| 55 |
+
linear-gradient(to bottom, #F1F5F9 1px, transparent 1px);
|
| 56 |
+
background-size: 30px 30px;
|
| 57 |
+
}
|
| 58 |
+
</style>
|
| 59 |
+
</head>
|
| 60 |
+
<body class="bg-light text-dark font-sans">
|
| 61 |
+
<!-- 导航栏 -->
|
| 62 |
+
<nav class="bg-primary text-white shadow-lg">
|
| 63 |
+
<div class="container mx-auto px-4 py-3 flex justify-between items-center">
|
| 64 |
+
<div class="flex items-center space-x-2">
|
| 65 |
+
<i class="fas fa-brain text-2xl"></i>
|
| 66 |
+
<h1 class="text-xl font-bold">Transformer架构交互式教学</h1>
|
| 67 |
+
</div>
|
| 68 |
+
<div class="flex space-x-4">
|
| 69 |
+
<a href="#architecture" class="hover:text-secondary transition">架构</a>
|
| 70 |
+
<a href="#attention" class="hover:text-secondary transition">注意力机制</a>
|
| 71 |
+
<a href="#generation" class="hover:text-secondary transition">文本生成</a>
|
| 72 |
+
<a href="#deployment" class="hover:text-secondary transition">部署指南</a>
|
| 73 |
+
</div>
|
| 74 |
+
</div>
|
| 75 |
+
</nav>
|
| 76 |
+
|
| 77 |
+
<!-- 介绍部分 -->
|
| 78 |
+
<section class="py-16 bg-gradient-to-br from-primary to-secondary text-white">
|
| 79 |
+
<div class="container mx-auto px-4 text-center">
|
| 80 |
+
<h2 class="text-4xl font-bold mb-6">探索Transformer架构的奥秘</h2>
|
| 81 |
+
<p class="text-xl max-w-3xl mx-auto mb-8">
|
| 82 |
+
通过交互式可视化,直观理解大语言模型的核心原理 - Transformer架构和注意力机制
|
| 83 |
+
</p>
|
| 84 |
+
<div class="flex justify-center space-x-4">
|
| 85 |
+
<a href="#architecture" class="bg-white text-primary px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">开始探索</a>
|
| 86 |
+
<a href="#generation" class="bg-accent text-white px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">体验文本生成</a>
|
| 87 |
+
</div>
|
| 88 |
+
</div>
|
| 89 |
+
</section>
|
| 90 |
+
|
| 91 |
+
<!-- Transformer架构可视化 -->
|
| 92 |
+
<section id="architecture" class="py-16 bg-white">
|
| 93 |
+
<div class="container mx-auto px-4">
|
| 94 |
+
<h2 class="text-3xl font-bold text-center mb-4">Transformer架构解析</h2>
|
| 95 |
+
<p class="text-center text-gray-600 max-w-3xl mx-auto mb-12">
|
| 96 |
+
Transformer是当今大语言模型的核心架构,由编码器和解码器组成,通过自注意力机制处理序列数据
|
| 97 |
+
</p>
|
| 98 |
+
|
| 99 |
+
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8 items-center">
|
| 100 |
+
<div>
|
| 101 |
+
<div class="architecture-diagram bg-white rounded-xl shadow-lg p-4 h-96" id="transformer-viz"></div>
|
| 102 |
+
</div>
|
| 103 |
+
<div>
|
| 104 |
+
<div class="bg-gray-50 rounded-xl p-6 shadow">
|
| 105 |
+
<h3 class="text-xl font-semibold mb-4">核心组件</h3>
|
| 106 |
+
<ul class="space-y-3">
|
| 107 |
+
<li class="flex items-start">
|
| 108 |
+
<i class="fas fa-cube text-secondary mt-1 mr-3"></i>
|
| 109 |
+
<div>
|
| 110 |
+
<strong>输入嵌入层</strong> - 将单词转换为向量表示
|
| 111 |
+
</div>
|
| 112 |
+
</li>
|
| 113 |
+
<li class="flex items-start">
|
| 114 |
+
<i class="fas fa-project-diagram text-secondary mt-1 mr-3"></i>
|
| 115 |
+
<div>
|
| 116 |
+
<strong>位置编码</strong> - 为输入添加位置信息
|
| 117 |
+
</div>
|
| 118 |
+
</li>
|
| 119 |
+
<li class="flex items-start">
|
| 120 |
+
<i class="fas fa-bullseye text-accent mt-1 mr-3"></i>
|
| 121 |
+
<div>
|
| 122 |
+
<strong>多头注意力</strong> - 模型的核心机制,计算单词间相关性
|
| 123 |
+
</div>
|
| 124 |
+
</li>
|
| 125 |
+
<li class="flex items-start">
|
| 126 |
+
<i class="fas fa-layer-group text-secondary mt-1 mr-3"></i>
|
| 127 |
+
<div>
|
| 128 |
+
<strong>前馈神经网络</strong> - 对注意力结果进行非线性变换
|
| 129 |
+
</div>
|
| 130 |
+
</li>
|
| 131 |
+
<li class="flex items-start">
|
| 132 |
+
<i class="fas fa-code-branch text-secondary mt-1 mr-3"></i>
|
| 133 |
+
<div>
|
| 134 |
+
<strong>残差连接和层归一化</strong> - 稳定训练过程
|
| 135 |
+
</div>
|
| 136 |
+
</li>
|
| 137 |
+
</ul>
|
| 138 |
+
|
| 139 |
+
<div class="mt-8">
|
| 140 |
+
<h4 class="font-semibold mb-2">探索架构</h4>
|
| 141 |
+
<div class="flex flex-wrap gap-2">
|
| 142 |
+
<button onclick="highlightComponent('encoder')" class="bg-secondary text-white px-3 py-1 rounded hover:bg-opacity-90 transition">编码器</button>
|
| 143 |
+
<button onclick="highlightComponent('decoder')" class="bg-accent text-white px-3 py-1 rounded hover:bg-opacity-90 transition">解码器</button>
|
| 144 |
+
<button onclick="highlightComponent('attention')" class="bg-primary text-white px-3 py-1 rounded hover:bg-opacity-90 transition">注意力层</button>
|
| 145 |
+
<button onclick="resetHighlight()" class="bg-gray-300 text-gray-700 px-3 py-1 rounded hover:bg-gray-400 transition">重置</button>
|
| 146 |
+
</div>
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
</div>
|
| 150 |
+
</div>
|
| 151 |
+
</div>
|
| 152 |
+
</section>
|
| 153 |
+
|
| 154 |
+
<!-- 注意力机制可视化 -->
|
| 155 |
+
<section id="attention" class="py-16 bg-gray-50">
|
| 156 |
+
<div class="container mx-auto px-4">
|
| 157 |
+
<h2 class="text-3xl font-bold text-center mb-4">注意力机制详解</h2>
|
| 158 |
+
<p class="text-center text-gray-600 max-w-3xl mx-auto mb-12">
|
| 159 |
+
注意力机制让模型能够"关注"输入序列中不同部分之间的关系,是Transformer的核心创新
|
| 160 |
+
</p>
|
| 161 |
+
|
| 162 |
+
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
|
| 163 |
+
<div class="bg-white rounded-xl shadow-lg p-4">
|
| 164 |
+
<h3 class="text-xl font-semibold mb-4">注意力权重可视化</h3>
|
| 165 |
+
<div id="attention-heatmap" class="h-80"></div>
|
| 166 |
+
<div class="mt-4">
|
| 167 |
+
<label class="block text-sm font-medium mb-2">输入文本</label>
|
| 168 |
+
<input type="text" id="attention-text" value="人工智能正在改变世界" class="w-full p-2 border rounded">
|
| 169 |
+
<button onclick="updateAttentionViz()" class="mt-3 bg-primary text-white px-4 py-2 rounded hover:bg-opacity-90 transition">计算注意力</button>
|
| 170 |
+
</div>
|
| 171 |
+
</div>
|
| 172 |
+
|
| 173 |
+
<div>
|
| 174 |
+
<div class="bg-white rounded-xl shadow-lg p-6 mb-6">
|
| 175 |
+
<h3 class="text-xl font-semibold mb-4">注意力机制如何工作</h3>
|
| 176 |
+
<p class="mb-4">注意力机制通过三个步骤计算单词之间的相关性:</p>
|
| 177 |
+
<ol class="list-decimal pl-6 space-y-2">
|
| 178 |
+
<li><strong>查询(Query)</strong> - 当前处理的单词</li>
|
| 179 |
+
<li><strong>键(Key)</strong> - 序列中的所有单词</li>
|
| 180 |
+
<li><strong>值(Value)</strong> - 每个键对应的内容</li>
|
| 181 |
+
</ol>
|
| 182 |
+
<div class="mt-4 p-4 bg-blue-50 rounded-lg">
|
| 183 |
+
<p class="font-mono text-sm">注意力分数 = Softmax(Query • Key<sup>T</sup> / √d<sub>k</sub>) • Value</p>
|
| 184 |
+
</div>
|
| 185 |
+
</div>
|
| 186 |
+
|
| 187 |
+
<div class="bg-white rounded-xl shadow-lg p-6">
|
| 188 |
+
<h3 class="text-xl font-semibold mb-4">多头注意力</h3>
|
| 189 |
+
<p>Transformer使用多个"注意力头",每个头学习不同的表示方式,使模型能够捕捉不同方面的关系。</p>
|
| 190 |
+
<div class="mt-4 flex justify-center">
|
| 191 |
+
<div class="relative">
|
| 192 |
+
<div class="w-12 h-12 bg-secondary rounded-full flex items-center justify-center text-white">头1</div>
|
| 193 |
+
<div class="w-12 h-12 bg-primary rounded-full flex items-center justify-center text-white absolute top-0 left-14">头2</div>
|
| 194 |
+
<div class="w-12 h-12 bg-accent rounded-full flex items-center justify-center text-white absolute top-0 left-28">头3</div>
|
| 195 |
+
<div class="w-12 h-12 bg-purple-500 rounded-full flex items-center justify-center text-white absolute top-0 left-42">头4</div>
|
| 196 |
+
</div>
|
| 197 |
+
</div>
|
| 198 |
+
</div>
|
| 199 |
+
</div>
|
| 200 |
+
</div>
|
| 201 |
+
</div>
|
| 202 |
+
</section>
|
| 203 |
+
|
| 204 |
+
<!-- 文本生成体验 -->
|
| 205 |
+
<section id="generation" class="py-16 bg-white">
|
| 206 |
+
<div class="container mx-auto px-4">
|
| 207 |
+
<h2 class="text-3xl font-bold text-center mb-4">文本生成体验</h2>
|
| 208 |
+
<p class="text-center text-gray-600 max-w-3xl mx-auto mb-12">
|
| 209 |
+
体验Transformer模型如何生成文本,并可视化生成过程中的注意力机制
|
| 210 |
+
</p>
|
| 211 |
+
|
| 212 |
+
<div class="max-w-4xl mx-auto bg-gray-50 rounded-xl shadow-lg p-6">
|
| 213 |
+
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
|
| 214 |
+
<div>
|
| 215 |
+
<label class="block text-sm font-medium mb-2">选择模型</label>
|
| 216 |
+
<select id="model-select" class="w-full p-2 border rounded">
|
| 217 |
+
<option value="gpt-mini">硅基流动-GPT-Mini</option>
|
| 218 |
+
<option value="gpt-medium">硅基流动-GPT-Medium</option>
|
| 219 |
+
</select>
|
| 220 |
+
</div>
|
| 221 |
+
<div>
|
| 222 |
+
<label class="block text-sm font-medium mb-2">生成长度</label>
|
| 223 |
+
<input type="range" id="length-slider" min="20" max="100" value="50" class="w-full">
|
| 224 |
+
<div class="text-center text-sm"><span id="length-value">50</span> tokens</div>
|
| 225 |
+
</div>
|
| 226 |
+
<div>
|
| 227 |
+
<label class="block text-sm font-medium mb-2">温度</label>
|
| 228 |
+
<input type="range" id="temp-slider" min="0.1" max="1.0" step="0.1" value="0.7" class="w-full">
|
| 229 |
+
<div class="text-center text-sm"><span id="temp-value">0.7</span></div>
|
| 230 |
+
</div>
|
| 231 |
+
</div>
|
| 232 |
+
|
| 233 |
+
<div class="mb-6">
|
| 234 |
+
<label class="block text-sm font-medium mb-2">输入提示</label>
|
| 235 |
+
<textarea id="prompt-input" class="w-full p-3 border rounded h-32">人工智能是</textarea>
|
| 236 |
+
</div>
|
| 237 |
+
|
| 238 |
+
<div class="flex justify-center mb-6">
|
| 239 |
+
<button id="generate-btn" class="bg-primary text-white px-6 py-3 rounded-lg font-semibold hover:bg-opacity-90 transition flex items-center">
|
| 240 |
+
<i class="fas fa-bolt mr-2"></i>生成文本
|
| 241 |
+
</button>
|
| 242 |
+
</div>
|
| 243 |
+
|
| 244 |
+
<div class="bg-white rounded-lg shadow p-4 mb-6">
|
| 245 |
+
<h3 class="font-semibold mb-2">生成结果</h3>
|
| 246 |
+
<div id="generated-text" class="min-h-32 p-3 bg-gray-100 rounded">
|
| 247 |
+
<p class="text-gray-500">点击"生成文本"按钮查看结果...</p>
|
| 248 |
+
</div>
|
| 249 |
+
</div>
|
| 250 |
+
|
| 251 |
+
<div class="bg-white rounded-lg shadow p-4">
|
| 252 |
+
<h3 class="font-semibold mb-2">生成过程可视化</h3>
|
| 253 |
+
<div id="generation-viz" class="h-64"></div>
|
| 254 |
+
</div>
|
| 255 |
+
</div>
|
| 256 |
+
</div>
|
| 257 |
+
</section>
|
| 258 |
+
|
| 259 |
+
<!-- 部署指南 -->
|
| 260 |
+
<section id="deployment" class="py-16 bg-gray-50">
|
| 261 |
+
<div class="container mx-auto px-4">
|
| 262 |
+
<h2 class="text-3xl font-bold text-center mb-4">Windows 11部署指南</h2>
|
| 263 |
+
<p class="text-center text-gray-600 max-w-3xl mx-auto mb-12">
|
| 264 |
+
面向小白的详细部署教程,轻松在Windows 11上运行此交互式教学网站
|
| 265 |
+
</p>
|
| 266 |
+
|
| 267 |
+
<div class="max-w-4xl mx-auto bg-white rounded-xl shadow-lg overflow-hidden">
|
| 268 |
+
<div class="grid grid-cols-1 md:grid-cols-2">
|
| 269 |
+
<div class="p-6">
|
| 270 |
+
<h3 class="text-xl font-semibold mb-4">部署步骤</h3>
|
| 271 |
+
<ol class="list-decimal pl-6 space-y-4">
|
| 272 |
+
<li>
|
| 273 |
+
<strong>下载网站文件</strong>
|
| 274 |
+
<p class="text-sm text-gray-600 mt-1">将本网页保存为HTML文件(右键点击 → 另存为)</p>
|
| 275 |
+
</li>
|
| 276 |
+
<li>
|
| 277 |
+
<strong>安装本地服务器(可选)</strong>
|
| 278 |
+
<p class="text-sm text-gray-600 mt-1">安装Node.js和http-server模块:<code class="bg-gray-100 p-1 rounded">npm install -g http-server</code></p>
|
| 279 |
+
</li>
|
| 280 |
+
<li>
|
| 281 |
+
<strong>运行网站</strong>
|
| 282 |
+
<p class="text-sm text-gray-600 mt-1">双击HTML文件直接打开,或在命令行中运行:<code class="bg-gray-100 p-1 rounded">http-server</code></p>
|
| 283 |
+
</li>
|
| 284 |
+
<li>
|
| 285 |
+
<strong>访问网站</strong>
|
| 286 |
+
<p class="text-sm text-gray-600 mt-1">在浏览器中访问 <code class="bg-gray-100 p-1 rounded">http://localhost:8080</code></p>
|
| 287 |
+
</li>
|
| 288 |
+
</ol>
|
| 289 |
+
</div>
|
| 290 |
+
<div class="bg-gray-800 text-white p-6">
|
| 291 |
+
<h3 class="text-xl font-semibold mb-4">常见问题</h3>
|
| 292 |
+
<div class="space-y-4">
|
| 293 |
+
<div>
|
| 294 |
+
<h4 class="font-medium">Q: 为什么需要本地服务器?</h4>
|
| 295 |
+
<p class="text-gray-300 text-sm mt-1">A: 直接打开HTML文件时,某些API调用可能被浏览器安全策略阻止。使用本地服务器可以避免此问题。</p>
|
| 296 |
+
</div>
|
| 297 |
+
<div>
|
| 298 |
+
<h4 class="font-medium">Q: 如何安装Node.js?</h4>
|
| 299 |
+
<p class="text-gray-300 text-sm mt-1">A: 访问 <a href="https://nodejs.org" class="text-secondary underline">nodejs.org</a> 下载安装包,按提示安装即可。</p>
|
| 300 |
+
</div>
|
| 301 |
+
<div>
|
| 302 |
+
<h4 class="font-medium">Q: 网站无法加载可视化?</h4>
|
| 303 |
+
<p class="text-gray-300 text-sm mt-1">A: 确保浏览器支持JavaScript,并检查网络连接是否正常。</p>
|
| 304 |
+
</div>
|
| 305 |
+
</div>
|
| 306 |
+
</div>
|
| 307 |
+
</div>
|
| 308 |
+
</div>
|
| 309 |
+
|
| 310 |
+
<div class="mt-12 max-w-4xl mx-auto">
|
| 311 |
+
<h3 class="text-xl font-semibold mb-4">视频教程</h3>
|
| 312 |
+
<div class="bg-gray-200 border-2 border-dashed rounded-xl w-full h-64 flex items-center justify-center">
|
| 313 |
+
<div class="text-center">
|
| 314 |
+
<i class="fas fa-video text-4xl text-gray-400 mb-3"></i>
|
| 315 |
+
<p class="text-gray-500">Windows 11部署视频教程</p>
|
| 316 |
+
</div>
|
| 317 |
+
</div>
|
| 318 |
+
</div>
|
| 319 |
+
</div>
|
| 320 |
+
</section>
|
| 321 |
+
|
| 322 |
+
<!-- 页脚 -->
|
| 323 |
+
<footer class="bg-dark text-white py-8">
|
| 324 |
+
<div class="container mx-auto px-4 text-center">
|
| 325 |
+
<p class="mb-4">Transformer架构交互式教学网站 | 帮助初学者理解大语言模型原理</p>
|
| 326 |
+
<div class="flex justify-center space-x-4">
|
| 327 |
+
<a href="#" class="hover:text-secondary transition"><i class="fab fa-github"></i></a>
|
| 328 |
+
<a href="#" class="hover:text-secondary transition"><i class="fab fa-twitter"></i></a>
|
| 329 |
+
<a href="#" class="hover:text-secondary transition"><i class="fab fa-linkedin"></i></a>
|
| 330 |
+
</div>
|
| 331 |
+
<p class="mt-4 text-gray-400 text-sm">© 2023 AI Education Project. 本网站仅用于教学目的。</p>
|
| 332 |
+
</div>
|
| 333 |
+
</footer>
|
| 334 |
+
|
| 335 |
+
<script>
|
| 336 |
+
// 初始化Transformer架构可视化
|
| 337 |
+
function initTransformerViz() {
|
| 338 |
+
const svg = d3.select("#transformer-viz")
|
| 339 |
+
.append("svg")
|
| 340 |
+
.attr("width", "100%")
|
| 341 |
+
.attr("height", "100%")
|
| 342 |
+
.attr("viewBox", "0 0 800 400");
|
| 343 |
+
|
| 344 |
+
// 绘制编码器
|
| 345 |
+
const encoderGroup = svg.append("g").attr("transform", "translate(100, 50)");
|
| 346 |
+
|
| 347 |
+
// 编码器标签
|
| 348 |
+
encoderGroup.append("text")
|
| 349 |
+
.text("编码器")
|
| 350 |
+
.attr("x", 0)
|
| 351 |
+
.attr("y", -10)
|
| 352 |
+
.attr("font-weight", "bold")
|
| 353 |
+
.attr("fill", "#4F46E5");
|
| 354 |
+
|
| 355 |
+
// 绘制编码器层
|
| 356 |
+
for (let i = 0; i < 3; i++) {
|
| 357 |
+
const layerGroup = encoderGroup.append("g").attr("transform", `translate(0, ${i * 100})`);
|
| 358 |
+
|
| 359 |
+
// 层矩形
|
| 360 |
+
layerGroup.append("rect")
|
| 361 |
+
.attr("id", `encoder-layer-${i}`)
|
| 362 |
+
.attr("x", 0)
|
| 363 |
+
.attr("y", 0)
|
| 364 |
+
.attr("width", 200)
|
| 365 |
+
.attr("height", 80)
|
| 366 |
+
.attr("rx", 10)
|
| 367 |
+
.attr("fill", i === 1 ? "#E0E7FF" : "#F1F5F9")
|
| 368 |
+
.attr("stroke", "#4F46E5")
|
| 369 |
+
.attr("stroke-width", 2);
|
| 370 |
+
|
| 371 |
+
// 层标签
|
| 372 |
+
layerGroup.append("text")
|
| 373 |
+
.text(`编码器层 ${i+1}`)
|
| 374 |
+
.attr("x", 100)
|
| 375 |
+
.attr("y", 40)
|
| 376 |
+
.attr("text-anchor", "middle")
|
| 377 |
+
.attr("dominant-baseline", "middle")
|
| 378 |
+
.attr("fill", "#4F46E5");
|
| 379 |
+
|
| 380 |
+
// 内部组件
|
| 381 |
+
layerGroup.append("rect")
|
| 382 |
+
.attr("x", 20)
|
| 383 |
+
.attr("y", 20)
|
| 384 |
+
.attr("width", 60)
|
| 385 |
+
.attr("height", 40)
|
| 386 |
+
.attr("rx", 5)
|
| 387 |
+
.attr("fill", i === 1 ? "#C7D2FE" : "#DBEAFE")
|
| 388 |
+
.attr("stroke", "#818CF8")
|
| 389 |
+
.attr("id", `attention-${i}`);
|
| 390 |
+
|
| 391 |
+
layerGroup.append("text")
|
| 392 |
+
.text("注意力")
|
| 393 |
+
.attr("x", 50)
|
| 394 |
+
.attr("y", 40)
|
| 395 |
+
.attr("text-anchor", "middle")
|
| 396 |
+
.attr("dominant-baseline", "middle")
|
| 397 |
+
.attr("fill", "#4F46E5")
|
| 398 |
+
.attr("font-size", "12px");
|
| 399 |
+
|
| 400 |
+
layerGroup.append("rect")
|
| 401 |
+
.attr("x", 120)
|
| 402 |
+
.attr("y", 20)
|
| 403 |
+
.attr("width", 60)
|
| 404 |
+
.attr("height", 40)
|
| 405 |
+
.attr("rx", 5)
|
| 406 |
+
.attr("fill", i === 1 ? "#C7D2FE" : "#DBEAFE")
|
| 407 |
+
.attr("stroke", "#818CF8");
|
| 408 |
+
|
| 409 |
+
layerGroup.append("text")
|
| 410 |
+
.text("前馈")
|
| 411 |
+
.attr("x", 150)
|
| 412 |
+
.attr("y", 40)
|
| 413 |
+
.attr("text-anchor", "middle")
|
| 414 |
+
.attr("dominant-baseline", "middle")
|
| 415 |
+
.attr("fill", "#4F46E5")
|
| 416 |
+
.attr("font-size", "12px");
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
// 绘制解码器
|
| 420 |
+
const decoderGroup = svg.append("g").attr("transform", "translate(500, 50)");
|
| 421 |
+
|
| 422 |
+
// 解码器标签
|
| 423 |
+
decoderGroup.append("text")
|
| 424 |
+
.text("解码器")
|
| 425 |
+
.attr("x", 0)
|
| 426 |
+
.attr("y", -10)
|
| 427 |
+
.attr("font-weight", "bold")
|
| 428 |
+
.attr("fill", "#F472B6");
|
| 429 |
+
|
| 430 |
+
// 绘制解码器层
|
| 431 |
+
for (let i = 0; i < 3; i++) {
|
| 432 |
+
const layerGroup = decoderGroup.append("g").attr("transform", `translate(0, ${i * 100})`);
|
| 433 |
+
|
| 434 |
+
// 层矩形
|
| 435 |
+
layerGroup.append("rect")
|
| 436 |
+
.attr("id", `decoder-layer-${i}`)
|
| 437 |
+
.attr("x", 0)
|
| 438 |
+
.attr("y", 0)
|
| 439 |
+
.attr("width", 200)
|
| 440 |
+
.attr("height", 80)
|
| 441 |
+
.attr("rx", 10)
|
| 442 |
+
.attr("fill", i === 1 ? "#FCE7F3" : "#FDF2F8")
|
| 443 |
+
.attr("stroke", "#F472B6")
|
| 444 |
+
.attr("stroke-width", 2);
|
| 445 |
+
|
| 446 |
+
// 层标签
|
| 447 |
+
layerGroup.append("text")
|
| 448 |
+
.text(`解码器层 ${i+1}`)
|
| 449 |
+
.attr("x", 100)
|
| 450 |
+
.attr("y", 40)
|
| 451 |
+
.attr("text-anchor", "middle")
|
| 452 |
+
.attr("dominant-baseline", "middle")
|
| 453 |
+
.attr("fill", "#F472B6");
|
| 454 |
+
|
| 455 |
+
// 内部组件
|
| 456 |
+
layerGroup.append("rect")
|
| 457 |
+
.attr("x", 20)
|
| 458 |
+
.attr("y", 20)
|
| 459 |
+
.attr("width", 50)
|
| 460 |
+
.attr("height", 40)
|
| 461 |
+
.attr("rx", 5)
|
| 462 |
+
.attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3")
|
| 463 |
+
.attr("stroke", "#F472B6");
|
| 464 |
+
|
| 465 |
+
layerGroup.append("text")
|
| 466 |
+
.text("自注意")
|
| 467 |
+
.attr("x", 45)
|
| 468 |
+
.attr("y", 40)
|
| 469 |
+
.attr("text-anchor", "middle")
|
| 470 |
+
.attr("dominant-baseline", "middle")
|
| 471 |
+
.attr("fill", "#F472B6")
|
| 472 |
+
.attr("font-size", "12px");
|
| 473 |
+
|
| 474 |
+
layerGroup.append("rect")
|
| 475 |
+
.attr("x", 85)
|
| 476 |
+
.attr("y", 20)
|
| 477 |
+
.attr("width", 50)
|
| 478 |
+
.attr("height", 40)
|
| 479 |
+
.attr("rx", 5)
|
| 480 |
+
.attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3")
|
| 481 |
+
.attr("stroke", "#F472B6");
|
| 482 |
+
|
| 483 |
+
layerGroup.append("text")
|
| 484 |
+
.text("编码注意")
|
| 485 |
+
.attr("x", 110)
|
| 486 |
+
.attr("y", 40)
|
| 487 |
+
.attr("text-anchor", "middle")
|
| 488 |
+
.attr("dominant-baseline", "middle")
|
| 489 |
+
.attr("fill", "#F472B6")
|
| 490 |
+
.attr("font-size", "10px");
|
| 491 |
+
|
| 492 |
+
layerGroup.append("rect")
|
| 493 |
+
.attr("x", 150)
|
| 494 |
+
.attr("y", 20)
|
| 495 |
+
.attr("width", 40)
|
| 496 |
+
.attr("height", 40)
|
| 497 |
+
.attr("rx", 5)
|
| 498 |
+
.attr("fill", i === 1 ? "#FBCFE8" : "#FCE7F3")
|
| 499 |
+
.attr("stroke", "#F472B6");
|
| 500 |
+
|
| 501 |
+
layerGroup.append("text")
|
| 502 |
+
.text("前馈")
|
| 503 |
+
.attr("x", 170)
|
| 504 |
+
.attr("y", 40)
|
| 505 |
+
.attr("text-anchor", "middle")
|
| 506 |
+
.attr("dominant-baseline", "middle")
|
| 507 |
+
.attr("fill", "#F472B6")
|
| 508 |
+
.attr("font-size", "12px");
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
// 绘制连接线
|
| 512 |
+
svg.append("line")
|
| 513 |
+
.attr("x1", 300)
|
| 514 |
+
.attr("y1", 200)
|
| 515 |
+
.attr("x2", 500)
|
| 516 |
+
.attr("y2", 200)
|
| 517 |
+
.attr("stroke", "#94A3B8")
|
| 518 |
+
.attr("stroke-width", 2)
|
| 519 |
+
.attr("marker-end", "url(#arrow)");
|
| 520 |
+
|
| 521 |
+
// 绘制输入输出
|
| 522 |
+
svg.append("rect")
|
| 523 |
+
.attr("x", 20)
|
| 524 |
+
.attr("y", 180)
|
| 525 |
+
.attr("width", 60)
|
| 526 |
+
.attr("height", 40)
|
| 527 |
+
.attr("rx", 5)
|
| 528 |
+
.attr("fill", "#D1FAE5")
|
| 529 |
+
.attr("stroke", "#10B981");
|
| 530 |
+
|
| 531 |
+
svg.append("text")
|
| 532 |
+
.text("输入")
|
| 533 |
+
.attr("x", 50)
|
| 534 |
+
.attr("y", 200)
|
| 535 |
+
.attr("text-anchor", "middle")
|
| 536 |
+
.attr("dominant-baseline", "middle")
|
| 537 |
+
.attr("fill", "#10B981");
|
| 538 |
+
|
| 539 |
+
svg.append("rect")
|
| 540 |
+
.attr("x", 720)
|
| 541 |
+
.attr("y", 180)
|
| 542 |
+
.attr("width", 60)
|
| 543 |
+
.attr("height", 40)
|
| 544 |
+
.attr("rx", 5)
|
| 545 |
+
.attr("fill", "#D1FAE5")
|
| 546 |
+
.attr("stroke", "#10B981");
|
| 547 |
+
|
| 548 |
+
svg.append("text")
|
| 549 |
+
.text("输出")
|
| 550 |
+
.attr("x", 750)
|
| 551 |
+
.attr("y", 200)
|
| 552 |
+
.attr("text-anchor", "middle")
|
| 553 |
+
.attr("dominant-baseline", "middle")
|
| 554 |
+
.attr("fill", "#10B981");
|
| 555 |
+
|
| 556 |
+
// 添加箭头标记
|
| 557 |
+
svg.append("defs").append("marker")
|
| 558 |
+
.attr("id", "arrow")
|
| 559 |
+
.attr("viewBox", "0 -5 10 10")
|
| 560 |
+
.attr("refX", 5)
|
| 561 |
+
.attr("refY", 0)
|
| 562 |
+
.attr("markerWidth", 6)
|
| 563 |
+
.attr("markerHeight", 6)
|
| 564 |
+
.attr("orient", "auto")
|
| 565 |
+
.append("path")
|
| 566 |
+
.attr("d", "M0,-5L10,0L0,5")
|
| 567 |
+
.attr("fill", "#94A3B8");
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
// 初始化注意力热力图
|
| 571 |
+
function initAttentionViz() {
|
| 572 |
+
const text = "人工智能正在改变世界";
|
| 573 |
+
const tokens = text.split('');
|
| 574 |
+
|
| 575 |
+
const data = [{
|
| 576 |
+
z: generateAttentionMatrix(tokens.length),
|
| 577 |
+
x: tokens,
|
| 578 |
+
y: tokens,
|
| 579 |
+
type: 'heatmap',
|
| 580 |
+
colorscale: 'Blues',
|
| 581 |
+
hoverongaps: false
|
| 582 |
+
}];
|
| 583 |
+
|
| 584 |
+
const layout = {
|
| 585 |
+
title: '注意力权重分布',
|
| 586 |
+
xaxis: { title: '键 (Key)' },
|
| 587 |
+
yaxis: { title: '查询 (Query)', autorange: 'reversed' },
|
| 588 |
+
height: 300,
|
| 589 |
+
margin: { l: 60, r: 20, t: 40, b: 60 }
|
| 590 |
+
};
|
| 591 |
+
|
| 592 |
+
Plotly.newPlot('attention-heatmap', data, layout);
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
// 生成模拟注意力矩阵
|
| 596 |
+
function generateAttentionMatrix(size) {
|
| 597 |
+
const matrix = [];
|
| 598 |
+
for (let i = 0; i < size; i++) {
|
| 599 |
+
const row = [];
|
| 600 |
+
for (let j = 0; j < size; j++) {
|
| 601 |
+
// 模拟对角线附近有较高权重
|
| 602 |
+
const dist = Math.abs(i - j);
|
| 603 |
+
let value = Math.exp(-dist * 0.5);
|
| 604 |
+
// 添加一些随机性
|
| 605 |
+
value += (Math.random() - 0.5) * 0.2;
|
| 606 |
+
value = Math.max(0, Math.min(1, value));
|
| 607 |
+
row.push(value);
|
| 608 |
+
}
|
| 609 |
+
matrix.push(row);
|
| 610 |
+
}
|
| 611 |
+
return matrix;
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
// 更新注意力可视化
|
| 615 |
+
function updateAttentionViz() {
|
| 616 |
+
const text = document.getElementById('attention-text').value || "人工智能正在改变世界";
|
| 617 |
+
const tokens = text.split('');
|
| 618 |
+
|
| 619 |
+
Plotly.update('attention-heatmap', {
|
| 620 |
+
z: [generateAttentionMatrix(tokens.length)],
|
| 621 |
+
x: [tokens],
|
| 622 |
+
y: [tokens]
|
| 623 |
+
});
|
| 624 |
+
}
|
| 625 |
+
|
| 626 |
+
// 高亮架构组件
|
| 627 |
+
function highlightComponent(component) {
|
| 628 |
+
d3.selectAll("rect").attr("stroke-width", 1);
|
| 629 |
+
|
| 630 |
+
if (component === 'encoder') {
|
| 631 |
+
d3.selectAll("#encoder-layer-1")
|
| 632 |
+
.attr("stroke-width", 3)
|
| 633 |
+
.attr("stroke", "#4F46E5");
|
| 634 |
+
} else if (component === 'decoder') {
|
| 635 |
+
d3.selectAll("#decoder-layer-1")
|
| 636 |
+
.attr("stroke-width", 3)
|
| 637 |
+
.attr("stroke", "#F472B6");
|
| 638 |
+
} else if (component === 'attention') {
|
| 639 |
+
d3.selectAll("[id^='attention-']")
|
| 640 |
+
.attr("stroke-width", 3)
|
| 641 |
+
.attr("stroke", "#818CF8");
|
| 642 |
+
}
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
// 重置高亮
|
| 646 |
+
function resetHighlight() {
|
| 647 |
+
d3.selectAll("rect").attr("stroke-width", 1);
|
| 648 |
+
}
|
| 649 |
+
|
| 650 |
+
// 文本生成函数
|
| 651 |
+
async function generateText() {
|
| 652 |
+
const prompt = document.getElementById('prompt-input').value;
|
| 653 |
+
const model = document.getElementById('model-select').value;
|
| 654 |
+
const length = document.getElementById('length-slider').value;
|
| 655 |
+
const temperature = document.getElementById('temp-slider').value;
|
| 656 |
+
|
| 657 |
+
if (!prompt.trim()) {
|
| 658 |
+
alert("请输入提示文本!");
|
| 659 |
+
return;
|
| 660 |
+
}
|
| 661 |
+
|
| 662 |
+
document.getElementById('generate-btn').innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>生成中...';
|
| 663 |
+
|
| 664 |
+
try {
|
| 665 |
+
// 模拟API调用延迟
|
| 666 |
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
| 667 |
+
|
| 668 |
+
// 模拟生成结果
|
| 669 |
+
const generatedText = prompt + "一项革命性技术,它通过模拟人类智能过程,使计算机能够执行复杂的任务。从语音助手到自动驾驶,AI正在渗透到我们生活的方方面面。";
|
| 670 |
+
|
| 671 |
+
// 显示生成结果
|
| 672 |
+
document.getElementById('generated-text').innerHTML =
|
| 673 |
+
`<p class="fade-in">${generatedText}</p>`;
|
| 674 |
+
|
| 675 |
+
// 更新生成过程可视化
|
| 676 |
+
updateGenerationViz(prompt, generatedText);
|
| 677 |
+
|
| 678 |
+
} catch (error) {
|
| 679 |
+
console.error("生成错误:", error);
|
| 680 |
+
document.getElementById('generated-text').innerHTML =
|
| 681 |
+
`<p class="text-red-500">生成失败: ${error.message}</p>`;
|
| 682 |
+
} finally {
|
| 683 |
+
document.getElementById('generate-btn').innerHTML = '<i class="fas fa-bolt mr-2"></i>生成文本';
|
| 684 |
+
}
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
// 更新生成过程可视化
|
| 688 |
+
function updateGenerationViz(prompt, fullText) {
|
| 689 |
+
const promptTokens = prompt.split('');
|
| 690 |
+
const allTokens = fullText.split('');
|
| 691 |
+
|
| 692 |
+
const data = [{
|
| 693 |
+
z: generateAttentionMatrix(allTokens.length),
|
| 694 |
+
x: allTokens,
|
| 695 |
+
y: allTokens,
|
| 696 |
+
type: 'heatmap',
|
| 697 |
+
colorscale: 'Blues',
|
| 698 |
+
hoverongaps: false
|
| 699 |
+
}];
|
| 700 |
+
|
| 701 |
+
const layout = {
|
| 702 |
+
title: '生成过程注意力权重',
|
| 703 |
+
xaxis: { title: '位置' },
|
| 704 |
+
yaxis: { title: '生成步骤', autorange: 'reversed' },
|
| 705 |
+
height: 250,
|
| 706 |
+
margin: { l: 50, r: 20, t: 40, b: 50 }
|
| 707 |
+
};
|
| 708 |
+
|
| 709 |
+
Plotly.newPlot('generation-viz', data, layout);
|
| 710 |
+
}
|
| 711 |
+
|
| 712 |
+
// 初始化页面
|
| 713 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 714 |
+
// 初始化可视化
|
| 715 |
+
initTransformerViz();
|
| 716 |
+
initAttentionViz();
|
| 717 |
+
|
| 718 |
+
// 设置滑块事件
|
| 719 |
+
document.getElementById('length-slider').addEventListener('input', function() {
|
| 720 |
+
document.getElementById('length-value').textContent = this.value;
|
| 721 |
+
});
|
| 722 |
+
|
| 723 |
+
document.getElementById('temp-slider').addEventListener('input', function() {
|
| 724 |
+
document.getElementById('temp-value').textContent = this.value;
|
| 725 |
+
});
|
| 726 |
+
|
| 727 |
+
// 设置生成按钮事件
|
| 728 |
+
document.getElementById('generate-btn').addEventListener('click', generateText);
|
| 729 |
+
|
| 730 |
+
// 平滑滚动
|
| 731 |
+
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
| 732 |
+
anchor.addEventListener('click', function(e) {
|
| 733 |
+
e.preventDefault();
|
| 734 |
+
document.querySelector(this.getAttribute('href')).scrollIntoView({
|
| 735 |
+
behavior: 'smooth'
|
| 736 |
+
});
|
| 737 |
+
});
|
| 738 |
+
});
|
| 739 |
+
});
|
| 740 |
+
</script>
|
| 741 |
+
<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=ahepc/ai" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
|
| 742 |
+
</html>
|