Add 3 files
Browse files- README.md +7 -5
- index.html +554 -19
- prompts.txt +0 -0
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: space
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: yellow
|
| 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,554 @@
|
|
| 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="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>AdaBoost Implementation with Decision Stumps</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
| 9 |
+
<script src="https://cdn.jsdelivr.net/npm/pca-js@1.0.0/pca.min.js"></script>
|
| 10 |
+
<style>
|
| 11 |
+
.loading-spinner {
|
| 12 |
+
border: 4px solid rgba(0, 0, 0, 0.1);
|
| 13 |
+
border-radius: 50%;
|
| 14 |
+
border-top: 4px solid #3498db;
|
| 15 |
+
width: 30px;
|
| 16 |
+
height: 30px;
|
| 17 |
+
animation: spin 1s linear infinite;
|
| 18 |
+
margin: 0 auto;
|
| 19 |
+
}
|
| 20 |
+
@keyframes spin {
|
| 21 |
+
0% { transform: rotate(0deg); }
|
| 22 |
+
100% { transform: rotate(360deg); }
|
| 23 |
+
}
|
| 24 |
+
.card {
|
| 25 |
+
transition: all 0.3s ease;
|
| 26 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 27 |
+
}
|
| 28 |
+
.card:hover {
|
| 29 |
+
transform: translateY(-5px);
|
| 30 |
+
box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
|
| 31 |
+
}
|
| 32 |
+
</style>
|
| 33 |
+
</head>
|
| 34 |
+
<body class="bg-gray-50 min-h-screen">
|
| 35 |
+
<div class="container mx-auto px-4 py-8">
|
| 36 |
+
<header class="text-center mb-12">
|
| 37 |
+
<h1 class="text-4xl font-bold text-indigo-700 mb-2">AdaBoost with Decision Stumps</h1>
|
| 38 |
+
<p class="text-xl text-gray-600">Implementation from scratch with MNIST digit classification</p>
|
| 39 |
+
</header>
|
| 40 |
+
|
| 41 |
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-12">
|
| 42 |
+
<div class="card bg-white rounded-lg p-6">
|
| 43 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-4">Algorithm Overview</h2>
|
| 44 |
+
<div class="space-y-4">
|
| 45 |
+
<div class="flex items-start">
|
| 46 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 47 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 48 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
|
| 49 |
+
</svg>
|
| 50 |
+
</div>
|
| 51 |
+
<div>
|
| 52 |
+
<h3 class="font-medium text-gray-800">Decision Stumps</h3>
|
| 53 |
+
<p class="text-gray-600">Weak learners (depth-1 decision trees) that make predictions based on a single feature threshold.</p>
|
| 54 |
+
</div>
|
| 55 |
+
</div>
|
| 56 |
+
<div class="flex items-start">
|
| 57 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 58 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 59 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
|
| 60 |
+
</svg>
|
| 61 |
+
</div>
|
| 62 |
+
<div>
|
| 63 |
+
<h3 class="font-medium text-gray-800">Weighted Error</h3>
|
| 64 |
+
<p class="text-gray-600">Sample weights are updated to focus on misclassified examples in each boosting round.</p>
|
| 65 |
+
</div>
|
| 66 |
+
</div>
|
| 67 |
+
<div class="flex items-start">
|
| 68 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 69 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 70 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
|
| 71 |
+
</svg>
|
| 72 |
+
</div>
|
| 73 |
+
<div>
|
| 74 |
+
<h3 class="font-medium text-gray-800">Classifier Weights</h3>
|
| 75 |
+
<p class="text-gray-600">Each stump's contribution is weighted by its accuracy (β = ½ ln((1-err)/err)).</p>
|
| 76 |
+
</div>
|
| 77 |
+
</div>
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
|
| 81 |
+
<div class="card bg-white rounded-lg p-6">
|
| 82 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-4">MNIST Dataset</h2>
|
| 83 |
+
<div class="space-y-4">
|
| 84 |
+
<div class="flex items-start">
|
| 85 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 86 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 87 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z" />
|
| 88 |
+
</svg>
|
| 89 |
+
</div>
|
| 90 |
+
<div>
|
| 91 |
+
<h3 class="font-medium text-gray-800">Classes 0 and 1</h3>
|
| 92 |
+
<p class="text-gray-600">Binary classification task distinguishing between digits 0 and 1.</p>
|
| 93 |
+
</div>
|
| 94 |
+
</div>
|
| 95 |
+
<div class="flex items-start">
|
| 96 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 97 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 98 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 3v2m6-2v2M9 19v2m6-2v2M5 9H3m2 6H3m18-6h-2m2 6h-2M7 19h10a2 2 0 002-2V7a2 2 0 00-2-2H7a2 2 0 00-2 2v10a2 2 0 002 2zM9 9h6v6H9V9z" />
|
| 99 |
+
</svg>
|
| 100 |
+
</div>
|
| 101 |
+
<div>
|
| 102 |
+
<h3 class="font-medium text-gray-800">Dimensionality Reduction</h3>
|
| 103 |
+
<p class="text-gray-600">PCA applied to reduce 784 features to 5 principal components.</p>
|
| 104 |
+
</div>
|
| 105 |
+
</div>
|
| 106 |
+
<div class="flex items-start">
|
| 107 |
+
<div class="bg-indigo-100 p-2 rounded-full mr-3">
|
| 108 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 109 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
|
| 110 |
+
</svg>
|
| 111 |
+
</div>
|
| 112 |
+
<div>
|
| 113 |
+
<h3 class="font-medium text-gray-800">Training Size</h3>
|
| 114 |
+
<p class="text-gray-600">1000 samples per class for training, full test set for evaluation.</p>
|
| 115 |
+
</div>
|
| 116 |
+
</div>
|
| 117 |
+
</div>
|
| 118 |
+
</div>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
<div class="card bg-white rounded-lg p-6 mb-12">
|
| 122 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-6">Run AdaBoost Training</h2>
|
| 123 |
+
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
|
| 124 |
+
<div>
|
| 125 |
+
<label class="block text-gray-700 mb-2">Number of Rounds</label>
|
| 126 |
+
<input type="number" id="numRounds" value="200" min="1" max="500" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
|
| 127 |
+
</div>
|
| 128 |
+
<div>
|
| 129 |
+
<label class="block text-gray-700 mb-2">Training Samples per Class</label>
|
| 130 |
+
<input type="number" id="trainSamples" value="1000" min="100" max="6000" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
|
| 131 |
+
</div>
|
| 132 |
+
<div>
|
| 133 |
+
<label class="block text-gray-700 mb-2">PCA Components</label>
|
| 134 |
+
<input type="number" id="pcaComponents" value="5" min="1" max="10" class="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">
|
| 135 |
+
</div>
|
| 136 |
+
</div>
|
| 137 |
+
<button id="trainButton" class="w-full md:w-auto bg-indigo-600 hover:bg-indigo-700 text-white font-medium py-2 px-6 rounded-lg transition duration-300 flex items-center justify-center">
|
| 138 |
+
<span id="buttonText">Train AdaBoost Model</span>
|
| 139 |
+
<div id="loadingSpinner" class="loading-spinner ml-2 hidden"></div>
|
| 140 |
+
</button>
|
| 141 |
+
</div>
|
| 142 |
+
|
| 143 |
+
<div id="resultsSection" class="hidden">
|
| 144 |
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-12">
|
| 145 |
+
<div class="card bg-white rounded-lg p-6">
|
| 146 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-4">Training Progress</h2>
|
| 147 |
+
<div class="h-80">
|
| 148 |
+
<canvas id="errorChart"></canvas>
|
| 149 |
+
</div>
|
| 150 |
+
</div>
|
| 151 |
+
<div class="card bg-white rounded-lg p-6">
|
| 152 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-4">Loss Curves</h2>
|
| 153 |
+
<div class="h-80">
|
| 154 |
+
<canvas id="lossChart"></canvas>
|
| 155 |
+
</div>
|
| 156 |
+
</div>
|
| 157 |
+
</div>
|
| 158 |
+
|
| 159 |
+
<div class="card bg-white rounded-lg p-6 mb-12">
|
| 160 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-6">Final Results</h2>
|
| 161 |
+
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
|
| 162 |
+
<div class="bg-indigo-50 rounded-lg p-4 text-center">
|
| 163 |
+
<p class="text-sm text-indigo-600 font-medium">Training Accuracy</p>
|
| 164 |
+
<p id="trainAccuracy" class="text-3xl font-bold text-indigo-800">0%</p>
|
| 165 |
+
</div>
|
| 166 |
+
<div class="bg-indigo-50 rounded-lg p-4 text-center">
|
| 167 |
+
<p class="text-sm text-indigo-600 font-medium">Validation Accuracy</p>
|
| 168 |
+
<p id="valAccuracy" class="text-3xl font-bold text-indigo-800">0%</p>
|
| 169 |
+
</div>
|
| 170 |
+
<div class="bg-indigo-50 rounded-lg p-4 text-center">
|
| 171 |
+
<p class="text-sm text-indigo-600 font-medium">Test Accuracy</p>
|
| 172 |
+
<p id="testAccuracy" class="text-3xl font-bold text-indigo-800">0%</p>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
</div>
|
| 176 |
+
|
| 177 |
+
<div class="card bg-white rounded-lg p-6">
|
| 178 |
+
<h2 class="text-2xl font-semibold text-gray-800 mb-4">Classifier Weights Over Time</h2>
|
| 179 |
+
<div class="h-80">
|
| 180 |
+
<canvas id="weightsChart"></canvas>
|
| 181 |
+
</div>
|
| 182 |
+
</div>
|
| 183 |
+
</div>
|
| 184 |
+
</div>
|
| 185 |
+
|
| 186 |
+
<script>
|
| 187 |
+
// AdaBoost implementation
|
| 188 |
+
class AdaBoost {
|
| 189 |
+
constructor() {
|
| 190 |
+
this.classifiers = [];
|
| 191 |
+
this.betas = [];
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// Decision stump (weak classifier)
|
| 195 |
+
createStump(X, y, weights) {
|
| 196 |
+
let bestErr = Infinity;
|
| 197 |
+
let bestStump = {};
|
| 198 |
+
let bestPred = null;
|
| 199 |
+
|
| 200 |
+
// Try all features
|
| 201 |
+
for (let feature = 0; feature < X[0].length; feature++) {
|
| 202 |
+
const featureValues = X.map(x => x[feature]);
|
| 203 |
+
const minVal = Math.min(...featureValues);
|
| 204 |
+
const maxVal = Math.max(...featureValues);
|
| 205 |
+
|
| 206 |
+
// Try 3 possible thresholds between min and max
|
| 207 |
+
for (let threshold of [minVal + (maxVal-minVal)/4,
|
| 208 |
+
minVal + (maxVal-minVal)/2,
|
| 209 |
+
minVal + 3*(maxVal-minVal)/4]) {
|
| 210 |
+
|
| 211 |
+
// Try both inequality directions
|
| 212 |
+
for (let direction of [-1, 1]) {
|
| 213 |
+
let err = 0;
|
| 214 |
+
const pred = X.map(x =>
|
| 215 |
+
direction * x[feature] < direction * threshold ? 1 : -1
|
| 216 |
+
);
|
| 217 |
+
|
| 218 |
+
// Calculate weighted error
|
| 219 |
+
for (let i = 0; i < y.length; i++) {
|
| 220 |
+
if (pred[i] !== y[i]) {
|
| 221 |
+
err += weights[i];
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
// Keep track of best stump
|
| 226 |
+
if (err < bestErr) {
|
| 227 |
+
bestErr = err;
|
| 228 |
+
bestStump = { feature, threshold, direction };
|
| 229 |
+
bestPred = pred;
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
return { stump: bestStump, err: bestErr, pred: bestPred };
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
// Train AdaBoost with decision stumps
|
| 239 |
+
fit(X, y, rounds = 200) {
|
| 240 |
+
const n = X.length;
|
| 241 |
+
let weights = Array(n).fill(1/n);
|
| 242 |
+
this.classifiers = [];
|
| 243 |
+
this.betas = [];
|
| 244 |
+
|
| 245 |
+
const trainErrors = [];
|
| 246 |
+
const betasHistory = [];
|
| 247 |
+
|
| 248 |
+
for (let t = 0; t < rounds; t++) {
|
| 249 |
+
// Create and train a new stump
|
| 250 |
+
const { stump, err, pred } = this.createStump(X, y, weights);
|
| 251 |
+
|
| 252 |
+
// Calculate beta (classifier weight)
|
| 253 |
+
const beta = 0.5 * Math.log((1 - err) / Math.max(err, 1e-10));
|
| 254 |
+
this.betas.push(beta);
|
| 255 |
+
this.classifiers.push(stump);
|
| 256 |
+
betasHistory.push([...this.betas]);
|
| 257 |
+
|
| 258 |
+
// Update sample weights
|
| 259 |
+
for (let i = 0; i < n; i++) {
|
| 260 |
+
weights[i] *= Math.exp(-beta * y[i] * pred[i]);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
// Normalize weights
|
| 264 |
+
const sumWeights = weights.reduce((a, b) => a + b, 0);
|
| 265 |
+
weights = weights.map(w => w / sumWeights);
|
| 266 |
+
|
| 267 |
+
// Calculate training error
|
| 268 |
+
const trainPred = this.predict(X);
|
| 269 |
+
const trainErr = trainPred.reduce((sum, pred, i) =>
|
| 270 |
+
sum + (pred !== y[i] ? 1 : 0), 0) / n;
|
| 271 |
+
trainErrors.push(trainErr);
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
return { trainErrors, betasHistory };
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
// Make predictions using all classifiers
|
| 278 |
+
predict(X) {
|
| 279 |
+
const preds = X.map(x => {
|
| 280 |
+
let score = 0;
|
| 281 |
+
for (let i = 0; i < this.classifiers.length; i++) {
|
| 282 |
+
const { feature, threshold, direction } = this.classifiers[i];
|
| 283 |
+
score += this.betas[i] *
|
| 284 |
+
(direction * x[feature] < direction * threshold ? 1 : -1);
|
| 285 |
+
}
|
| 286 |
+
return score >= 0 ? 1 : -1;
|
| 287 |
+
});
|
| 288 |
+
return preds;
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
// Load MNIST data
|
| 293 |
+
async function loadMNIST() {
|
| 294 |
+
const response = await fetch('https://storage.googleapis.com/tfjs-tutorials/mnist_data.json');
|
| 295 |
+
if (!response.ok) {
|
| 296 |
+
throw new Error('Failed to load MNIST data');
|
| 297 |
+
}
|
| 298 |
+
return await response.json();
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
// Prepare data for binary classification (0 vs 1)
|
| 302 |
+
function prepareData(data, trainSamplesPerClass, pcaComponents) {
|
| 303 |
+
// Filter only 0s and 1s
|
| 304 |
+
const zeros = data.filter(d => d.label === 0);
|
| 305 |
+
const ones = data.filter(d => d.label === 1);
|
| 306 |
+
|
| 307 |
+
// Shuffle and select samples
|
| 308 |
+
shuffleArray(zeros);
|
| 309 |
+
shuffleArray(ones);
|
| 310 |
+
|
| 311 |
+
const trainSize = Math.min(trainSamplesPerClass, zeros.length, ones.length);
|
| 312 |
+
const testZeros = zeros.slice(trainSize);
|
| 313 |
+
const testOnes = ones.slice(trainSize);
|
| 314 |
+
|
| 315 |
+
// Create train/test sets
|
| 316 |
+
const X_train = zeros.slice(0, trainSize).concat(ones.slice(0, trainSize))
|
| 317 |
+
.map(d => d.value);
|
| 318 |
+
const y_train = Array(trainSize).fill(-1).concat(Array(trainSize).fill(1));
|
| 319 |
+
|
| 320 |
+
const X_test = testZeros.concat(testOnes).map(d => d.value);
|
| 321 |
+
const y_test = Array(testZeros.length).fill(-1).concat(Array(testOnes.length).fill(1));
|
| 322 |
+
|
| 323 |
+
// Split train into train/validation (80/20)
|
| 324 |
+
const splitIdx = Math.floor(X_train.length * 0.8);
|
| 325 |
+
const X_val = X_train.slice(splitIdx);
|
| 326 |
+
const y_val = y_train.slice(splitIdx);
|
| 327 |
+
X_train.splice(splitIdx);
|
| 328 |
+
y_train.splice(splitIdx);
|
| 329 |
+
|
| 330 |
+
// Apply PCA
|
| 331 |
+
const pca = new PCA(X_train);
|
| 332 |
+
const reducedTrain = pca.reduce(X_train, pcaComponents);
|
| 333 |
+
const reducedVal = pca.reduce(X_val, pcaComponents);
|
| 334 |
+
const reducedTest = pca.reduce(X_test, pcaComponents);
|
| 335 |
+
|
| 336 |
+
return {
|
| 337 |
+
X_train: reducedTrain,
|
| 338 |
+
y_train,
|
| 339 |
+
X_val: reducedVal,
|
| 340 |
+
y_val,
|
| 341 |
+
X_test: reducedTest,
|
| 342 |
+
y_test
|
| 343 |
+
};
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
// Utility function to shuffle array
|
| 347 |
+
function shuffleArray(array) {
|
| 348 |
+
for (let i = array.length - 1; i > 0; i--) {
|
| 349 |
+
const j = Math.floor(Math.random() * (i + 1));
|
| 350 |
+
[array[i], array[j]] = [array[j], array[i]];
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
// Calculate accuracy
|
| 355 |
+
function calculateAccuracy(yTrue, yPred) {
|
| 356 |
+
let correct = 0;
|
| 357 |
+
for (let i = 0; i < yTrue.length; i++) {
|
| 358 |
+
if (yTrue[i] === yPred[i]) {
|
| 359 |
+
correct++;
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
return correct / yTrue.length;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
// Main function to run training
|
| 366 |
+
async function runTraining() {
|
| 367 |
+
const trainButton = document.getElementById('trainButton');
|
| 368 |
+
const buttonText = document.getElementById('buttonText');
|
| 369 |
+
const loadingSpinner = document.getElementById('loadingSpinner');
|
| 370 |
+
const resultsSection = document.getElementById('resultsSection');
|
| 371 |
+
|
| 372 |
+
// Show loading state
|
| 373 |
+
trainButton.disabled = true;
|
| 374 |
+
buttonText.textContent = 'Loading Data...';
|
| 375 |
+
loadingSpinner.classList.remove('hidden');
|
| 376 |
+
|
| 377 |
+
try {
|
| 378 |
+
// Get parameters
|
| 379 |
+
const numRounds = parseInt(document.getElementById('numRounds').value);
|
| 380 |
+
const trainSamples = parseInt(document.getElementById('trainSamples').value);
|
| 381 |
+
const pcaComponents = parseInt(document.getElementById('pcaComponents').value);
|
| 382 |
+
|
| 383 |
+
// Load and prepare data
|
| 384 |
+
const mnistData = await loadMNIST();
|
| 385 |
+
const { X_train, y_train, X_val, y_val, X_test, y_test } =
|
| 386 |
+
prepareData(mnistData, trainSamples, pcaComponents);
|
| 387 |
+
|
| 388 |
+
buttonText.textContent = 'Training...';
|
| 389 |
+
|
| 390 |
+
// Train AdaBoost
|
| 391 |
+
const adaboost = new AdaBoost();
|
| 392 |
+
const { trainErrors, betasHistory } = adaboost.fit(X_train, y_train, numRounds);
|
| 393 |
+
|
| 394 |
+
// Make predictions
|
| 395 |
+
const trainPred = adaboost.predict(X_train);
|
| 396 |
+
const valPred = adaboost.predict(X_val);
|
| 397 |
+
const testPred = adaboost.predict(X_test);
|
| 398 |
+
|
| 399 |
+
// Calculate accuracies
|
| 400 |
+
const trainAcc = calculateAccuracy(y_train, trainPred);
|
| 401 |
+
const valAcc = calculateAccuracy(y_val, valPred);
|
| 402 |
+
const testAcc = calculateAccuracy(y_test, testPred);
|
| 403 |
+
|
| 404 |
+
// Update UI with results
|
| 405 |
+
document.getElementById('trainAccuracy').textContent = `${(trainAcc * 100).toFixed(1)}%`;
|
| 406 |
+
document.getElementById('valAccuracy').textContent = `${(valAcc * 100).toFixed(1)}%`;
|
| 407 |
+
document.getElementById('testAccuracy').textContent = `${(testAcc * 100).toFixed(1)}%`;
|
| 408 |
+
|
| 409 |
+
// Create charts
|
| 410 |
+
createCharts(trainErrors, betasHistory, numRounds);
|
| 411 |
+
|
| 412 |
+
// Show results
|
| 413 |
+
resultsSection.classList.remove('hidden');
|
| 414 |
+
|
| 415 |
+
} catch (error) {
|
| 416 |
+
console.error('Error during training:', error);
|
| 417 |
+
alert('An error occurred during training. Please check console for details.');
|
| 418 |
+
} finally {
|
| 419 |
+
// Reset button state
|
| 420 |
+
trainButton.disabled = false;
|
| 421 |
+
buttonText.textContent = 'Train AdaBoost Model';
|
| 422 |
+
loadingSpinner.classList.add('hidden');
|
| 423 |
+
}
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
// Create visualization charts
|
| 427 |
+
function createCharts(trainErrors, betasHistory, numRounds) {
|
| 428 |
+
const rounds = Array.from({length: numRounds}, (_, i) => i + 1);
|
| 429 |
+
|
| 430 |
+
// Training error chart
|
| 431 |
+
const errorCtx = document.getElementById('errorChart').getContext('2d');
|
| 432 |
+
new Chart(errorCtx, {
|
| 433 |
+
type: 'line',
|
| 434 |
+
data: {
|
| 435 |
+
labels: rounds,
|
| 436 |
+
datasets: [{
|
| 437 |
+
label: 'Training Error',
|
| 438 |
+
data: trainErrors,
|
| 439 |
+
borderColor: 'rgba(79, 70, 229, 1)',
|
| 440 |
+
backgroundColor: 'rgba(79, 70, 229, 0.1)',
|
| 441 |
+
borderWidth: 2,
|
| 442 |
+
fill: true
|
| 443 |
+
}]
|
| 444 |
+
},
|
| 445 |
+
options: {
|
| 446 |
+
responsive: true,
|
| 447 |
+
maintainAspectRatio: false,
|
| 448 |
+
scales: {
|
| 449 |
+
y: {
|
| 450 |
+
beginAtZero: true,
|
| 451 |
+
title: {
|
| 452 |
+
display: true,
|
| 453 |
+
text: 'Error Rate'
|
| 454 |
+
}
|
| 455 |
+
},
|
| 456 |
+
x: {
|
| 457 |
+
title: {
|
| 458 |
+
display: true,
|
| 459 |
+
text: 'Boosting Round'
|
| 460 |
+
}
|
| 461 |
+
}
|
| 462 |
+
}
|
| 463 |
+
}
|
| 464 |
+
});
|
| 465 |
+
|
| 466 |
+
// Loss chart (using training error as proxy)
|
| 467 |
+
const lossCtx = document.getElementById('lossChart').getContext('2d');
|
| 468 |
+
new Chart(lossCtx, {
|
| 469 |
+
type: 'line',
|
| 470 |
+
data: {
|
| 471 |
+
labels: rounds,
|
| 472 |
+
datasets: [
|
| 473 |
+
{
|
| 474 |
+
label: 'Training Loss',
|
| 475 |
+
data: trainErrors,
|
| 476 |
+
borderColor: 'rgba(79, 70, 229, 1)',
|
| 477 |
+
backgroundColor: 'rgba(79, 70, 229, 0.1)',
|
| 478 |
+
borderWidth: 2,
|
| 479 |
+
fill: true
|
| 480 |
+
},
|
| 481 |
+
{
|
| 482 |
+
label: 'Validation Loss',
|
| 483 |
+
data: trainErrors.map(e => e * 1.1), // Placeholder
|
| 484 |
+
borderColor: 'rgba(220, 38, 38, 1)',
|
| 485 |
+
backgroundColor: 'rgba(220, 38, 38, 0.1)',
|
| 486 |
+
borderWidth: 2,
|
| 487 |
+
fill: true,
|
| 488 |
+
borderDash: [5, 5]
|
| 489 |
+
}
|
| 490 |
+
]
|
| 491 |
+
},
|
| 492 |
+
options: {
|
| 493 |
+
responsive: true,
|
| 494 |
+
maintainAspectRatio: false,
|
| 495 |
+
scales: {
|
| 496 |
+
y: {
|
| 497 |
+
beginAtZero: true,
|
| 498 |
+
title: {
|
| 499 |
+
display: true,
|
| 500 |
+
text: 'Loss'
|
| 501 |
+
}
|
| 502 |
+
},
|
| 503 |
+
x: {
|
| 504 |
+
title: {
|
| 505 |
+
display: true,
|
| 506 |
+
text: 'Boosting Round'
|
| 507 |
+
}
|
| 508 |
+
}
|
| 509 |
+
}
|
| 510 |
+
}
|
| 511 |
+
});
|
| 512 |
+
|
| 513 |
+
// Classifier weights chart
|
| 514 |
+
const weightsCtx = document.getElementById('weightsChart').getContext('2d');
|
| 515 |
+
new Chart(weightsCtx, {
|
| 516 |
+
type: 'line',
|
| 517 |
+
data: {
|
| 518 |
+
labels: rounds,
|
| 519 |
+
datasets: betasHistory[0].map((_, i) => ({
|
| 520 |
+
label: `Stump ${i+1}`,
|
| 521 |
+
data: betasHistory.map(round => round[i] || 0),
|
| 522 |
+
borderWidth: 1,
|
| 523 |
+
pointRadius: 0
|
| 524 |
+
}))
|
| 525 |
+
},
|
| 526 |
+
options: {
|
| 527 |
+
responsive: true,
|
| 528 |
+
maintainAspectRatio: false,
|
| 529 |
+
scales: {
|
| 530 |
+
y: {
|
| 531 |
+
beginAtZero: true,
|
| 532 |
+
title: {
|
| 533 |
+
display: true,
|
| 534 |
+
text: 'Classifier Weight (β)'
|
| 535 |
+
}
|
| 536 |
+
},
|
| 537 |
+
x: {
|
| 538 |
+
title: {
|
| 539 |
+
display: true,
|
| 540 |
+
text: 'Boosting Round'
|
| 541 |
+
}
|
| 542 |
+
}
|
| 543 |
+
}
|
| 544 |
+
}
|
| 545 |
+
});
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
// Initialize
|
| 549 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 550 |
+
document.getElementById('trainButton').addEventListener('click', runTraining);
|
| 551 |
+
});
|
| 552 |
+
</script>
|
| 553 |
+
<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=harshil09/space" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
|
| 554 |
+
</html>
|
prompts.txt
ADDED
|
File without changes
|