reshapeVectorToMatrix function

Tensor<Matrix> reshapeVectorToMatrix(
  1. Tensor<Vector> v,
  2. int numRows,
  3. int numCols
)

Implementation

Tensor<Matrix> reshapeVectorToMatrix(
  Tensor<Vector> v,
  int numRows,
  int numCols,
) {
  Matrix outValue = [];
  int index = 0;
  for (int i = 0; i < numRows; i++) {
    Vector row = [];
    for (int j = 0; j < numCols; j++) {
      row.add(v.value[index]);
      index++;
    }
    outValue.add(row);
  }
  Tensor<Matrix> out = Tensor<Matrix>(outValue);
  out.creator = Node(
    [v],
    () {
      int index = 0;
      for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
          v.grad[index] += out.grad[i][j];
          index++;
        }
      }
    },
    opName: 'reshape',
    // <-- CRITICAL: Storing the non-Tensor parameters
    extraParams: {'numRows': numRows, 'numCols': numCols},
    cost: 0,
  );
  return out;
}