Advertisement
here2share

AI Visualizer

Jul 13th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html> JS is far too hard for me to understand whereas PY is very easy -- the cell coloring is somehow wrong
  2. <html>
  3. <head>
  4. <title>AI Visualizer</title>
  5. <style>
  6. #visualizer {
  7.   width: 1400px;
  8.   height: 600px;
  9.   overflow-y: scroll;
  10.   background-color: black;
  11. }
  12.  
  13. .cell {
  14.   width: 20px;
  15.   height: 18px;
  16.   display: inline-block;
  17.   text-align: center;
  18.   color: white;
  19.   font-size: 12px;
  20. }
  21.  
  22. .result,
  23. .gain,
  24. .weights-container,
  25. .line {
  26.   display: inline-block;
  27.   width: 80px;
  28.   text-align: left;
  29.   color: white;
  30.   padding-left: 10px;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <h1>AI Visualizer</h1>
  36. <button id="start">Start</button>
  37. <button id="pause">Pause</button>
  38. <button id="reset">Reset</button>
  39. <div id="controls">
  40. <div id="visualizer"></div>
  41. </div>
  42.  
  43. <script>
  44. let running = false;
  45. let data = [];
  46. let weights = {};
  47. let progress = [];
  48. let line = 0;
  49.  
  50. // Color constants
  51. const j = 256 / 400;
  52. const a = Array.from({ length: 400 }, (_, i) => Math.floor(i * j));
  53. const b = Array.from({ length: 300 }, () => 0);
  54. const red = [...a.reverse(), ...b, ...b];
  55. const green = [...b, ...b, ...a];
  56. const blue = [...b, ...a, ...b];
  57.  
  58. function initializeData() {
  59.   // Generate 5000 data points
  60.   for (let i = 0; i < 5000; i++) {
  61.     const category = (i % 5) + 1;
  62.     const string = generateString(category);
  63.     data.push({ string, category });
  64.   }
  65.  
  66.   // Initialize weights
  67.   for (let i = 1; i <= 5; i++) {
  68.     weights[i] = {};
  69.   }
  70. }
  71.  
  72. function generateString(category) {
  73.   let result = '';
  74.   switch(category) {
  75.     case 1:
  76.       result = '1234567yellow';
  77.       break;
  78.     case 2:
  79.       result = '123456789helloworld';
  80.       break;
  81.     case 3:
  82.       result = 'python3407';
  83.       break;
  84.     case 4:
  85.       result = 'thankyou12345';
  86.       break;
  87.     case 5:
  88.       for (let i = 0; i < 12; i++) {
  89.         result += String.fromCharCode(65 + Math.floor(Math.random() * 26));
  90.         result += Math.floor(Math.random() * 10);
  91.       }
  92.       result += String.fromCharCode(65 + Math.floor(Math.random() * 26));
  93.       break;
  94.   }
  95.   while (result.length < 25) {
  96.     result += String.fromCharCode(65 + Math.floor(Math.random() * 26));
  97.   }
  98.   return result.split('').sort(() => Math.random() - 0.5).join('');
  99. }
  100.  
  101. function getNodes(string) {
  102.   const nodes = [];
  103.   for (let i = 0; i < string.length; i++) {
  104.     for (let j = i + 1; j < string.length; j++) {
  105.       nodes.push(`${string[i]}:${i}:${string[j]}:${j}`);
  106.     }
  107.   }
  108.   return nodes;
  109. }
  110.  
  111. function predict(nodes, string) {
  112.   const w = {};
  113.   const cell_colors = {};
  114.   for (let i = 0; i < string.length; i++) {
  115.     cell_colors[i] = {};
  116.     cell_colors[i][string[i]] = 0;
  117.   }
  118.   for (let category = 1; category <= 5; category++) {
  119.     w[category] = 0;
  120.     for (const node of nodes) {
  121.       const [chr, idxStr] = node.split(':');
  122.       const idx = parseInt(idxStr);
  123.       const sum = weights[category][node] || 0;
  124.       w[category] += sum;
  125.       cell_colors[idx][chr] += sum;
  126.     }
  127.   }
  128.   return [Object.entries(w).sort((a, b) => b[1] - a[1]), cell_colors];
  129. }
  130.  
  131. function train(correctCategory, nodes) {
  132.     for (const node of nodes) {
  133.       weights[correctCategory][node] = (weights[correctCategory][node] || 50) + 1;
  134.     }
  135. }
  136.  
  137. function step() {
  138.   const { string, category } = data[line % data.length];
  139.   const nodes = getNodes(string);
  140.   const [ sortedSums, cell_colors ] = predict(nodes, string);
  141.   const prediction = sortedSums[0][0];
  142.  
  143.   const isCorrect = prediction == category;
  144.   if (!isCorrect) {
  145.     train(category, nodes);
  146.   }
  147.   progress.push(isCorrect ? 1 : 0);
  148.   if (progress.length > 100) progress.shift();
  149.  
  150.   const gain = progress.reduce((a, b) => a + b, 0);
  151.   const gainText = `${gain}% gain`;
  152.   const resultText = isCorrect ? `Y [${category}]` : `N [${category}]`;
  153.  
  154.   visualize(string, category, isCorrect, gainText, resultText, nodes, sortedSums, cell_colors);
  155.  
  156.   line++;
  157. }
  158.  
  159. function visualize(string, category, isCorrect, gainText, resultText, nodes, sortedSums, cell_colors) {
  160.   const visualizer = document.getElementById('visualizer');
  161.   const row = document.createElement('div');
  162.  
  163.   let valuesString = '';
  164.   for (let idx = 0; idx < string.length; idx++) {
  165.     let chr = string[idx];
  166.     valuesString += `cell_colors[${idx}][${chr}] = ${cell_colors[idx][chr]}\n`;
  167.   }
  168.  
  169.   for (let i = 0; i < string.length; i++) {
  170.     const cell = document.createElement('div');
  171.     cell.className = 'cell';
  172.     cell.textContent = string[i];
  173.     const value = cell_colors[i] ? cell_colors[i][string[i]] : 0;
  174.     cell.style.backgroundColor = getColor(value || 0)
  175.     row.appendChild(cell);
  176.   }
  177.  
  178.   const result = document.createElement('span');
  179.   result.className = 'result';
  180.   result.textContent = resultText;
  181.   row.appendChild(result);
  182.  
  183.   const gainElement = document.createElement('span');
  184.   gainElement.className = 'gain';
  185.   gainElement.textContent = gainText;
  186.   row.appendChild(gainElement);
  187.  
  188.   for (let i = 0; i < sortedSums.length; i++) {
  189.     const [category, weight] = sortedSums[i];
  190.     const weightContainer = document.createElement('span');
  191.     weightContainer.className = 'weights-container';
  192.     const weightElement = document.createElement('span');
  193.     weightElement.className = 'weights';
  194.     weightElement.textContent = `${category}: ${weight}`;
  195.     weightContainer.appendChild(weightElement);
  196.     row.appendChild(weightContainer);
  197.   }
  198.  
  199.   const lineElement = document.createElement('span');
  200.   lineElement.className = 'line';
  201.   lineElement.textContent = line + 1;
  202.   row.appendChild(lineElement);
  203.  
  204.   visualizer.appendChild(row);
  205.   visualizer.scrollTop = visualizer.scrollHeight;
  206. }
  207.  
  208.  
  209. function getColor(value) {
  210.   const index = Math.min(Math.floor(value), 999);
  211.   return `rgb(${red[index]}, ${green[index]}, ${blue[index]})`;
  212. }
  213.  
  214. document.getElementById('start').addEventListener('click', () => {
  215.   running = true;
  216.   run();
  217. });
  218.  
  219. document.getElementById('pause').addEventListener('click', () => {
  220.   running = false;
  221. });
  222.  
  223. document.getElementById('reset').addEventListener('click', () => {
  224.   running = false;
  225.   visualizer.innerHTML = '';
  226.   line = 0;
  227.   progress = [];
  228.   weights = {};
  229.   initializeData();
  230. });
  231.  
  232. function run() {
  233.   if (running) {
  234.     step();
  235.     requestAnimationFrame(run);
  236.   }
  237. }
  238.  
  239. initializeData();
  240. </script>
  241. </body>
  242. </html>
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement