Skip to content

Commit

Permalink
test: Add test for Method DL, for the DNN case
Browse files Browse the repository at this point in the history
  • Loading branch information
IlievskiV authored and lmoneta committed Apr 8, 2018
1 parent a94c617 commit f99b00c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,5 @@ tags

# TMVA datasets
/tmva/tmva/test/DNN/CNN/dataset/SingleElectronPt50_FEVTDEBUG_n250k_IMG_CROPS32.root
/tmva/tmva/test/DNN/CNN/dataset/SinglePhotonPt50_FEVTDEBUG_n250k_IMG_CROPS32.root
/tmva/tmva/test/DNN/CNN/dataset/SinglePhotonPt50_FEVTDEBUG_n250k_IMG_CROPS32.root
/tmva/tmva/test/DNN/CNN/dataset/tmva_class_example.root
1 change: 1 addition & 0 deletions tmva/tmva/test/DNN/CNN/TestMethodDL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ int main()

TString archCPU = "CPU";
testMethodDL(archCPU);
testMethodDL_DNN(archCPU);
}
109 changes: 109 additions & 0 deletions tmva/tmva/test/DNN/CNN/TestMethodDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,113 @@ void testMethodDL(TString architectureStr)
delete dataloader;
}

/** Testing the entire pipeline of the Method DL, when only a Multilayer Percepton
* is constructed. */
//______________________________________________________________________________
void testMethodDL_DNN(TString architectureStr)
{
TFile *input(0);
TString fname = "/Users/vladimirilievski/Desktop/Vladimir/GSoC/ROOT-CI/common-version/root/tmva/tmva/test/DNN/CNN/"
"dataset/tmva_class_example.root";
input = TFile::Open(fname);

// Register the training and test trees
TTree *signalTree = (TTree *)input->Get("TreeS");
TTree *background = (TTree *)input->Get("TreeB");

TString outfileName("results/TMVA_DNN.root");
TFile *outputFile = TFile::Open(outfileName, "RECREATE");

// create factory
TMVA::Factory *factory =
new TMVA::Factory("TMVAClassification", outputFile,
"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");

// create dataset and add variables
TMVA::DataLoader *dataloader = new TMVA::DataLoader("dataset");

dataloader->AddVariable("myvar1 := var1+var2", 'F');
dataloader->AddVariable("myvar2 := var1-var2", "Expression 2", "", 'F');
dataloader->AddVariable("var3", "Variable 3", "units", 'F');
dataloader->AddVariable("var4", "Variable 4", "units", 'F');

dataloader->AddSpectator("spec1 := var1*2", "Spectator 1", "units", 'F');
dataloader->AddSpectator("spec2 := var1*3", "Spectator 2", "units", 'F');

// Add Signal and Background Trees
Double_t signalWeight = 1.0;
Double_t backgroundWeight = 1.0;

dataloader->AddSignalTree(signalTree, signalWeight);
dataloader->AddBackgroundTree(background, backgroundWeight);

// Prepare training and testing set
dataloader->SetBackgroundWeightExpression("weight");
TCut mycuts = "";
TCut mycutb = "";
dataloader->PrepareTrainingAndTestTree(
mycuts, mycutb, "nTrain_Signal=1000:nTrain_Background=1000:SplitMode=Random:NormMode=NumEvents:!V");

// Input Layout
TString inputLayoutString("InputLayout=1|1|4");

// Batch Layout
TString batchLayoutString("BatchLayout=1|256|4");

// General layout.
TString layoutString("Layout=DENSE|128|TANH,DENSE|128|TANH,DENSE|128|TANH,DENSE|1|LINEAR");

// Training strategies.
TString training0("LearningRate=1e-1,Momentum=0.9,Repetitions=1,"
"ConvergenceSteps=20,BatchSize=256,TestRepetitions=10,"
"WeightDecay=1e-4,Regularization=L2,"
"DropConfig=0.0+0.5+0.5+0.5, Multithreading=True");
TString training1("LearningRate=1e-2,Momentum=0.9,Repetitions=1,"
"ConvergenceSteps=20,BatchSize=256,TestRepetitions=10,"
"WeightDecay=1e-4,Regularization=L2,"
"DropConfig=0.0+0.0+0.0+0.0, Multithreading=True");
TString training2("LearningRate=1e-3,Momentum=0.0,Repetitions=1,"
"ConvergenceSteps=20,BatchSize=256,TestRepetitions=10,"
"WeightDecay=1e-4,Regularization=L2,"
"DropConfig=0.0+0.0+0.0+0.0, Multithreading=True");
TString trainingStrategyString("TrainingStrategy=");
trainingStrategyString += training0 + "|" + training1 + "|" + training2;

// General Options.
TString dnnOptions("!H:V:ErrorStrategy=CROSSENTROPY:"
"WeightInitialization=XAVIERUNIFORM");

// Concatenate all option strings
dnnOptions.Append(":");
dnnOptions.Append(inputLayoutString);

// Concatenate all option strings
dnnOptions.Append(":");
dnnOptions.Append(batchLayoutString);

dnnOptions.Append(":");
dnnOptions.Append(layoutString);

dnnOptions.Append(":");
dnnOptions.Append(trainingStrategyString);

dnnOptions.Append(":Architecture=");
dnnOptions.Append(architectureStr);

TString methodTitle = "DL_CPU";
factory->BookMethod(dataloader, TMVA::Types::kDL, methodTitle, dnnOptions);

// Train MVAs using the set of training events
factory->TrainAllMethods();

// Save the output
outputFile->Close();

std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
std::cout << "==> TMVAClassification is done!" << std::endl;

delete factory;
delete dataloader;
}

#endif

0 comments on commit f99b00c

Please sign in to comment.