tanhMatrix function

Tensor<Matrix> tanhMatrix(
  1. Tensor<Matrix> m
)

Implementation

Tensor<Matrix> tanhMatrix(Tensor<Matrix> m) {
  double _tanh(double x) {
    var e2x = exp(2 * x);
    if (e2x.isInfinite) return 1.0;
    return (e2x - 1) / (e2x + 1);
  }

  int numRows = m.value.length;
  int numCols = m.value[0].length;
  Matrix outValue = [];
  for (int i = 0; i < numRows; i++) {
    Vector row = [];
    for (int j = 0; j < numCols; j++) {
      row.add(_tanh(m.value[i][j]));
    }
    outValue.add(row);
  }
  Tensor<Matrix> out = Tensor<Matrix>(outValue);
  out.creator = Node(
    [m],
    () {
      for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
          m.grad[i][j] += out.grad[i][j] * (1 - pow(out.value[i][j], 2));
        }
      }
    },
    opName: 'tanh_matrix', // <-- Renamed for clarity
    cost: numRows * numCols,
  );
  return out;
}