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); });
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)
82 : m_hasDropOut (false)
83 , m_isInputLayer (true)
84 , m_hasWeights (false)
85 , m_hasGradients (false)
86 , m_eModeOutput (eModeOutput)
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,
104 , m_hasDropOut (false)
105 , m_itConstWeightBegin (itWeightBegin)
106 , m_itGradientBegin (itGradientBegin)
107 , m_activationFunction (_activationFunction)
108 , m_inverseActivationFunction (_inverseActivationFunction)
109 , m_isInputLayer (false)
110 , m_hasWeights (true)
111 , m_hasGradients (true)
112 , m_eModeOutput (eModeOutput)
123 std::shared_ptr<std::function<
double(
double)>> _activationFunction,
126 , m_hasDropOut (false)
127 , m_itConstWeightBegin (itWeightBegin)
128 , m_activationFunction (_activationFunction)
129 , m_inverseActivationFunction ()
130 , m_isInputLayer (false)
131 , m_hasWeights (true)
132 , m_hasGradients (false)
133 , m_eModeOutput (eModeOutput)
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);
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)
365 bool isSignal = (truth > 0.5 ? true :
false);
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;
417 size_t numEvents =
m_output.size ();
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);
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);
524 std::string _fileNameNetConfig,
525 std::string _fileNameResult,
526 std::vector<Pattern>* _resultPatternContainer)
550 if (index >= trainingStartLayer)
551 num += layer.numWeights (prevNodes);
552 prevNodes = layer.numNodes ();
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{});
void startTrainCycle()
action to be done when the training cycle is started (e.g.
void endTrainCycle(double)
action to be done when the training cycle is ended (e.g.
virtual void endTestCycle()
action to be done when the training cycle is ended (e.g.
std::vector< Pattern > * m_pResultPatternContainer
void setResultComputation(std::string _fileNameNetConfig, std::string _fileNameResult, std::vector< Pattern > *_resultPatternContainer)
preparation for monitoring output
std::string m_fileNameResult
std::vector< double > m_significances
std::vector< double > m_weights
std::string m_fileNameNetConfig
std::vector< double > m_targets
void testSample(double error, double output, double target, double weight)
action to be done after the computation of a test sample (e.g.
size_t m_scaleToNumEvents
virtual void startTestCycle()
action to be done when the test cycle is started (e.g.
void setWeightSums(double sumOfSigWeights, double sumOfBkgWeights)
set the weight sums to be scaled to (preparations for monitoring output)
std::vector< double > m_output
const_iterator_type m_itInputBegin
iterator to the first of the nodes in the input node vector
std::vector< double > m_deltas
stores the deltas for the DNN training
container_type::iterator iterator_type
LayerData(const_iterator_type itInputBegin, const_iterator_type itInputEnd, ModeOutputValues eModeOutput=ModeOutputValues::DIRECT)
c'tor of LayerData
std::vector< double > container_type
std::vector< double > m_values
stores the values of the nodes in this layer
const_iterator_type m_itInputEnd
iterator to the end of the nodes in the input node vector
container_type::const_iterator const_iterator_type
std::vector< double > m_valueGradients
stores the gradients of the values (nodes)
container_type computeProbabilities() const
compute the probabilities from the node values
ModeOutputValues m_eModeOutput
stores the output mode (DIRECT, SIGMOID, SOFTMAX)
std::shared_ptr< std::function< double(double)> > m_activationFunction
stores the activation function
std::shared_ptr< std::function< double(double)> > m_inverseActivationFunction
stores the inverse activation function
Layer(size_t numNodes, EnumFunction activationFunction, ModeOutputValues eModeOutputValues=ModeOutputValues::DIRECT)
c'tor for defining a Layer
std::vector< Layer > m_layers
layer-structure-data
size_t inputSize() const
input size of the DNN
size_t numNodes(size_t trainingStartLayer=0) const
returns the number of nodes in this net
void fillDropContainer(DropContainer &dropContainer, double dropFraction, size_t numNodes) const
prepare the drop-out-container (select the nodes which are to be dropped out)
size_t numWeights(size_t trainingStartLayer=0) const
returns the number of weights in this net
void clear(std::string histoName)
for monitoring
virtual bool hasConverged(double testError)
has this training converged already?
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
void addPoint(std::string histoName, double x)
for monitoring
size_t m_convergenceCount
void plot(std::string histoName, std::string options, int pad, EColor color)
for monitoring
size_t convergenceSteps() const
how many steps until training is deemed to have converged
std::shared_ptr< Monitoring > fMonitoring
void create(std::string histoName, int bins, double min, double max)
for monitoring
size_t m_maxConvergenceCount
std::shared_ptr< std::function< double(double)> > InvGauss
double uniformDouble(double minValue, double maxValue)
std::shared_ptr< std::function< double(double)> > SymmReLU
std::shared_ptr< std::function< double(double)> > TanhShift
std::shared_ptr< std::function< double(double)> > Tanh
std::shared_ptr< std::function< double(double)> > InvSigmoid
std::shared_ptr< std::function< double(double)> > SoftPlus
std::shared_ptr< std::function< double(double)> > ZeroFnc
std::shared_ptr< std::function< double(double)> > InvSoftSign
double studenttDouble(double distributionParameter)
std::shared_ptr< std::function< double(double)> > InvGaussComplement
std::shared_ptr< std::function< double(double)> > InvTanh
std::shared_ptr< std::function< double(double)> > Linear
std::shared_ptr< std::function< double(double)> > InvReLU
std::shared_ptr< std::function< double(double)> > GaussComplement
std::shared_ptr< std::function< double(double)> > Gauss
MinimizerType
< list all the minimizer types
std::shared_ptr< std::function< double(double)> > Sigmoid
double gaussDouble(double mean, double sigma)
std::shared_ptr< std::function< double(double)> > SoftSign
std::shared_ptr< std::function< double(double)> > InvSoftPlus
std::shared_ptr< std::function< double(double)> > ReLU
bool isFlagSet(T flag, T value)
int randomInt(int maxValue)
std::shared_ptr< std::function< double(double)> > InvTanhShift
std::vector< char > DropContainer
std::shared_ptr< std::function< double(double)> > InvSymmReLU
std::shared_ptr< std::function< double(double)> > InvLinear
create variable transformations
static uint64_t sum(uint64_t i)
static void output(int code)