leakyReluVector function

Tensor<Vector> leakyReluVector(
  1. Tensor<Vector> v,
  2. double alpha
)

Implementation

Tensor<Vector> leakyReluVector(Tensor<Vector> v, double alpha) {
  int N = v.data.length;
  Vector outValue = [];

  for (int i = 0; i < N; i = i + 1) {
    double x = v.data[i];
    if (x > 0.0) {
      outValue.add(x);
    } else {
      outValue.add(alpha * x);
    }
  }

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

  out.creator = Node(
    [v],
        () {
      for (int i = 0; i < N; i = i + 1) {
        double x = v.data[i];
        double grad = x > 0.0 ? 1.0 : alpha;
        v.grad[i] = v.grad[i] + out.grad[i] * grad;
      }
    },
    opName: 'leaky_relu_vector',
    cost: N,
  );

  return out;
}