sigmoidMatrix function

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

Implementation

Tensor<Matrix> sigmoidMatrix(Tensor<Matrix> m) {
  int numRows = m.shape[0];
  int numCols = m.shape[1];

  Matrix mMat = m.value;

  Matrix outValue = [];
  for (int i = 0; i < numRows; i = i + 1) {
    Vector row = [];
    for (int j = 0; j < numCols; j = j + 1) {
      row.add(1.0 / (1.0 + exp(-mMat[i][j])));
    }
    outValue.add(row);
  }

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

  out.creator = Node(
    [m],
        () {
      int length = m.data.length;
      for (int i = 0; i < length; i = i + 1) {
        double val = out.data[i];
        m.grad[i] = m.grad[i] + out.grad[i] * (val * (1.0 - val));
      }
    },
    opName: 'sigmoid_matrix',
    cost: numRows * numCols,
  );
  return out;
}