45 template<
typename Architecture,
typename F,
typename dF>
47 typename Architecture::Scalar_t dx)
48 ->
typename Architecture::Scalar_t
50 using Scalar_t =
typename Architecture::Scalar_t;
51 using Matrix_t =
typename Architecture::Matrix_t;
53 Scalar_t maximum_error = 0.0;
55 for (
size_t i = 0; i < 100; i++)
57 Matrix_t
X(10,10),
Y(10,10);
71 Scalar_t dy_num = (y1 - y0) / (2.0 * dx);
73 maximum_error = std::max(maximum_error, error);
82 template<
typename Architecture>
84 ->
typename Architecture::Scalar_t
86 using Scalar_t =
typename Architecture::Scalar_t;
87 using Matrix_t =
typename Architecture::Matrix_t;
90 std::vector<EActivationFunction> EActivationFunctions
97 Scalar_t error, maximum_error;
100 for (
auto & af : EActivationFunctions)
102 auto f = [& af](Matrix_t &
X){ evaluate<Architecture>(
X, af);};
103 auto df = [& af](Matrix_t &
X,
const Matrix_t &
Y)
105 evaluateDerivative<Architecture>(
X, af,
Y);
107 error = testDerivatives<Architecture>(
f, df, 1.0e-04);
109 std::cout <<
"Testing " <<
static_cast<int>(af) <<
": ";
110 std::cout <<
"Maximum Relative Error = " << error << std::endl;
112 maximum_error = std::max(maximum_error, error);
115 return maximum_error;
127 template<
typename Architecture,
typename F,
typename dF>
129 typename Architecture::Scalar_t dx)
130 ->
typename Architecture::Scalar_t
132 using Scalar_t =
typename Architecture::Scalar_t;
133 using Matrix_t =
typename Architecture::Matrix_t;
135 Scalar_t maximum_error = 0.0;
137 for (
size_t i = 0; i < 100; i++)
139 Matrix_t
X(10,10),
Y(10,10),
Z(10,10);
144 Scalar_t dy =
Z(0,0);
147 Scalar_t y1 =
f(
Y,
X);
149 Scalar_t y0 =
f(
Y,
X);
150 Scalar_t dy_num = (y1 - y0) / (2.0 * dx);
152 Scalar_t error = 0.0;
160 maximum_error = std::max(maximum_error, error);
163 return maximum_error;
170 template<
typename Architecture>
172 ->
typename Architecture::Scalar_t
174 using Scalar_t =
typename Architecture::Scalar_t;
175 using Matrix_t =
typename Architecture::Matrix_t;
177 std::vector<ELossFunction> LossFunctions
182 Scalar_t error, maximum_error;
185 for (
auto & lf : LossFunctions)
187 auto f = [lf](
const Matrix_t &
Y,
const Matrix_t &
Z)
189 return evaluate<Architecture>(lf,
Y,
Z);
191 auto df = [& lf](Matrix_t &
X,
195 evaluateGradients<Architecture>(
X, lf,
Y,
Z);
198 error = testGradients<Architecture>(
f, df, 5
e-6);
200 std::cout <<
"Testing " <<
static_cast<char>(lf) <<
": ";
201 std::cout <<
"Maximum Relative Error = " << error << std::endl;
203 maximum_error = std::max(maximum_error, error);
206 return maximum_error;
217 template<
typename Architecture>
219 ->
typename Architecture::Scalar_t
221 using Scalar_t =
typename Architecture::Scalar_t;
222 using Matrix_t =
typename Architecture::Matrix_t;
224 std::vector<ERegularization> Regularizations
228 Scalar_t error, maximum_error;
231 for (
auto &
r : Regularizations)
233 auto f = [
r](
const Matrix_t & ,
const Matrix_t &
Y)
235 return regularization<Architecture>(
Y,
r);
237 auto df = [&
r](Matrix_t &
X,
242 addRegularizationGradients<Architecture>(
X,
Y, (Scalar_t) 1.0,
r);
245 error = testGradients<Architecture>(
f, df, 1.0);
247 std::cout <<
"Testing " <<
static_cast<char>(
r) <<
": ";
248 std::cout <<
"Maximum Relative Error = " << error << std::endl;
250 maximum_error = std::max(maximum_error, error);
253 return maximum_error;
auto testLossFunctionGradients() -> typename Architecture::Scalar_t
Test gradients of all loss function for the given architecture type and return the maximum relative e...
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
void applyMatrix(AMatrix &X, F f)
Apply functional to each element in the matrix.
auto testDerivatives(F f, dF df, typename Architecture::Scalar_t dx) -> typename Architecture::Scalar_t
Generic function that numerically computes the derivative of a matrix function f and the analytical s...
auto testActivationFunctionDerivatives() -> typename Architecture::Scalar_t
Test derivatives of all activation functions and return the maximum relative error.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
void copyMatrix(AMatrix &X, const AMatrix &Y)
Generate a random batch as input for a neural net.
T relativeError(const T &x, const T &y)
Compute the relative error of x and y.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
auto testGradients(F f, dF df, typename Architecture::Scalar_t dx) -> typename Architecture::Scalar_t
Similar to testDerivatives only that here the mathematical function is expected to be a matrix functi...
auto testRegularizationGradients() -> typename Architecture::Scalar_t
Test the computation of gradients for all differentiable regularization types, which is so far only L...