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);
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;
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)
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);
460 for (
size_t i = 0; i < numBinsData; ++i)
462 addPoint (
"OutputSig",
x.at (i), datSig.at (i)/sumWeightsSig);
463 addPoint (
"OutputBkg",
x.at (i), datBkg.at (i)/sumWeightsBkg);
521 std::string _fileNameNetConfig,
522 std::string _fileNameResult,
523 std::vector<Pattern>* _resultPatternContainer)
547 if (index >= trainingStartLayer)
548 num += layer.numWeights (prevNodes);
549 prevNodes = layer.numNodes ();
562 if (index >= trainingStartLayer)
563 num += layer.numNodes ();
574 size_t numDrops = dropFraction * _numNodes;
575 if (numDrops >= _numNodes)
576 numDrops = _numNodes - 1;
578 dropContainer.insert (end (dropContainer), _numNodes-numDrops,
true);
580 dropContainer.insert (end (dropContainer), numDrops,
false);
582 std::shuffle(end(dropContainer)-_numNodes, end(dropContainer), std::default_random_engine{});
void startTestCycle() override
action to be done when the test cycle is started (e.g.
void endTrainCycle(double) override
action to be done when the training cycle is ended (e.g.
std::vector< Pattern > * m_pResultPatternContainer
void endTestCycle() override
action to be done when the training cycle is ended (e.g.
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 startTrainCycle() override
action to be done when the training cycle is started (e.g.
size_t m_scaleToNumEvents
void testSample(double error, double output, double target, double weight) override
action to be done after the computation of a test sample (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
bool m_hasGradients
does this layer have gradients (only if in training mode)
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
bool m_hasWeights
does this layer have weights (it does not if it is the input layer)
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
iterator_type m_itGradientBegin
iterator to the first gradient of this layer in the gradient vector
std::vector< double > m_valueGradients
stores the gradients of the values (nodes)
const_iterator_type m_itConstWeightBegin
const iterator to the first weight of this layer in the weight vector
container_type computeProbabilities() const
compute the probabilities from the node values
bool m_hasDropOut
dropOut is turned on?
bool m_isInputLayer
is this layer an input layer
ModeOutputValues m_eModeOutput
stores the output mode (DIRECT, SIGMOID, SOFTMAX)
std::shared_ptr< std::function< double(double)> > m_inverseActivationFunction
inverse activation function for this layer
std::shared_ptr< std::function< double(double)> > m_activationFunction
activation function for this layer
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
ModeOutputValues m_eModeOutputValues
do the output values of this layer have to be transformed somehow (e.g. to probabilities) or returned...
EnumFunction m_activationFunctionType
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
size_t m_batchSize
mini-batch size
Timer m_timer
timer for monitoring
void clear(std::string histoName)
for monitoring
MinimizerType fMinimizerType
size_t m_convergenceSteps
number of steps without improvement to consider the DNN to have converged
virtual bool hasConverged(double testError)
has this training converged already?
double m_minProgress
current limits for the progress bar
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
double m_maxProgress
current limits for the progress bar
void addPoint(std::string histoName, double x)
for monitoring
size_t m_convergenceCount
EnumRegularization m_regularization
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
double m_factorWeightDecay
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)