dropoutVectorMath function

Tensor<Vector> dropoutVectorMath(
  1. Tensor<Vector> input,
  2. double rate,
  3. bool isTraining
)

If isTraining is set to true this function sets values with the probability rate to zero. The remaining values get scaled up tp conserve the total sum of the values.

Implementation

Tensor<Vector> dropoutVectorMath(
    Tensor<Vector> input, double rate, bool isTraining) {
  if (isTraining == false || rate == 0.0) {
    return input;
  }

  double scale = 1.0 / (1.0 - rate);
  Random random = Random();
  int length = input.data.length;

  Vector outputValue = [];
  List<bool> mask = [];

  for (int i = 0; i < length; i = i + 1) {
    if (random.nextDouble() < rate) {
      outputValue.add(0.0);
      mask.add(false);
    } else {
      outputValue.add(input.data[i] * scale);
      mask.add(true);
    }
  }

  Tensor<Vector> out = Tensor<Vector>(outputValue);

  out.creator = Node(
    [input],
    () {
      for (int i = 0; i < length; i = i + 1) {
        if (mask[i]) {
          input.grad[i] = input.grad[i] + out.grad[i] * scale;
        }
      }
    },
    opName: 'dropout_vector',
    cost: length,
  );

  return out;
}