Chapter 19 of 22 Part IV: Data, Media & Machine Intelligence

When Machines Learn to See

4 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

block-01.js

Printed book snippet (byte-identical) · 296 bytes · SHA-256: d3a301428fae…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

function edges(grid, threshold = 80) {
  return grid.map((row, y) => row.map((v, x) => {
    const dx = x + 1 < row.length ?
      Math.abs(v - row[x + 1]) : 0;
    const dy = y + 1 < grid.length ?
      Math.abs(v - grid[y + 1][x]) : 0;
    return Math.max(dx, dy) >= threshold ? 1 : 0;
  }));
}
Download .js

block-02.js

Printed book snippet (byte-identical) · 94 bytes · SHA-256: f825b00ad707…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const sample = [[20,20,200],[20,20,200]];
console.log(edges(sample, 80));
// [[0,1,0],[0,1,0]]
Download .js

block-03.js

Printed book snippet (byte-identical) · 638 bytes · SHA-256: 998774decfa4…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

const templates = [
  '111101101101111', '010110010010111',
  '111001111100111', '111001111001111',
  '101101111001001', '111100111001111',
  '111100111101111', '111001001001001',
  '111101111101111', '111101111001111'
];
function recognize(bits) {
  if (!/^[01]{15}$/.test(bits)) throw Error('15 bits needed');
  const ranked = templates.map((t, digit) => ({
    digit, errors: [...t].filter((b,i) => b !== bits[i]).length
  })).sort((a,b) => a.errors - b.errors);
  const best = ranked[0];
  const tied = ranked[1].errors === best.errors;
  return { digit: tied || best.errors > 2 ? null : best.digit,
    errors: best.errors, tied };
}
Download .js

block-04.js

Printed book snippet (byte-identical) · 494 bytes · SHA-256: 1746cd178afd…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

function evaluate(samples, threshold) {
  const result = {tp:0, fp:0, tn:0, fn:0};
  for (const {score, same} of samples) {
    const accepted = score >= threshold;
    if (same) result[accepted ? 'tp' : 'fn']++;
    else result[accepted ? 'fp' : 'tn']++;
  }
  return result;
}
const samples = [
  {score:.95,same:true}, {score:.88,same:true},
  {score:.76,same:true}, {score:.61,same:true},
  {score:.81,same:false}, {score:.64,same:false},
  {score:.42,same:false}, {score:.22,same:false}
];
Download .js