24 template <
typename AReal>
25 AReal TReference<AReal>::MeanSquaredError(
const TMatrixT<AReal> &Y,
const TMatrixT<AReal> &output,
26 const TMatrixT<AReal> &weights)
33 for (
size_t i = 0; i < m; i++) {
34 for (
size_t j = 0; j < n; j++) {
35 AReal dY = (Y(i,j) - output(i,j));
36 result += weights(i, 0) * dY * dY;
39 result /=
static_cast<AReal
>(m * n);
44 template <
typename AReal>
45 void TReference<AReal>::MeanSquaredErrorGradients(TMatrixT<AReal> &dY,
const TMatrixT<AReal> &Y,
46 const TMatrixT<AReal> &output,
const TMatrixT<AReal> &weights)
53 dY *= -2.0 /
static_cast<AReal
>(m * n);
55 for (
size_t i = 0; i < m; i++) {
56 for (
size_t j = 0; j < n; j++) {
57 dY(i, j) *= weights(i, 0);
63 template <
typename AReal>
64 AReal TReference<AReal>::CrossEntropy(
const TMatrixT<AReal> &Y,
const TMatrixT<AReal> &output,
65 const TMatrixT<AReal> &weights)
72 for (
size_t i = 0; i < m; i++) {
73 AReal w = weights(i, 0);
74 for (
size_t j = 0; j < n; j++) {
75 AReal sig = 1.0 / (1.0 + std::exp(-output(i,j)));
76 result += w * (Y(i, j) * std::log(sig) + (1.0 - Y(i, j)) * std::log(1.0 - sig));
79 result /= -
static_cast<AReal
>(m * n);
84 template <
typename AReal>
85 void TReference<AReal>::CrossEntropyGradients(TMatrixT<AReal> &dY,
const TMatrixT<AReal> &Y,
86 const TMatrixT<AReal> &output,
const TMatrixT<AReal> &weights)
92 AReal norm = 1.0 /
static_cast<AReal
>(m * n);
93 for (
size_t i = 0; i < m; i++)
95 AReal w = weights(i, 0);
96 for (
size_t j = 0; j < n; j++)
99 AReal sig = 1.0 / (1.0 + std::exp(-output(i,j)));
100 dY(i, j) = norm * w * (sig - y);
106 template <
typename AReal>
107 AReal TReference<AReal>::SoftmaxCrossEntropy(
const TMatrixT<AReal> &Y,
const TMatrixT<AReal> &output,
108 const TMatrixT<AReal> &weights)
115 for (
size_t i = 0; i < m; i++) {
117 AReal w = weights(i, 0);
118 for (
size_t j = 0; j < n; j++) {
119 sum += exp(output(i,j));
121 for (
size_t j = 0; j < n; j++) {
122 result += w * Y(i, j) * log(exp(output(i, j)) / sum);
125 result /= -
static_cast<AReal
>(m);
130 template <
typename AReal>
131 void TReference<AReal>::SoftmaxCrossEntropyGradients(TMatrixT<AReal> &dY,
const TMatrixT<AReal> &Y,
132 const TMatrixT<AReal> &output,
const TMatrixT<AReal> &weights)
137 AReal norm = 1.0 / m ;
139 for (
size_t i = 0; i < m; i++)
143 AReal w = weights(i, 0);
144 for (
size_t j = 0; j < n; j++) {
145 sum += exp(output(i,j));
148 for (
size_t j = 0; j < n; j++) {
149 dY(i, j) = w * norm * (exp(output(i, j)) / sum * sumY - Y(i, j));