ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mlpRegression.C
Go to the documentation of this file.
1 // This macro shows the use of an ANN for regression analysis:
2 //given a set {i} of input vectors i and a set {o} of output vectors o,
3 //one looks for the unknown function f(i)=o.
4 //The ANN can approximate this function; TMLPAnalyzer::DrawTruthDeviation
5 //methods can be used to evaluate the quality of the approximation.
6 //
7 //For simplicity, we use a known function to create test and training data.
8 //In reality this function is usually not known, and the data comes e.g.
9 //from measurements.
10 //
11 //Axel Naumann, 2005-02-02
12 
14  return sin((1.7+x)*(x-0.3)-2.3*(y+0.7));
15 }
16 
17 void mlpRegression() {
18  // create a tree with train and test data.
19  // we have two input parameters x and y,
20  // and one output value f(x,y)
21  TNtuple* t=new TNtuple("tree","tree","x:y:f");
22  TRandom r;
23  for (Int_t i=0; i<1000; i++) {
24  Float_t x=r.Rndm();
25  Float_t y=r.Rndm();
26  // fill it with x, y, and f(x,y) - usually this function
27  // is not known, and the value of f given an x and a y comes
28  // e.g. from measurements
29  t->Fill(x,y,theUnknownFunction(x,y));
30  }
31 
32  // create ANN
33  TMultiLayerPerceptron* mlp=new TMultiLayerPerceptron("x,y:10:8:f",t,
34  "Entry$%2","(Entry$%2)==0");
35  mlp->Train(150,"graph update=10");
36 
37  // analyze it
38  TMLPAnalyzer* mlpa=new TMLPAnalyzer(mlp);
39  mlpa->GatherInformations();
40  mlpa->CheckNetwork();
41  mlpa->DrawDInputs();
42 
43  // draw statistics shows the quality of the ANN's approximation
44  TCanvas* cIO=new TCanvas("TruthDeviation", "TruthDeviation");
45  cIO->Divide(2,2);
46  cIO->cd(1);
47  // draw the difference between the ANN's output for (x,y) and
48  // the true value f(x,y), vs. f(x,y), as TProfiles
49  mlpa->DrawTruthDeviations();
50 
51  cIO->cd(2);
52  // draw the difference between the ANN's output for (x,y) and
53  // the true value f(x,y), vs. x, and vs. y, as TProfiles
55 
56  cIO->cd(3);
57  // draw a box plot of the ANN's output for (x,y) vs f(x,y)
58  mlpa->GetIOTree()->Draw("Out.Out0-True.True0:True.True0>>hDelta","","goff");
59  TH2F* hDelta=(TH2F*)gDirectory->Get("hDelta");
60  hDelta->SetTitle("Difference between ANN output and truth vs. truth");
61  hDelta->Draw("BOX");
62 
63  cIO->cd(4);
64  // draw difference of ANN's output for (x,y) vs f(x,y) assuming
65  // the ANN can extrapolate
66  Double_t vx[225];
67  Double_t vy[225];
68  Double_t delta[225];
69  Double_t v[2];
70  for (Int_t ix=0; ix<15; ix++) {
71  v[0]=ix/5.-1.;
72  for (Int_t iy=0; iy<15; iy++) {
73  v[1]=iy/5.-1.;
74  Int_t idx=ix*15+iy;
75  vx[idx]=v[0];
76  vy[idx]=v[1];
77  delta[idx]=mlp->Evaluate(0, v)-theUnknownFunction(v[0],v[1]);
78  }
79  }
80  TGraph2D* g2Extrapolate=new TGraph2D("ANN extrapolation",
81  "ANN extrapolation, ANN output - truth",
82  225, vx, vy, delta);
83  g2Extrapolate->Draw("TRI2");
84 }
TTree * GetIOTree() const
Definition: TMLPAnalyzer.h:69
Double_t theUnknownFunction(Double_t x, Double_t y)
Definition: mlpRegression.C:13
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:719
void CheckNetwork()
Gives some information about the network in the terminal.
float Float_t
Definition: RtypesCore.h:53
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
int Int_t
Definition: RtypesCore.h:41
void GatherInformations()
Collect information about what is usefull in the network.
void DrawDInputs()
Draws the distribution (on the test sample) of the impact on the network output of a small variation ...
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
void mlpRegression()
Definition: mlpRegression.C:17
double sin(double)
Double_t Evaluate(Int_t index, Double_t *params) const
Returns the Neural Net for a given set of input parameters #parameters must equal #input neurons...
TThread * t[5]
Definition: threadsh1.C:13
ROOT::R::TRInterface & r
Definition: Object.C:4
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
void Train(Int_t nEpoch, Option_t *option="text", Double_t minE=0)
Train the network.
SVector< double, 2 > v
Definition: Dict.h:5
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:360
Double_t y[n]
Definition: legend1.C:17
virtual Int_t Fill()
[fNvar] Array of variables
Definition: TNtuple.cxx:168
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
#define gDirectory
Definition: TDirectory.h:221
THStack * DrawTruthDeviations(Option_t *option="")
Creates TProfiles of the difference of the MLP output minus the true value vs the true value...
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:50
THStack * DrawTruthDeviationInsOut(Int_t outnode=0, Option_t *option="")
Creates a profile of the difference of the MLP output outnode minus the true value of outnode vs the ...