Skip to content

Fix for testArithmeticCpu from TMVA #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions tmva/tmva/test/DNN/TestMatrixArithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ auto testMultiplication(size_t ntests)
n = rand() % 100 + 1;
k = rand() % 100 + 1;

TMatrixT<Double_t> ARef(m,k), A2Ref(m,k), ATRef(k,m) , BRef(k,n),
BTRef(n,k), CRef(m,n);
TMatrixT<Scalar_t> ARef(m,k), A2Ref(m,k), ATRef(k,m) , BRef(k,n), BTRef(n,k), CRef(m,n);
TMVA::DNN::randomMatrix(ARef);
TMVA::DNN::randomMatrix(A2Ref);
TMVA::DNN::randomMatrix(ATRef);
Expand All @@ -50,19 +49,19 @@ auto testMultiplication(size_t ntests)
// A * B
CRef.Mult(ARef,BRef);
Architecture_t::Multiply(C, A, B);
Scalar_t error = TMVA::DNN::maximumRelativeError((TMatrixT<Double_t>) C, CRef);
Scalar_t error = TMVA::DNN::maximumRelativeError(C, CRef);
maximumError = std::max(error, maximumError);

// A^T * B
CRef.TMult(ATRef,BRef);
Architecture_t::TransposeMultiply(C, AT, B);
error = TMVA::DNN::maximumRelativeError((TMatrixT<Double_t>) C, CRef);
error = TMVA::DNN::maximumRelativeError(C, CRef);
maximumError = std::max(error, maximumError);

// A * B^T
CRef.MultT(ARef,BTRef);
Architecture_t::MultiplyTranspose(C, A, BT);
error = TMVA::DNN::maximumRelativeError((TMatrixT<Double_t>) C, CRef);
error = TMVA::DNN::maximumRelativeError(C, CRef);
maximumError = std::max(error, maximumError);

// A .* B
Expand All @@ -72,7 +71,7 @@ auto testMultiplication(size_t ntests)
}
}
Architecture_t::Hadamard(A, A2);
error = TMVA::DNN::maximumRelativeError((TMatrixT<Double_t>) A, ARef);
error = TMVA::DNN::maximumRelativeError(A, ARef);
maximumError = std::max(error, maximumError);
}

Expand Down
65 changes: 25 additions & 40 deletions tmva/tmva/test/DNN/Utility.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TMVA_TEST_DNN_UTILITY
#define TMVA_TEST_DNN_UTILITY

#include <cassert>
#include <iostream>
#include <sstream>
#include <type_traits>
Expand Down Expand Up @@ -172,64 +173,48 @@ AFloat reduceMean(F f, AFloat start, const AMatrix &X)
return result / (AFloat) (m * n);
}

/** Compute the relative error of x and y normalized by y. Specialized for
* float and double to make sure both arguments are above expected machine
* precision (1e-5 and 1e-10). */
/** Compute the relative error of x and y */
//______________________________________________________________________________
template <typename AFloat>
inline AFloat relativeError(const AFloat &x,
const AFloat &y);
template <typename T>
inline T relativeError(const T &x, const T &y)
{
using std::abs;

if (x == y)
return T(0.0);

//______________________________________________________________________________
template <>
inline Double_t relativeError(const Double_t &x,
const Double_t &y)
{
if ((std::abs(x) > 1e-10) && (std::abs(y) > 1e-10)) {
return std::fabs((x - y) / y);
} else {
return std::fabs(x - y);
}
}
T diff = abs(x - y);

//______________________________________________________________________________
template <>
inline Real_t relativeError(const Real_t &x,
const Real_t &y)
{
if ((std::abs(x) > 1e-5) && (std::abs(y) > 1e-5)) {
return std::fabs((x - y) / y);
} else {
return std::fabs(x - y);
}
if (x * y == T(0.0) ||
diff < std::numeric_limits<T>::epsilon())
return diff;

return diff / (abs(x) + abs(y));
}

/*! Compute the maximum, element-wise relative error of the matrices
* X and Y normalized by the element of Y. Protected against division
* by zero. */
//______________________________________________________________________________
template <typename AMatrix>
auto maximumRelativeError(const AMatrix &X,
const AMatrix &Y)
-> decltype(X(0,0))
template <typename Matrix1, typename Matrix2>
auto maximumRelativeError(const Matrix1 &X, const Matrix2 &Y) -> decltype(X(0,0))
{
decltype(X(0,0)) curError, maxError = 0.0;

using AFloat = decltype(X(0,0));

size_t m,n;
m = X.GetNrows();
n = X.GetNcols();
size_t m = X.GetNrows();
size_t n = X.GetNcols();

AFloat maximumError = 0.0;
assert(m == Y.GetNrows());
assert(n == Y.GetNcols());

for (size_t i = 0; i < m; i++) {
for (size_t j = 0; j < n; j++) {
AFloat error = relativeError(X(i,j), Y(i,j));
maximumError = std::max(error, maximumError);
curError = relativeError(X(i,j), Y(i,j));
maxError = std::max(curError, maxError);
}
}
return maximumError;

return maxError;
}

/*! Numerically compute the derivative of the functional f using finite
Expand Down