vectorTanh function

Tensor<Vector> vectorTanh(
  1. Tensor<Vector> v
)

Implementation

Tensor<Vector> vectorTanh(Tensor<Vector> v) {
  int N = v.data.length;
  Vector outValue = [];
  for (int i = 0; i < N; i = i + 1) {
    double x = v.data[i];
    double e2x = exp(2.0 * x);
    if (e2x.isInfinite) {
      outValue.add(1.0);
    } else {
      outValue.add((e2x - 1.0) / (e2x + 1.0));
    }
  }
  Tensor<Vector> out = Tensor<Vector>(outValue);
  out.creator = Node(
    [v],
        () {
      for (int i = 0; i < N; i = i + 1) {
        double val = out.data[i];
        v.grad[i] = v.grad[i] + out.grad[i] * (1.0 - (val * val));
      }
    },
    opName: 'tanh_vector',
    cost: N,
  );
  return out;
}