void Tempcoder::AddJittering()

in tempcoding/tempcoder.cc [504:543]


void Tempcoder::AddJittering(const JitteringParams& params,
                             VectorXd* activations) {
  if (params.sigma == 0.0 && params.new_spike_probability == 0.0) {
    return;
  }

  // Compute the maximum time of a spike in this layer.
  // Initialized to a negative value (which is invalid as a spike time), as we
  // do not know if any activation time is not kNoSpike.
  double max_original_time = -kNoSpike;
  for (double spike : *activations) {
    if (spike != kNoSpike && spike > max_original_time) {
      max_original_time = spike;
    }
  }

  // If no neuron in this layer spiked, max_original_time will still be
  // negative: nothing cannot be done in this case.
  if (max_original_time < 0.0) return;

  std::default_random_engine generator(std::random_device{}());
  std::normal_distribution<double> dist(params.mean, params.sigma);
  std::bernoulli_distribution bernoulli(params.new_spike_probability);

  for (double& spike : *activations) {
    if (spike == kNoSpike) {
      if (bernoulli(generator)) {
        // Create a spike with the maximum seen spike time plus some random
        // noise.
        spike = max_original_time + dist(generator);
      }
      continue;
    }
    spike += dist(generator);
    // Avoid negative time.
    if (spike < 0.0) {
      spike = 0.0;
    }
  }
}