So, I've managed to solve this issue, it's mainly due to my malformed input which cause Mathematica stuck. I leave it here for anyone who have the similar issue.
Below is the minimal reproducible test case(still tediously long). You'll found there is a minor difference(there is an empty superscript after a[[i]]
) but not visually noticeable, and my InputForm rewrote in the question was also incorrect(mse = Sum[y[[i]] - (a x[[i]]^2 + b x[[i]] + c), {i, 1, n}]^2 / n;
) /facepalm.
derivativeTest[{x_, y_}] :=
Module[{ n, mseOrig, mseStandard, mseInput, gradInput,
gradStandard, gradSelf},
(*MSE loss*)
n = Length[x];
mseOrig = 1/n \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]
\*SuperscriptBox[\((y[\([\)\(i\)\(]\)]\ - \ \((a\ \
\*SuperscriptBox[\(x[\([\)\(i\)\(]\)]\), \(\ \)] x[\([i]\)] + \
b\ x[\([\)\(i\)\(]\)]\ + \ c)\)\ )\), \(2\)]\) ;
mseStandard = 1/n \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]
\*SuperscriptBox[\((y[\([\)\(i\)\(]\)]\ - \ \((a\ \ x[\([i]\)] \
x[\([i]\)] + \ b\ x[\([\)\(i\)\(]\)]\ + \ c)\)\ )\), \(2\)]\) ;
mseInput =
1/n * Sum[(y[[i]] - (a x[[i]]^2 + b x[[i]] + c))^2, {i, 1, n}];
gradInput = {D[mseInput, a], D[mseInput, b], D[mseInput, c]};
gradStandard = {D[mseStandard, a], D[mseStandard, b],
D[mseStandard, c]};
gradSelf = {
-2/n \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]\(
\*SuperscriptBox[\(x[\([\)\(i\)\(]\)]\), \(2\)] \((y[\([i]\)]\ - \ \
\((a\
\*SuperscriptBox[\(x[\([\)\(i\)\(]\)]\), \(2\)] + b\ x[\([i]\)]\ + \
c)\))\)\)\),
-2/n \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i =
1\), \(n\)]\(x[\([i]\)] \((y[\([i]\)]\ - \ \((a\
\*SuperscriptBox[\(x[\([\)\(i\)\(]\)]\), \(2\)] + b\ x[\([i]\)]\ + \
c)\))\)\)\),
-2/n \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i =
1\), \(n\)]\((y[\([i]\)]\ - \ \((a\
\*SuperscriptBox[\(x[\([\)\(i\)\(]\)]\), \(2\)] + b\ x[\([i]\)]\ + \
c)\))\)\)
};
Grid[{{"MSE Original", "MSE StandardForm",
"MSE InputForm"}, {mseOrig // Simplify,
mseStandard // Simplify , mseInput // Simplify}}, Frame -> All]
Grid[{{"Gradient by StandardForm", "Gradient by InputForm",
"Gradient by hand"}, { gradStandard // Simplify,
gradInput // Simplify , gradSelf // Simplify }}, Frame -> All]
];