12 std::shared_ptr<std::function<double(double)>>
Gauss = std::make_shared<std::function<double(double)>> ([](
double value){
const double s = 6.0;
return exp (-
std::pow(value*s,2.0)); });
13 std::shared_ptr<std::function<double(double)>>
GaussComplement = std::make_shared<std::function<double(double)>> ([](
double value){
const double s = 6.0;
return 1.0 -
exp (-
std::pow(value*s,2.0)); });
14 std::shared_ptr<std::function<double(double)>>
InvGauss = std::make_shared<std::function<double(double)>> ([](
double value){
const double s = 6.0;
return -2.0 * value * s*s * (*Gauss.get ()) (value); });
15 std::shared_ptr<std::function<double(double)>>
InvGaussComplement = std::make_shared<std::function<double(double)>> ([](
double value){
const double s = 6.0;
return +2.0 * value * s*s * (*GaussComplement.get ()) (value); });
16 std::shared_ptr<std::function<double(double)>>
InvLinear = std::make_shared<std::function<double(double)>> ([](
double ){
return 1.0; });
17 std::shared_ptr<std::function<double(double)>>
InvReLU = std::make_shared<std::function<double(double)>> ([](
double value){
const double margin = 0.0;
return value > margin ? 1.0 : 0; });
18 std::shared_ptr<std::function<double(double)>>
InvSigmoid = std::make_shared<std::function<double(double)>> ([](
double value){
double s = (*
Sigmoid.get ()) (value);
return s*(1.0-
s); });
19 std::shared_ptr<std::function<double(double)>>
InvSoftPlus = std::make_shared<std::function<double(double)>> ([](
double value){
return 1.0 / (1.0 +
std::exp (-value)); });
20 std::shared_ptr<std::function<double(double)>>
InvSoftSign = std::make_shared<std::function<double(double)>> ([](
double value){
return std::pow ((1.0 -
fabs (value)),2.0); });
21 std::shared_ptr<std::function<double(double)>>
InvSymmReLU = std::make_shared<std::function<double(double)>> ([](
double value){
const double margin = 0.3;
return value > margin ? 1.0 : value < -margin ? 1.0 : 0; });
22 std::shared_ptr<std::function<double(double)>>
InvTanh = std::make_shared<std::function<double(double)>> ([](
double value){
return 1.0 -
std::pow (value, 2.0); });
23 std::shared_ptr<std::function<double(double)>>
InvTanhShift = std::make_shared<std::function<double(double)>> ([](
double value){
return 0.3 + (1.0 -
std::pow (value, 2.0)); });
24 std::shared_ptr<std::function<double(double)>>
Linear = std::make_shared<std::function<double(double)>> ([](
double value){
return value; });
25 std::shared_ptr<std::function<double(double)>>
ReLU = std::make_shared<std::function<double(double)>> ([](
double value){
const double margin = 0.0;
return value > margin ? value-margin : 0; });
26 std::shared_ptr<std::function<double(double)>>
Sigmoid = std::make_shared<std::function<double(double)>> ([](
double value){ value = std::max (-100.0, std::min (100.0,value));
return 1.0/(1.0 +
std::exp (-value)); });
27 std::shared_ptr<std::function<double(double)>>
SoftPlus = std::make_shared<std::function<double(double)>> ([](
double value){
return std::log (1.0+
std::exp (value)); });
28 std::shared_ptr<std::function<double(double)>>
ZeroFnc = std::make_shared<std::function<double(double)>> ([](
double ){
return 0; });
29 std::shared_ptr<std::function<double(double)>>
Tanh = std::make_shared<std::function<double(double)>> ([](
double value){
return tanh (value); });
30 std::shared_ptr<std::function<double(double)>>
SymmReLU = std::make_shared<std::function<double(double)>> ([](
double value){
const double margin = 0.3;
return value > margin ? value-margin : value < -margin ? value+margin : 0; });
31 std::shared_ptr<std::function<double(double)>>
TanhShift = std::make_shared<std::function<double(double)>> ([](
double value){
return tanh (value-0.3); });
32 std::shared_ptr<std::function<double(double)>>
SoftSign = std::make_shared<std::function<double(double)>> ([](
double value){
return value / (1.0 +
fabs (value)); });
37 static std::default_random_engine generator;
38 std::normal_distribution<double> distribution (mean, sigma);
39 return distribution (generator);
45 static std::default_random_engine generator;
46 std::uniform_real_distribution<double> distribution(minValue, maxValue);
47 return distribution(generator);
54 static std::default_random_engine generator;
55 std::uniform_int_distribution<int> distribution(0,maxValue-1);
56 return distribution(generator);
62 static std::default_random_engine generator;
63 std::student_t_distribution<double> distribution (distributionParameter);
64 return distribution (generator);
69 : m_hasDropOut (false)
70 , m_isInputLayer (true)
71 , m_hasWeights (false)
72 , m_hasGradients (false)
90 m_size = std::distance (itInputBegin, itInputEnd);
100 std::shared_ptr<
std::function<
double(
double)>> _activationFunction,
101 std::shared_ptr<
std::function<
double(
double)>> _inverseActivationFunction,
123 std::shared_ptr<
std::function<
double(
double)>> _activationFunction,
145 std::transform (begin (
m_values), end (
m_values), std::back_inserter (probabilitiesContainer), (*Sigmoid.get ()));
151 std::for_each (begin (probabilitiesContainer), end (probabilitiesContainer), [&sum](
double& p){ p =
std::exp (p); sum += p; });
153 std::for_each (begin (probabilitiesContainer), end (probabilitiesContainer), [sum ](
double& p){ p /=
sum; });
159 return probabilitiesContainer;
167 : m_numNodes (_numNodes)
168 , m_eModeOutputValues (eModeOutputValues)
169 , m_activationFunctionType (_activationFunction)
171 for (
size_t iNode = 0; iNode < _numNodes; ++iNode)
175 switch (_activationFunction)
233 size_t _convergenceSteps,
size_t _batchSize,
size_t _testRepetitions,
236 double _momentum,
int _repetitions,
bool _useMultithreading)
237 : m_timer (100, name)
239 , m_maxProgress (100)
240 , m_convergenceSteps (_convergenceSteps)
241 , m_batchSize (_batchSize)
242 , m_testRepetitions (_testRepetitions)
243 , m_factorWeightDecay (_factorWeightDecay)
248 , m_regularization (eRegularization)
249 , fLearningRate (_learningRate)
250 , fMomentum (_momentum)
251 , fRepetitions (_repetitions)
252 , fMinimizerType (_eMinimizerType)
253 , m_convergenceCount (0)
254 , m_maxConvergenceCount (0)
256 , m_useMultithreading (_useMultithreading)
285 create (
"ROC", 100, 0, 1, 100, 0, 1);
286 create (
"Significance", 100, 0, 1, 100, 0, 3);
287 create (
"OutputSig", 100, 0, 1);
288 create (
"OutputBkg", 100, 0, 1);
307 m_output.push_back (output);
308 m_targets.push_back (target);
309 m_weights.push_back (weight);
328 if (m_output.empty ())
330 double minVal = *std::min_element (begin (m_output), end (m_output));
331 double maxVal = *std::max_element (begin (m_output), end (m_output));
332 const size_t numBinsROC = 1000;
333 const size_t numBinsData = 100;
335 std::vector<double> truePositives (numBinsROC+1, 0);
336 std::vector<double> falsePositives (numBinsROC+1, 0);
337 std::vector<double> trueNegatives (numBinsROC+1, 0);
338 std::vector<double> falseNegatives (numBinsROC+1, 0);
340 std::vector<double>
x (numBinsData, 0);
341 std::vector<double> datSig (numBinsData+1, 0);
342 std::vector<double> datBkg (numBinsData+1, 0);
344 double binSizeROC = (maxVal - minVal)/(
double)numBinsROC;
345 double binSizeData = (maxVal - minVal)/(
double)numBinsData;
347 double sumWeightsSig = 0.0;
348 double sumWeightsBkg = 0.0;
350 for (
size_t b = 0;
b < numBinsData; ++
b)
352 double binData = minVal +
b*binSizeData;
356 if (
fabs(binSizeROC) < 0.0001)
359 for (
size_t i = 0, iEnd = m_output.size (); i < iEnd; ++i)
361 double val = m_output.at (i);
362 double truth = m_targets.at (i);
363 double weight = m_weights.at (i);
365 bool isSignal = (truth > 0.5 ? true :
false);
367 if (m_sumOfSigWeights != 0 && m_sumOfBkgWeights != 0)
370 weight *= m_sumOfSigWeights;
372 weight *= m_sumOfBkgWeights;
375 size_t binROC = (val-minVal)/binSizeROC;
376 size_t binData = (val-minVal)/binSizeData;
380 for (
size_t n = 0;
n <= binROC; ++
n)
382 truePositives.at (
n) += weight;
384 for (
size_t n = binROC+1;
n < numBinsROC; ++
n)
386 falseNegatives.at (
n) += weight;
389 datSig.at (binData) += weight;
390 sumWeightsSig += weight;
394 for (
size_t n = 0;
n <= binROC; ++
n)
396 falsePositives.at (
n) += weight;
398 for (
size_t n = binROC+1;
n < numBinsROC; ++
n)
400 trueNegatives.at (
n) += weight;
403 datBkg.at (binData) += weight;
404 sumWeightsBkg += weight;
408 std::vector<double> sigEff;
409 std::vector<double> backRej;
411 double bestSignificance = 0;
412 double bestCutSignificance = 0;
414 double numEventsScaleFactor = 1.0;
415 if (m_scaleToNumEvents > 0)
417 size_t numEvents = m_output.size ();
418 numEventsScaleFactor = double (m_scaleToNumEvents)/double (numEvents);
422 clear (
"Significance");
424 for (
size_t i = 0; i < numBinsROC; ++i)
426 double tp = truePositives.at (i) * numEventsScaleFactor;
427 double fp = falsePositives.at (i) * numEventsScaleFactor;
428 double tn = trueNegatives.at (i) * numEventsScaleFactor;
429 double fn = falseNegatives.at (i) * numEventsScaleFactor;
431 double seff = (tp+fn == 0.0 ? 1.0 : (tp / (tp+fn)));
432 double brej = (tn+fp == 0.0 ? 0.0 : (tn / (tn+fp)));
434 sigEff.push_back (seff);
435 backRej.push_back (brej);
441 double currentCut = (i * binSizeROC)+minVal;
445 double significance = sig /
sqrt (sig + bkg);
446 if (significance > bestSignificance)
448 bestSignificance = significance;
449 bestCutSignificance = currentCut;
452 addPoint (
"Significance", currentCut, significance);
456 m_significances.push_back (bestSignificance);
457 static size_t testCycle = 0;
461 for (
size_t i = 0; i < numBinsData; ++i)
463 addPoint (
"OutputSig", x.at (i), datSig.at (i)/sumWeightsSig);
464 addPoint (
"OutputBkg", x.at (i), datBkg.at (i)/sumWeightsBkg);
481 m_cutValue = bestCutSignificance;
517 m_sumOfSigWeights = sumOfSigWeights; m_sumOfBkgWeights = sumOfBkgWeights;
524 std::string _fileNameNetConfig,
525 std::string _fileNameResult,
526 std::vector<Pattern>* _resultPatternContainer)
528 m_pResultPatternContainer = _resultPatternContainer;
529 m_fileNameResult = _fileNameResult;
530 m_fileNameNetConfig = _fileNameNetConfig;
547 size_t prevNodes (inputSize ());
548 for (
auto& layer : m_layers)
550 if (index >= trainingStartLayer)
551 num += layer.numWeights (prevNodes);
552 prevNodes = layer.numNodes ();
563 for (
auto& layer : m_layers)
565 if (index >= trainingStartLayer)
566 num += layer.numNodes ();
577 size_t numDrops = dropFraction * _numNodes;
578 if (numDrops >= _numNodes)
579 numDrops = _numNodes - 1;
581 dropContainer.insert (end (dropContainer), _numNodes-numDrops,
true);
583 dropContainer.insert (end (dropContainer), numDrops,
false);
585 std::shuffle(end(dropContainer)-_numNodes, end(dropContainer), std::default_random_engine{});
std::shared_ptr< std::function< double(double)> > InvSoftSign
void addPoint(std::string histoName, double x)
for monitoring
void setWeightSums(double sumOfSigWeights, double sumOfBkgWeights)
set the weight sums to be scaled to (preparations for monitoring output)
std::shared_ptr< std::function< double(double)> > InvSymmReLU
std::shared_ptr< std::function< double(double)> > InvGaussComplement
static long int sum(long int i)
std::shared_ptr< std::function< double(double)> > SymmReLU
MinimizerType
< list all the minimizer types
std::shared_ptr< std::function< double(double)> > SoftPlus
std::vector< char > DropContainer
std::shared_ptr< std::function< double(double)> > m_inverseActivationFunction
stores the inverse activation function
bool isFlagSet(T flag, T value)
std::shared_ptr< std::function< double(double)> > InvReLU
size_t convergenceSteps() const
how many steps until training is deemed to have converged
std::shared_ptr< std::function< double(double)> > TanhShift
std::shared_ptr< Monitoring > fMonitoring
void plot(std::string histoName, std::string options, int pad, EColor color)
for monitoring
iterator_type m_itGradientBegin
iterator to the first gradient of this layer in the gradient vector
bool m_hasGradients
does this layer have gradients (only if in training mode)
ModeOutputValues m_eModeOutput
stores the output mode (DIRECT, SIGMOID, SOFTMAX)
std::shared_ptr< std::function< double(double)> > ReLU
bool m_isInputLayer
is this layer an input layer
Settings(TString name, size_t _convergenceSteps=15, size_t _batchSize=10, size_t _testRepetitions=7, double _factorWeightDecay=1e-5, TMVA::DNN::EnumRegularization _regularization=TMVA::DNN::EnumRegularization::NONE, MinimizerType _eMinimizerType=MinimizerType::fSteepest, double _learningRate=1e-5, double _momentum=0.3, int _repetitions=3, bool _multithreading=true)
c'tor
std::shared_ptr< std::function< double(double)> > SoftSign
std::vector< double > m_valueGradients
stores the gradients of the values (nodes)
std::shared_ptr< std::function< double(double)> > Linear
container_type::const_iterator const_iterator_type
const_iterator_type m_itConstWeightBegin
const iterator to the first weight of this layer in the weight vector
double pow(double, double)
const_iterator_type m_itInputBegin
iterator to the first of the nodes in the input node vector
bool m_hasDropOut
dropOut is turned on?
std::shared_ptr< std::function< double(double)> > InvTanh
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
void startTrainCycle()
action to be done when the training cycle is started (e.g.
void create(std::string histoName, int bins, double min, double max)
for monitoring
double studenttDouble(double distributionParameter)
LayerData(const_iterator_type itInputBegin, const_iterator_type itInputEnd, ModeOutputValues eModeOutput=ModeOutputValues::DIRECT)
c'tor of LayerData
virtual ~Settings()
d'tor
std::vector< double > m_deltas
stores the deltas for the DNN training
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Layer(size_t numNodes, EnumFunction activationFunction, ModeOutputValues eModeOutputValues=ModeOutputValues::DIRECT)
c'tor for defining a Layer
container_type::iterator iterator_type
container_type computeProbabilities() const
compute the probabilities from the node values
size_t m_convergenceCount
std::vector< double > container_type
std::shared_ptr< std::function< double(double)> > ZeroFnc
std::shared_ptr< std::function< double(double)> > InvLinear
void endTrainCycle(double)
action to be done when the training cycle is ended (e.g.
void fillDropContainer(DropContainer &dropContainer, double dropFraction, size_t numNodes) const
prepare the drop-out-container (select the nodes which are to be dropped out)
virtual void endTestCycle()
action to be done when the training cycle is ended (e.g.
size_t numWeights(size_t trainingStartLayer=0) const
returns the number of weights in this net
bool m_hasWeights
does this layer have weights (it does not if it is the input layer)
std::shared_ptr< std::function< double(double)> > Tanh
double gaussDouble(double mean, double sigma)
std::shared_ptr< std::function< double(double)> > InvSigmoid
static constexpr double s
size_t numNodes(size_t trainingStartLayer=0) const
returns the number of nodes in this net
double uniformDouble(double minValue, double maxValue)
std::vector< double > m_values
stores the values of the nodes in this layer
void setResultComputation(std::string _fileNameNetConfig, std::string _fileNameResult, std::vector< Pattern > *_resultPatternContainer)
preparation for monitoring output
size_t m_maxConvergenceCount
std::shared_ptr< std::function< double(double)> > InvSoftPlus
std::shared_ptr< std::function< double(double)> > GaussComplement
Abstract ClassifierFactory template that handles arbitrary types.
const_iterator_type m_itInputEnd
iterator to the end of the nodes in the input node vector
std::shared_ptr< std::function< double(double)> > m_activationFunction
activation function for this layer
std::shared_ptr< std::function< double(double)> > m_inverseActivationFunction
inverse activation function for this layer
std::shared_ptr< std::function< double(double)> > InvGauss
void testSample(double error, double output, double target, double weight)
action to be done after the computation of a test sample (e.g.
virtual void startTestCycle()
action to be done when the test cycle is started (e.g.
std::shared_ptr< std::function< double(double)> > InvTanhShift
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
std::shared_ptr< std::function< double(double)> > Sigmoid
void clear(std::string histoName)
for monitoring
std::shared_ptr< std::function< double(double)> > Gauss
virtual bool hasConverged(double testError)
has this training converged already?
int randomInt(int maxValue)
std::shared_ptr< std::function< double(double)> > m_activationFunction
stores the activation function