Chapter 20 of 22 Part IV: Information Processing

Inside the Mind of Artificial Intelligence

5 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

Sigmoid (Ch. 20 Example 1)

File: block-01.js (byte-identical) · 533 bytes · SHA-256: 116187413c22…
machine learning Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

const trainData = [
  [0.1, 0], [0.2, 0], [0.3, 0],
  [0.7, 1], [0.8, 1], [0.9, 1]
];
function sigmoid(z) { return 1 / (1 + Math.exp(-z)); }
function train(data, epochs = 400) {
  let w = 0, b = 0;
  for (let epoch = 0; epoch < epochs; epoch++) {
    let dw = 0, db = 0;
    for (const [x, y] of data) {
      const error = sigmoid(w*x+b) - y;
      dw += error*x; db += error;
    }
    w -= 0.5*dw/data.length;
    b -= 0.5*db/data.length;
  }
  return {w,b};
}
function predict(model, x) {
  return sigmoid(model.w*x + model.b);
}
Download .js

Model — Inside the Mind of Artificial Intelligence (Example 2)

File: block-02.js (byte-identical) · 124 bytes · SHA-256: 424c8609a221…
machine learning Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const model = train(trainData);
for (const x of [0.15, 0.45, 0.55, 0.85]) {
  console.log(x, predict(model,x).toFixed(3));
}
Download .js

Next Words (Ch. 20 Example 3)

File: block-03.js (byte-identical) · 623 bytes · SHA-256: 09347b118a3a…
machine learning Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

const corpus = [
  'the sky is blue', 'the sky is clear',
  'the sky is blue', 'the lake is calm'
];
function nextWords(lines, word) {
  const counts = new Map();
  for (const line of lines) {
    const tokens = line.toLowerCase().match(/[a-z]+/g) || [];
    for (let i = 0; i + 1 < tokens.length; i++) {
      if (tokens[i] === word.toLowerCase()) {
        const t = tokens[i+1];
        counts.set(t, (counts.get(t)||0)+1);
      }
    }
  }
  const total = [...counts.values()].reduce((a,b)=>a+b,0);
  return [...counts].map(([word,n]) => ({word,p:n/total}))
    .sort((a,b)=>b.p-a.p || a.word.localeCompare(b.word));
}
Download .js

Notes — Inside the Mind of Artificial Intelligence (Example 4)

File: block-04.js (byte-identical) · 260 bytes · SHA-256: 6d9a403b776f…
machine learning Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

const notes = [
  {id: 'C03', text: 'A barcode identifies a product.'},
  {id: 'C04', text: 'QR codes use error correction.'},
  {id: 'C10', text: 'GPS receivers estimate position using signals.'},
  {id: 'C13', text: 'A hash helps detect changes in data.'}
];
Download .js

Terms (Ch. 20 Example 5)

File: block-05.js (byte-identical) · 624 bytes · SHA-256: 2aed6bfb569f…
machine learning Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function terms(s) {
  const stop = new Set(['a','an','the','is','what','does','how','in']);
  return new Set((s.toLowerCase().match(/[a-z]+/g)||[])
    .filter(t => !stop.has(t)));
}
function answer(question, library = notes) {
  const q = terms(question);
  const scored = library.map(n => ({...n, score:
    [...terms(n.text)].filter(t=>q.has(t)).length}));
  scored.sort((a,b)=>b.score-a.score);
  if (!scored.length || scored[0].score < 1 ||
      (scored[1] && scored[1].score === scored[0].score))
    return 'I need more evidence or a clearer question.';
  return scored[0].text + ' [Source: ' + scored[0].id + ']';
}
Download .js