sumMatrix function
Implementation
Tensor<Scalar> sumMatrix(Tensor<Matrix> m) {
int numRows = m.value.length;
int numCols = m.value[0].length;
double total = 0.0;
for (Vector row in m.value) {
for (double val in row) {
total += val;
}
}
Tensor<Scalar> out = Tensor<Scalar>(total);
out.creator = Node(
[m],
() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
m.grad[i][j] += out.grad * 1.0;
}
}
},
opName: 'sum_matrix', // <-- Renamed for clarity
cost: numRows * numCols,
);
return out;
}