mseMatrix function
Implementation
Tensor<Scalar> mseMatrix(Tensor<Matrix> predictions, Tensor<Matrix> targets) {
int length = predictions.data.length;
double sumSquaredError = 0.0;
for (int i = 0; i < length; i = i + 1) {
double error = predictions.data[i] - targets.data[i];
sumSquaredError = sumSquaredError + (error * error);
}
Tensor<Scalar> out = Tensor<Scalar>(sumSquaredError / length);
out.creator = Node(
[predictions, targets],
() {
double factor = 2.0 / length;
double outGrad = out.grad[0];
for (int i = 0; i < length; i = i + 1) {
predictions.grad[i] = predictions.grad[i] +
outGrad * factor * (predictions.data[i] - targets.data[i]);
}
},
opName: 'mse_matrix',
cost: 3 * length,
);
return out;
}