yang
This project has not described itself yet.
Brought by drakestapleton
yang / scripts / summarize_haar_shell.py
| 1 | """Compare a --haar-shell run to the corresponding archived --vertex-shell pairs.""" |
| 2 | import argparse |
| 3 | import csv |
| 4 | from itertools import groupby |
| 5 | |
| 6 | import numpy as np |
| 7 | |
| 8 | |
| 9 | PAIR = ('group', 'side', 'warmup', 'samples', 'stride', 'beta', 'start', 'seed', 'sample') |
| 10 | |
| 11 | |
| 12 | def summarize(rows, prior): |
| 13 | def paired(): |
| 14 | for row, ref in zip(rows, prior, strict=True): |
| 15 | if any(row[k] != ref[k] for k in PAIR): |
| 16 | raise ValueError('different chains or sample order') |
| 17 | for field in ('inner_j', 'outer_j'): |
| 18 | np.testing.assert_allclose(float(row[field]), float(ref[field]), atol=2e-9, rtol=2e-11) |
| 19 | yield row |
| 20 | |
| 21 | for key, batch in groupby(paired(), lambda r: (r['group'], r['beta'], r['start'])): |
| 22 | batch = list(batch) |
| 23 | def col(k): |
| 24 | return np.array([float(r[k]) for r in batch]) |
| 25 | a = np.abs(np.tanh(col('inner_j'))) |
| 26 | with np.errstate(divide='ignore'): |
| 27 | log_a = np.log(a) |
| 28 | def weighted_log10(log_values): |
| 29 | if not a.any(): |
| 30 | return float('nan') # preceding weight is zero |
| 31 | return (np.logaddexp.reduce(log_a + log_values) - np.log(a.sum())) / np.log(10.) |
| 32 | draws, neg = col('draws'), col('negative') |
| 33 | # Haar symmetry makes |E tanh(C+X)| <= tanh(|C|) exactly. |
| 34 | log_floor = np.log(2.) - np.logaddexp(0., 2*np.abs(col('center'))) |
| 35 | print(*key, len(a), int(draws.sum()), int(neg.sum()), f'{weighted_log10(log_floor):.8g}', |
| 36 | *(f'{weighted_log10(col(k)):.8g}' for k in ('log_p_upper', 'log_loss_upper', 'log_draw_loss'))) |
| 37 | |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | parser = argparse.ArgumentParser(description=__doc__) |
| 41 | parser.add_argument('haar', type=argparse.FileType('r')) |
| 42 | parser.add_argument('vertex', type=argparse.FileType('r')) |
| 43 | args = parser.parse_args() |
| 44 | print('group beta start pairs draws negative log10_weighted_loss_floor log10_weighted_p_ceiling log10_weighted_loss_ceiling log10_weighted_draw_loss') |
| 45 | summarize(*(csv.DictReader(f, delimiter='\t') for f in (args.haar, args.vertex))) |