| 39 | } |
| 40 | |
| 41 | export function showTestResults(batch, predictions, labels) { |
| 42 | statusElement.innerText = 'Testing...'; |
| 43 | |
| 44 | const testExamples = batch.xs.shape[0]; |
| 45 | let totalCorrect = 0; |
| 46 | for (let i = 0; i < testExamples; i++) { |
| 47 | const image = batch.xs.slice([i, 0], [1, batch.xs.shape[1]]); |
| 48 | |
| 49 | const div = document.createElement('div'); |
| 50 | div.className = 'pred-container'; |
| 51 | |
| 52 | const canvas = document.createElement('canvas'); |
| 53 | draw(image.flatten(), canvas); |
| 54 | |
| 55 | const pred = document.createElement('div'); |
| 56 | |
| 57 | const prediction = predictions[i]; |
| 58 | const label = labels[i]; |
| 59 | const correct = prediction === label; |
| 60 | if (correct) { |
| 61 | totalCorrect++; |
| 62 | } |
| 63 | |
| 64 | pred.className = `pred ${(correct ? 'pred-correct' : 'pred-incorrect')}`; |
| 65 | pred.innerText = `pred: ${prediction}`; |
| 66 | |
| 67 | div.appendChild(pred); |
| 68 | div.appendChild(canvas); |
| 69 | |
| 70 | imagesElement.appendChild(div); |
| 71 | } |
| 72 | |
| 73 | const accuracy = 100 * totalCorrect / testExamples; |
| 74 | const displayStr = |
| 75 | `Accuracy: ${accuracy.toFixed(2)}% (${totalCorrect} / ${testExamples})`; |
| 76 | messageElement.innerText = `${displayStr}\n`; |
| 77 | console.log(displayStr); |
| 78 | } |
| 79 | |
| 80 | export function draw(image, canvas) { |
| 81 | const [width, height] = [28, 28]; |