File size: 1,791 Bytes
ebf8f81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Which way of forgetting is better once a story is longer than the model's memory?
//   "slide":  each new piece overwrites the oldest one (no pause)
//   "reread": throw away the oldest half and re-read the rest (a pause of ~1 second)
// Measures how well the model predicts real story text beyond its memory size. Lower is better.
//   node tests/window_test.js web/storyteller.bin data/val.bin
const fs = require("fs"), path = require("path");
const Storyteller = require(path.resolve(__dirname, "../web/storyteller.js"));
const b = fs.readFileSync(process.argv[2]), v = fs.readFileSync(process.argv[3]);
const val = new Uint16Array(v.buffer, v.byteOffset, v.length / 2);
const st = new Storyteller(b.buffer.slice(b.byteOffset, b.byteOffset + b.length));
const C = st.cfg.ctx, SEG = +process.argv[4] || 10, LEN = 640;
const nll = () => { let m = -Infinity; for (const x of st.logits) if (x > m) m = x; let z = 0; for (const x of st.logits) z += Math.exp(x - m); return (t) => -(st.logits[t] - m - Math.log(z)); };
const out = {};
for (const mode of ["slide", "reread"]) {
  st.mode = mode; let inside = 0, nIn = 0, beyond = 0, nBe = 0; const t0 = Date.now();
  for (let s = 0; s < SEG; s++) {
    const off = 1000 + s * 250000;
    st.reset([]); st.pos = 0; st.tokens = [];            // start with no start marker, like a training window
    for (let i = 0; i < LEN; i++) {
      st.push(val[off + i]);
      const l = nll()(val[off + i + 1]);
      if (i < C - 1) { inside += l; nIn++; } else { beyond += l; nBe++; }
    }
  }
  out[mode] = { inside: inside / nIn, beyond: beyond / nBe };
  console.log(`${mode.padEnd(7)} loss inside memory ${out[mode].inside.toFixed(4)} | beyond memory ${out[mode].beyond.toFixed(4)} (${nBe} pieces, ${((Date.now() - t0) / 1000).toFixed(0)} s)`);
}