3 #include "StPmdNeuNet.h"
12 Float_t Teach[200000]={0.};
13 Float_t Value[200000]={0.};
20 AllocateVW(nInput,hidden,nOutput);
34 fRandom.SetSeed(temps.Convert());
35 gMessMgr->Info()<<
"StPmdNeuNet::StPmdNeuNet: First Random Seed = "<<fRandom.GetSeed();
36 gMessMgr->Info()<<
"StPmdNeuNet::StPmdNeuNet: Neural Network is created";
57 fRandom.SetSeed(temps.Convert());
58 gMessMgr->Info()<<
"StPmdNeuNet::StPmdNeuNet: First Random Seed = "<<fRandom.GetSeed();
64 StPmdNeuNet::~StPmdNeuNet()
67 gMessMgr->Info()<<
"StPmdNeuNet::~StPmdNeuNet : we are done ";
70 if(fEventsList)
delete [] fEventsList;
75 void StPmdNeuNet::ZeroAll()
94 void StPmdNeuNet::SetHidden(
const Text_t *ttext)
102 for (i=0;text[i];i++)
if(text[i]==
':')fNHiddL++;
103 if (fNUnits)
delete [] fNUnits;
104 fNUnits =
new Int_t[fNHiddL+2];
107 for (i=1;i<=fNHiddL;i++)
109 number=
new TString();
110 while(text[j]&&(text[j]!=
':')){number->Append(text[j]);j++;}
112 sscanf(number->Data(),
"%i",&fNUnits[i]);
120 void StPmdNeuNet::FreeVW()
128 for (i=0;i<fNHiddL+2;i++)
129 {
delete [] fValues[i];
delete [] fErrors[i];
delete [] fBiases[i];
delete [] fDB[i];}
130 delete [] fValues;
delete [] fErrors;
delete [] fBiases;
delete [] fDB;
146 for (i=0;i<fNHiddL+1;i++)
148 for(l=0;l<fNUnits[i];l++){
delete [] fW[i][l];
delete [] fDW[i][l];}
149 delete [] fW[i];
delete [] fDW[i];
156 if (fNUnits){
delete [] fNUnits; fNUnits=0;}
159 void StPmdNeuNet::AllocateVW(Int_t nInput,
const Text_t *hidden, Int_t nOutput)
164 gMessMgr->Info()<<
"StPmdNeuNet::AllocateVW: free memory first ";
170 fNUnits[fNHiddL+1]=nOutput;
174 fValues =
new Float_t*[fNHiddL+2];
175 fErrors =
new Double_t*[fNHiddL+2];
176 fBiases =
new Double_t*[fNHiddL+2];
177 fDB =
new Double_t*[fNHiddL+2];
179 for (i=0;i<fNHiddL+2;i++)
181 fValues[i]=
new Float_t[fNUnits[i]];
182 fErrors[i]=
new Double_t[fNUnits[i]];
183 fBiases[i]=
new Double_t[fNUnits[i]];
184 fDB[i]=
new Double_t[fNUnits[i]];
189 fTeach=
new Float_t[fNUnits[fNHiddL+1]];
193 fW=
new Double_t**[fNHiddL+1];
194 fDW=
new Double_t**[fNHiddL+1];
196 for (i=0;i<fNHiddL+1;i++)
198 fW[i]=
new Double_t*[fNUnits[i]];
199 fDW[i]=
new Double_t*[fNUnits[i]];
200 for (l=0;l<fNUnits[i];l++)
202 fW[i][l]=
new Double_t[fNUnits[i+1]];
203 fDW[i][l]=
new Double_t[fNUnits[i+1]];
209 void StPmdNeuNet::SetKernel(Int_t nInput,
const Text_t *hidden, Int_t nOutput)
212 AllocateVW(nInput,hidden,nOutput);
224 fLearnParam=fabs(learnParam);
228 gMessMgr->Info()<<
"StPmdNeuNet::AllocateVW: free memory first ";
229 if (fLearnParam>1.0) gMessMgr->Info()<<
"StPmdNeuNet::SetLearnParam: Warning : is not an usual value "<<fLearnParam;
230 if (fLearnParam==0.0) gMessMgr->Info()<<
"StPmdNeuNet::SetLearnParam: Warning : 0 is a stupid value";
231 gMessMgr->Info()<<
"StPmdNeuNet::SetLearnParam: Learning Parameter set to : "<<fLearnParam;
232 gMessMgr->Info()<<
"StPmdNeuNet::SetLearnParam: Flat Spot elimination value set to :"<<fFlatSE;
233 gMessMgr->Info()<<
"StPmdNeuNet::SetLearnParam: Momentum set to : "<<fMu;
242 fLowerInitWeight=lowerInitWeight;
243 fUpperInitWeight=upperInitWeight;
244 if (fLowerInitWeight>fUpperInitWeight)
246 temp=fUpperInitWeight;
247 fUpperInitWeight=fLowerInitWeight;
248 fLowerInitWeight=temp;
250 if (fLowerInitWeight==fUpperInitWeight)
251 gMessMgr->Info()<<
"StPmdNeuNet::SetInitParam: Warning : the weights initialisation bounds are equal ";
252 gMessMgr->Info()<<
"StPmdNeuNet::SetInitParam: Init Parameters set to ";
253 gMessMgr->Info()<<
"StPmdNeuNet::SetInitParam: --> Lower bound = "<<fLowerInitWeight;
254 gMessMgr->Info()<<
"StPmdNeuNet::SetInitParam: --> Upper bound = "<<fUpperInitWeight;
259 Float_t StPmdNeuNet::Alea()
261 return fLowerInitWeight+fRandom.Rndm()*(fUpperInitWeight-fLowerInitWeight);
275 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::Init: allocate memory first";
return;}
279 for (i=0;i<fNHiddL+1;i++)
280 for (l=0;l<fNUnits[i];l++)
281 for (c=0;c<fNUnits[i+1];c++) fW[i][l][c]=(Double_t)Alea();
283 for(i=0;i<fNHiddL+1;i++)
for(l=0;l<fNUnits[i];l++)
for(c=0;c<fNUnits[i+1];c++)
288 for (i=1;i<fNHiddL+2;i++)
289 for (l=0;l<fNUnits[i];l++) fBiases[i][l]=(Double_t)(Alea())*fUseBiases;
291 for(i=1;i<fNHiddL+2;i++)
for(l=0;l<fNUnits[i];l++)fDB[i][l]=0.;
295 gMessMgr->Info()<<
"StPmdNeuNet::Init: Initialisation done";
304 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::PrintS: no unit ";
return;}
306 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: +++++++++ Neural Network ++++++++++++"<<GetName();
307 for(i=0;i<fNHiddL+2;i++)gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Layer contains units"<<i<<
" "<<fNUnits[i];
309 if(fUseBiases)gMessMgr->Info()<<
"StPmdNeuNet::PrintS: >>>>>>> Biases USED";
310 else gMessMgr->Info()<<
"StPmdNeuNet::PrintS: >>>>>>>Biases DUMMY";
312 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: ---------- Biases ---------- ";
314 for(i=0;i<fNHiddL+2;i++)if(fNUnits[i]>=maxl)maxl=fNUnits[i];
315 for(i=0;i<fNHiddL+2;i++)gMessMgr->Info()<<
" | "<<i;
316 for(i=0;i<fNHiddL+2;i++)gMessMgr->Info()<<
"--------|-";
319 for(i=0;i<fNHiddL+2;i++)
320 if(l<fNUnits[i])gMessMgr->Info()<<
"StPmdNeuNet::PrintS: | "<<fBiases[i][l];
321 else gMessMgr->Info()<<
" | ";
325 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: ---------- Weights ----------- ";
326 for(i=0;i<fNHiddL+1;i++)
328 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: From "<<i<<
" to " <<i+1;
329 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: "<<i;
for(l=0;l<fNUnits[i];l++)gMessMgr->Info()<<
" |"<<l;
330 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: ===|";
for(l=0;l<fNUnits[i];l++)gMessMgr->Info()<<
"-------";
331 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: |"<<i+1;
for(l=0;l<fNUnits[i];l++)gMessMgr->Info()<<
"-------";
332 for(c=0;c<fNUnits[i+1];c++)
334 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: |"<<c;
335 for(l=0;l<fNUnits[i];l++)gMessMgr->Info()<<
"|"<<fW[i][l][c];
339 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Learning parameter = "<<fLearnParam;
340 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Flat Spot elimination value = "<<fFlatSE;
341 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Momentum = "<<fMu;
342 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Lower initialisation weight = "<<fLowerInitWeight;
343 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Upper initialisation weight = "<<fUpperInitWeight;
344 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Number of events for training = "<<fNTrainEvents;
345 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Number of events for validation = "<<fNValidEvents;
346 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: Number of cycles done = "<<fNTrainCycles;
347 gMessMgr->Info()<<
"StPmdNeuNet::PrintS: +++++++++++++++++++++++++++++++++++++++++++++++";
355 void StPmdNeuNet::Forward()
361 gMessMgr->Info()<<
"StPmdNeuNet::Forward no unit !";
365 for (i=0;i<fNHiddL+1;i++)
366 for (c=0;c<fNUnits[i+1];c++)
369 for(l=0;l<fNUnits[i];l++)sum+=fW[i][l][c]*(Double_t)fValues[i][l];
370 fValues[i+1][c]=(Float_t)Sigmoide(sum+fBiases[i+1][c]*fUseBiases);
377 void StPmdNeuNet::LearnBackward()
379 if(fNTrainEvents<1){gMessMgr->Info()<<
"StPmdNeuNet::LearnBackward: No event to train !!!";
return;}
380 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::LearnBackward: no unit !";
return;}
387 for (i=0;i<fNHiddL+1;i++)
388 for (l=0;l<fNUnits[i];l++)
389 for(c=0;c<fNUnits[i+1];c++)
391 delta=fLearnParam*fErrors[i+1][c]*(Double_t)fValues[i][l]+fMu*fDW[i][l][c];
396 if(((Bool_t)fUseBiases))
398 for (i=1;i<fNHiddL+2;i++)
399 for (l=0;l<fNUnits[i];l++)
401 delta=fLearnParam*fErrors[i][l]+fMu*fDB[i][l];
402 fBiases[i][l]+=delta;
413 Double_t StPmdNeuNet::Error()
417 Double_t sum,error=0,errorOneUnit;
418 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::Error: no unit !";
return 0;}
422 for(l=0;l<fNUnits[fNHiddL+1];l++)
424 errorOneUnit=(Double_t)(fTeach[l]-fValues[fNHiddL+1][l]);
427 error+=fabs(errorOneUnit);
428 fErrors[fNHiddL+1][l]=errorOneUnit*(SigPrim(fValues[fNHiddL+1][l])+fFlatSE);
430 error=error/(Double_t)fNUnits[fNHiddL+1];
434 for(i=fNHiddL;i==1;i--)
436 for(l=0;l<fNUnits[i];l++)
439 for(c=0;c<fNUnits[i+1];c++) sum+=fW[i][l][c]*fErrors[i+1][c];
440 fErrors[i][l]=sum*(SigPrim((Double_t)fValues[i][l])+fFlatSE);
453 Double_t StPmdNeuNet::ErrorO()
460 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::ErrorO: no unit !";
return 0;}
461 for(l=0;l<fNUnits[fNHiddL+1];l++)
462 error+=fabs((Double_t)(fTeach[l]-fValues[fNHiddL+1][l]));
463 error=error/(Double_t)fNUnits[fNHiddL+1];
479 if(fNTrainEvents<1){gMessMgr->Info()<<
"StPmdNeuNet::TrainOneCycle: No event to train !!!";
return 0.;}
480 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::TrainOneCycle: no unit !";
return 0.;}
486 for(i=0;i<fNTrainEvents;i++)
488 GetArrayEvt(fEventsList[i]);
490 for(Int_t l=0;l<fNUnits[fNHiddL+1];l++)
493 Value[i]=fValues[fNHiddL+1][l];
505 error=error/(Double_t)fNTrainEvents;
506 gMessMgr->Info()<<
"StPmdNeuNet::TrainOneCycle: cycle : E_t = "<<fNTrainCycles<<
" "<<error;
520 if(fNValidEvents<1)
return 0.;
527 for (Int_t j=0;j<fNValidEvents;j++)
531 error=error/(Double_t)fNValidEvents;
544 for(Int_t i=0;i<nCycles;i++)
548 errv=(Float_t)
Valid();
549 gMessMgr->Info()<<
"StPmdNeuNet::TrainNCycles: cycle > train : "<<fNTrainCycles<<
" "<<errt;
550 if(fNValidEvents)gMessMgr->Info()<<
"StPmdNeuNet::TrainNCycles: and valid : ";
551 else gMessMgr->Info()<<(
" ");
568 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::Export: no unit !";
return;}
571 file = fopen(fileName,
"w");
573 gMessMgr->Info()<<
"StPmdNeuNet::Export: ERROR Cannot open for write "<<fileName;
577 fprintf(file,
"%8i\n",fNTrainEvents);
578 for(l=0;l<fNTrainEvents;l++)fprintf(file,
"%8.4f %8.4f\n",Teach[l],Value[l]);
581 m_DiscMaker->mNNoutput->Fill(Value[l]);
583 fprintf(file,
"%3i\n",fNHiddL);
584 for(i=0;i<fNHiddL+2;i++)fprintf(file,
"%3i\n",fNUnits[i]);
586 for(i=0;i<fNHiddL+2;i++)
587 for(l=0;l<fNUnits[i];l++)fprintf(file,
"%8.4f\n",fBiases[i][l]);
589 for(i=0;i<fNHiddL+1;i++)
590 for(l=0;l<fNUnits[i];l++)
591 for(c=0;c<fNUnits[i+1];c++)fprintf(file,
"%8.4f\n",fW[i][l][c]);
593 fprintf(file,
"%5i\n",fNTrainCycles);
594 fprintf(file,
"%2.0f\n",fUseBiases);
608 Int_t i,l,c,newI,newHL,newO;
609 Text_t hidden[100],piece[5];
611 file = fopen(fileName,
"r");
614 gMessMgr->Info()<<
"StPmdNeuNet::Import: ERROR Cannot open for read"<<fileName;
618 fscanf(file,
"%3i",&newHL);
619 fscanf(file,
"%3i",&newI);
622 {fscanf(file,
"%s",piece);strcat(hidden,piece);strcat(hidden,
":");}
623 fscanf(file,
"%s",piece);strcat(hidden,piece);
624 fscanf(file,
"%3i",&newO);
626 gMessMgr->Info()<<
"StPmdNeuNet::Import: New NN set to : "<<newI<<
" "<<hidden<<
" "<<newO;
629 gMessMgr->Info()<<
"StPmdNeuNet::Import: Allocating";
630 AllocateVW(newI,hidden,newO);
632 gMessMgr->Info()<<
"StPmdNeuNet::Import: Filling fDB+fscanf()";
634 for(i=0;i<fNHiddL+2;i++)
635 for(l=0;l<fNUnits[i];l++){fDB[i][l]=0.;fscanf(file,
"%f",&tmpfl);*(fBiases[i]+l)=(Double_t)tmpfl;}
637 for(i=0;i<fNHiddL+1;i++)
638 for(l=0;l<fNUnits[i];l++)
639 for(c=0;c<fNUnits[i+1];c++){
640 fDW[i][l][c]=0.;fscanf(file,
"%f",&tmpfl);*(fW[i][l]+c)=(Double_t)tmpfl;
645 fscanf(file,
"%5i",&fNTrainCycles);
646 fscanf(file,
"%f",&tmpfl);fUseBiases=(Double_t)tmpfl;
648 gMessMgr->Info()<<
"StPmdNeuNet::Import: Done";
660 for (i=0;i<3*fNTrainEvents;i++)
662 i1=(Int_t)(fRandom.Rndm()*(Float_t)fNTrainEvents);
663 i2=(Int_t)(fRandom.Rndm()*(Float_t)fNTrainEvents);
664 temp=fEventsList[i1];
665 fEventsList[i1]=fEventsList[i2];
666 fEventsList[i2]=temp;
674 void StPmdNeuNet::SetArraySize(Int_t size)
677 if (fEventsList)
delete [] fEventsList;
681 fArrayIn =
new Float_t*[fNTrainEvents];
682 for (i=0;i<fNTrainEvents;i++) fArrayIn[i] =
new Float_t[fNUnits[0]];
685 fArrayOut =
new Float_t*[fNTrainEvents];
686 for (i=0;i<fNTrainEvents;i++) fArrayOut[i] =
new Float_t[fNUnits[fNHiddL+1]];
688 fEventsList =
new Int_t[fNTrainEvents];
689 for (i=0;i<fNTrainEvents;i++)fEventsList[i]=i;
692 void StPmdNeuNet::DeleteArray()
698 for (i=0;i<fNTrainEvents;i++)
delete [] fArrayIn[i];
705 for (i=0;i<fNTrainEvents;i++)
delete [] fArrayOut[i];
754 void StPmdNeuNet::FillArray(Int_t iev,Int_t iunit,Float_t value)
758 fArrayIn[iev][iunit]=value;
773 if(fNTrainEvents<1){gMessMgr->Info()<<
"StPmdNeuNet::ApplyWeights: No event to train !!!";
return 0.;}
774 if(!fW){gMessMgr->Info()<<
"StPmdNeuNet::ApplyWeights: no unit !";
return 0.;}
776 file1=fopen(
"testout",
"w");
781 for(i=0;i<fNTrainEvents;i++)
783 GetArrayEvt(fEventsList[i]);
785 for(Int_t l=0;l<fNUnits[fNHiddL+1];l++)
788 Value[i]=fValues[fNHiddL+1][l];
793 fprintf(file1,
"%d %8.4f %8.4f\n",i,Teach[i],Value[i]);
802 error=error/(Double_t)fNTrainEvents;
virtual Double_t ApplyWeights(Float_t *, Float_t *)
virtual void SetLearnParam(Double_t learnParam=0.2, Double_t fse=0., Double_t mu=0.)
StPmdNeuNet()
Constructor with no parameter . Purpose ??
virtual void SetInitParam(Float_t lowerInitWeight=-1., Float_t upperInitWeight=1.)
Sets the initialisation parameters : max and min weights.
virtual void TrainNCycles(Int_t nCycles=10)
virtual void Export(const Text_t *fileName="exportNN.dat")
virtual void Import(const Text_t *fileName="exportNN.dat")
virtual void PrintS()
prints structure of network on screen
virtual Double_t TrainOneCycle()