wolfDefenderPool function

double wolfDefenderPool(
  1. List<Float32List> logits
)

The injection probability of the windows' logits: per class, a smooth maximum over the windows (temperature 1 for one window, 0.1 for more), normalized by the window count; then softmax. One window is the softmax of its logits. Patronus's inference.py defines it.

Implementation

double wolfDefenderPool(List<Float32List> logits) {
  if (logits.isEmpty) throw ArgumentError.value(logits, 'logits', 'empty');
  final temperature = logits.length == 1 ? 1.0 : 0.1;
  final logCount = math.log(logits.length);
  final pooled = List<double>.filled(2, 0);
  for (var c = 0; c < 2; c++) {
    var maximum = double.negativeInfinity;
    for (final window in logits) {
      maximum = math.max(maximum, window[c]);
    }
    var sum = 0.0;
    for (final window in logits) {
      sum += math.exp((window[c] - maximum) / temperature);
    }
    pooled[c] = maximum + temperature * (math.log(sum) - logCount);
  }
  // Softmax of two values: the probability of class 1.
  return 1 / (1 + math.exp(pooled[0] - pooled[1]));
}