LayerSeparation.measure constructor

LayerSeparation.measure(
  1. List<Float32List> setA,
  2. List<Float32List> setB
)

Measures setA against setB; both non-empty, all rows the same length. Costs O(rows × hidden size): each held-out mean comes from the full one, (n mean - r) / (n - 1).

Degenerate cases:

  • a zero full-sample direction: means 0, separation 0;
  • a set of one row cannot be held out: its mean and deviation are null, and so is separation;
  • a zero held-out direction projects its row to 0;
  • spreads of zero, or at the level of rounding (1e-9 of the means): separation is plus or minus infinity by the sign of the gap, or 0 without one.

Implementation

factory LayerSeparation.measure(
  List<Float32List> setA,
  List<Float32List> setB,
) {
  if (setA.isEmpty || setB.isEmpty) {
    throw ArgumentError('both sets need at least one row');
  }
  final h = setA.first.length;
  if ([...setA, ...setB].any((r) => r.length != h)) {
    throw ArgumentError('rows of different lengths');
  }
  Float64List mean(List<Float32List> rows) {
    final m = Float64List(h);
    for (final r in rows) {
      for (var j = 0; j < h; j++) {
        m[j] += r[j];
      }
    }
    for (var j = 0; j < h; j++) {
      m[j] /= rows.length;
    }
    return m;
  }

  final ma = mean(setA), mb = mean(setB);
  final direction = Float32List(h);
  var squares = 0.0;
  for (var j = 0; j < h; j++) {
    direction[j] = ma[j] - mb[j];
    squares += direction[j] * direction[j];
  }
  final norm = math.sqrt(squares);
  if (norm == 0) {
    return LayerSeparation._(direction, 0, 0, null, 0, null, 0);
  }

  // Each row of [rows] (whose mean is [own]) projected on the unit
  // direction between [own] without that row and [other], oriented from
  // B to A by [sign] (1 for A's rows, -1 for B's).
  final held = Float64List(h);
  List<double>? heldOut(
    List<Float32List> rows,
    Float64List own,
    Float64List other,
    double sign,
  ) {
    final n = rows.length;
    if (n < 2) return null;
    return [
      for (final r in rows)
        () {
          var squares = 0.0, dot = 0.0;
          for (var j = 0; j < h; j++) {
            final without = (n * own[j] - r[j]) / (n - 1);
            held[j] = sign * (without - other[j]);
            squares += held[j] * held[j];
            dot += r[j] * held[j];
          }
          return squares == 0 ? 0.0 : dot / math.sqrt(squares);
        }(),
    ];
  }

  (double?, double?) stats(List<double>? p) {
    if (p == null) return (null, null);
    final m = p.reduce((a, b) => a + b) / p.length;
    final v =
        p.map((x) => (x - m) * (x - m)).reduce((a, b) => a + b) /
        (p.length - 1);
    return (m, math.sqrt(v));
  }

  final (meanA, sdA) = stats(heldOut(setA, ma, mb, 1));
  final (meanB, sdB) = stats(heldOut(setB, mb, ma, -1));
  double? separation;
  if (sdA != null && sdB != null) {
    final pooled = math.sqrt((sdA * sdA + sdB * sdB) / 2);
    final gap = meanA! - meanB!;
    // Rows that are equal within a set (a shallow layer that sees only
    // a shared prompt ending) spread by rounding alone; that is no spread.
    final rounding = 1e-9 * (meanA.abs() + meanB.abs());
    separation = pooled > rounding
        ? gap / pooled
        : gap.abs() <= rounding
        ? 0
        : gap.sign * double.infinity;
  }
  return LayerSeparation._(
    direction,
    norm,
    meanA,
    sdA,
    meanB,
    sdB,
    separation,
  );
}