1#ifndef TMVA_NEURAL_NET_I
2#define TMVA_NEURAL_NET_I
5#error "Do not use NeuralNet.icc directly. #include \"NeuralNet.h\" instead."
9#pragma GCC diagnostic ignored "-Wunused-variable"
41 template <
typename Container,
typename T>
52 extern std::shared_ptr<std::function<
double(
double)>>
ZeroFnc;
55 extern std::shared_ptr<std::function<
double(
double)>>
Sigmoid;
58 extern std::shared_ptr<std::function<
double(
double)>>
Tanh;
59 extern std::shared_ptr<std::function<
double(
double)>>
InvTanh;
61 extern std::shared_ptr<std::function<
double(
double)>>
Linear;
67 extern std::shared_ptr<std::function<
double(
double)>>
ReLU;
68 extern std::shared_ptr<std::function<
double(
double)>>
InvReLU;
79 extern std::shared_ptr<std::function<
double(
double)>>
Gauss;
90template <
bool HasDropOut,
typename ItSource,
typename ItWeight,
typename ItTarget,
typename ItDrop>
101 (*itTarget) += (*itSource) * (*itWeight);
117template <
bool HasDropOut,
typename ItSource,
typename ItWeight,
typename ItPrev,
typename ItDrop>
128 (*itPrev) += (*itCurr) * (*itWeight);
146 template <
typename ItValue,
typename Fnc>
151 auto&
value = (*itValue);
163 template <
typename ItValue,
typename Fnc,
typename InvFnc,
typename ItGradient>
168 auto&
value = (*itValue);
182 template <
typename ItSource,
typename ItDelta,
typename ItTargetGradient,
typename ItGradient>
194 (*itGradient) -= (*itTargetDelta) * (*itSource) * (*itTargetGradient);
208 template <EnumRegularization Regularization>
221 return weight == 0.0 ? 0.0 : std::copysign (factorWeightDecay, weight);
228 return factorWeightDecay * weight;
236 template <EnumRegularization Regularization,
typename ItSource,
typename ItDelta,
typename ItTargetGradient,
typename ItGradient,
typename ItWeight>
262#define USELOCALWEIGHTS 1
270 template <
typename Function,
typename Weights,
typename PassThrough>
273 size_t numWeights = weights.size ();
304 (*itLocWeight) += (*itPrevG);
336 std::cout <<
"\nlearning rate reduced to " <<
m_alpha << std::endl;
345 auto itW = std::begin (weights);
381 template <
typename ItOutput,
typename ItTruth,
typename ItDelta,
typename InvFnc>
392 double output = (*itOutput);
393 double error =
output - (*itTruth);
411 template <
typename ItProbability,
typename ItTruth,
typename ItDelta,
typename ItInvActFnc>
457 template <
typename ItOutput,
typename ItTruth,
typename ItDelta,
typename ItInvActFnc>
469 double truth = (*itTruth);
497 template <
typename ItWeight>
507 double weight = (*itWeight);
508 w += std::fabs (weight);
510 return error + 0.5 *
w * factorWeightDecay /
n;
519 double weight = (*itWeight);
522 return error + 0.5 *
w * factorWeightDecay /
n;
545 template <
typename LAYERDATA>
571template <
typename LAYERDATA>
599 template <
typename LAYERDATA>
603 if (factorWeightDecay != 0.0)
651 template <
typename WeightsType,
typename DropProbabilities>
656 if (
drops.empty () || weights.empty ())
659 auto itWeight = std::begin (weights);
711 template <
typename Minimizer>
715 Minimizer& minimizer,
725 settings.create (
"trainErrors", 100, 0, 100, 100, 0,1);
726 settings.create (
"testErrors", 100, 0, 100, 100, 0,1);
736 const std::vector<double>& dropFractions =
settings.dropFractions ();
775 bool hasConverged =
false;
790 size_t numThreads = std::thread::hardware_concurrency ();
803 std::vector<std::future<std::tuple<double,std::vector<double>>>>
futures;
808 std::async (std::launch::async, [&]()
821 std::tuple<double,std::vector<double>>
result =
f.get ();
832 (*pattern_it).weight ());
843 std::vector<double>
output;
861 (*pattern_it).weight ());
873 settings.computeResult (*
this, weights);
910 (
int)
settings.maxConvergenceCount ());
938 template <
typename Iterator,
typename Minimizer>
970 size_t numThreads = std::thread::hardware_concurrency ();
973 std::vector<std::pair<batch_iterator,batch_iterator>>
batchVec;
988 std::vector<std::future<double>>
futures;
993 std::async (std::launch::async, [&]()
1036 template <
typename Weights>
1051 layer.activationFunction (),
1052 layer.modeOutputValues ()));
1063 std::vector<double>
output;
1069 template <
typename Weights,
typename PassThrough>
1078 template <
typename Weights,
typename PassThrough,
typename OutContainer>
1088 template <
typename Weights,
typename Gradients,
typename PassThrough>
1093 assert (weights.size () == gradients.size ());
1098 template <
typename Weights,
typename Gradients,
typename PassThrough,
typename OutContainer>
1103 assert (weights.size () == gradients.size ());
1110 template <
typename LayerContainer,
typename DropContainer,
typename ItWeight,
typename ItGradient>
1126 throw std::string (
"no layers in this net");
1185 layer.activationFunction (),
1186 layer.modeOutputValues ()));
1191 layer.activationFunction (),
1192 layer.inverseActivationFunction (),
1193 layer.modeOutputValues ()));
1220 template <
typename LayerContainer>
1222 std::vector<LayerData>&
layerData)
const
1239 template <
typename LayerContainer,
typename LayerPatternContainer>
1290 template <
typename OutputContainer>
1311 template <
typename OutputContainer>
1320 template <
typename ItWeight>
1354 template <
typename Settings>
1404 template <
typename LayerContainer,
typename PassThrough,
typename ItWeight,
typename ItGradient,
typename OutContainer>
1454 double batchSize = std::distance (std::begin (
batch), std::end (
batch));
1469 template <
typename OutIterator>
1485 double stdDev = sqrt (2.0/
nIn);
1509 double minVal = -sqrt(2.0/
nIn);
1510 double maxVal = sqrt (2.0/
nIn);
1578 template <
typename Container,
typename ItWeight>
1584 double factorWeightDecay,
1601 std::vector<double> probabilities =
layerData.probabilities ();
1602 error =
crossEntropy (begin (probabilities), end (probabilities),
1611 std::cout <<
"softmax." << std::endl;
1613 std::vector<double> probabilities =
layerData.probabilities ();
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Double_t(* Function)(Double_t)
std::vector< double >::const_iterator const_iterator
const_iterator begin() const
const_iterator end() const
The Batch class encapsulates one mini-batch.
LayerData holds the data of one layer.
DropContainer::const_iterator const_dropout_iterator
void forwardBatch(const LayerContainer &_layers, LayerPatternContainer &layerPatternData, std::vector< double > &valuesMean, std::vector< double > &valuesStdDev, size_t trainFromLayer) const
std::vector< Layer > m_layers
layer-structure-data
std::vector< double > compute(const std::vector< double > &input, const Weights &weights) const
compute the net with the given input and the given weights
void fetchOutput(const LayerData &lastLayerData, OutputContainer &outputContainer) const
size_t inputSize() const
input size of the DNN
ModeErrorFunction m_eErrorFunction
denotes the error function
double train(std::vector< double > &weights, std::vector< Pattern > &trainPattern, const std::vector< Pattern > &testPattern, Minimizer &minimizer, Settings &settings)
start the training
const std::vector< Layer > & layers() const
returns the layers (structure)
std::vector< std::vector< LayerData > > prepareLayerData(LayerContainer &layers, Batch &batch, const DropContainer &dropContainer, ItWeight itWeightBegin, ItWeight itWeightEnd, ItGradient itGradientBegin, ItGradient itGradientEnd, size_t &totalNumWeights) const
void initializeWeights(WeightInitializationStrategy eInitStrategy, OutIterator itWeight)
initialize the weights with the given strategy
size_t outputSize() const
output size of the DNN
double errorFunction(LayerData &layerData, Container truth, ItWeight itWeight, ItWeight itWeightEnd, double patternWeight, double factorWeightDecay, EnumRegularization eRegularization) const
computes the error of the DNN
double forward_backward(LayerContainer &layers, PassThrough &settingsAndBatch, ItWeight itWeightBegin, ItWeight itWeightEnd, ItGradient itGradientBegin, ItGradient itGradientEnd, size_t trainFromLayer, OutContainer &outputContainer, bool fetchOutput) const
main NN computation function
double trainCycle(Minimizer &minimizer, std::vector< double > &weights, Iterator itPatternBegin, Iterator itPatternEnd, Settings &settings, DropContainer &dropContainer)
executes one training cycle
double operator()(PassThrough &settingsAndBatch, const Weights &weights) const
execute computation of the DNN for one mini-batch (used by the minimizer); no computation of gradient...
void dropOutWeightFactor(WeightsType &weights, const DropProbabilities &drops, bool inverse=false)
set the drop out configuration
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
IPythonInteractive * fInteractive
std::tuple< double, double > computeError(const Settings &settings, std::vector< LayerData > &lastLayerData, Batch &batch, ItWeight itWeightBegin, ItWeight itWeightEnd) const
void forwardPattern(const LayerContainer &_layers, std::vector< LayerData > &layerData) const
void backPropagate(std::vector< std::vector< LayerData > > &layerPatternData, const Settings &settings, size_t trainFromLayer, size_t totalNumWeights) const
Settings for the training of the neural net.
double m_beta
internal parameter (momentum)
std::vector< double > m_localGradients
local gradients for reuse in thread.
std::vector< double > m_prevGradients
vector remembers the gradients of the previous step
double m_alpha
internal parameter (learningRate)
std::vector< double > m_localWeights
local weights for reuse in thread.
double operator()(Function &fitnessFunction, Weights &weights, PassThrough &passThrough)
operator to call the steepest gradient descent algorithm
void AddPoint(Double_t x, Double_t y1, Double_t y2)
This function is used only in 2 TGraph case, and it will add new data points to graphs.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
std::shared_ptr< std::function< double(double)> > InvGauss
double sumOfSquares(ItOutput itOutputBegin, ItOutput itOutputEnd, ItTruth itTruthBegin, ItTruth itTruthEnd, ItDelta itDelta, ItDelta itDeltaEnd, ItInvActFnc itInvActFnc, double patternWeight)
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
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
void applyFunctions(ItValue itValue, ItValue itValueEnd, ItFunction itFunction)
T uniformFromTo(T from, T to)
double computeRegularization< EnumRegularization::L1 >(double weight, const double &factorWeightDecay)
std::shared_ptr< std::function< double(double)> > SoftPlus
double crossEntropy(ItProbability itProbabilityBegin, ItProbability itProbabilityEnd, ItTruth itTruthBegin, ItTruth itTruthEnd, ItDelta itDelta, ItDelta itDeltaEnd, ItInvActFnc itInvActFnc, double patternWeight)
cross entropy error function
void backward(LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
backward application of the weights (back-propagation of the error)
std::shared_ptr< std::function< double(double)> > ZeroFnc
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
std::shared_ptr< std::function< double(double)> > InvSoftSign
std::shared_ptr< std::function< double(double)> > InvGaussComplement
double computeRegularization< EnumRegularization::L2 >(double weight, const double &factorWeightDecay)
auto regularization(const typename Architecture_t::Matrix_t &A, ERegularization R) -> decltype(Architecture_t::L1Regularization(A))
Evaluate the regularization functional for a given weight matrix.
@ CROSSENTROPY_MUTUALEXCLUSIVE
double softMaxCrossEntropy(ItOutput itProbabilityBegin, ItOutput itProbabilityEnd, ItTruth itTruthBegin, ItTruth itTruthEnd, ItDelta itDelta, ItDelta itDeltaEnd, ItInvActFnc itInvActFnc, double patternWeight)
soft-max-cross-entropy error function (for mutual exclusive cross-entropy)
std::shared_ptr< std::function< double(double)> > InvTanh
std::shared_ptr< std::function< double(double)> > Linear
WeightInitializationStrategy
weight initialization strategies to be chosen from
std::shared_ptr< std::function< double(double)> > InvReLU
std::shared_ptr< std::function< double(double)> > GaussComplement
std::shared_ptr< std::function< double(double)> > Gauss
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
double computeRegularization(double weight, const double &factorWeightDecay)
compute the regularization (L1, L2)
void applyWeights(ItSource itSourceBegin, ItSource itSourceEnd, ItWeight itWeight, ItTarget itTargetBegin, ItTarget itTargetEnd)
std::tuple< Settings &, Batch &, DropContainer & > pass_through_type
bool isFlagSet(T flag, T value)
std::shared_ptr< std::function< double(double)> > InvTanhShift
void update(ItSource itSource, ItSource itSourceEnd, ItDelta itTargetDeltaBegin, ItDelta itTargetDeltaEnd, ItTargetGradient itTargetGradientBegin, ItGradient itGradient)
update the gradients
std::vector< char > DropContainer
void applyWeightsBackwards(ItSource itCurrBegin, ItSource itCurrEnd, ItWeight itWeight, ItPrev itPrevBegin, ItPrev itPrevEnd)
std::shared_ptr< std::function< double(double)> > InvSymmReLU
std::shared_ptr< std::function< double(double)> > InvLinear
create variable transformations