Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TNeuron.cxx
Go to the documentation of this file.
1// @(#)root/tmva $Id$
2// Author: Matt Jachowski
3
4/**********************************************************************************
5 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6 * Package: TMVA *
7 * Class : TNeuron *
8 * *
9 * *
10 * Description: *
11 * Implementation (see header for description) *
12 * *
13 * Authors (alphabetical): *
14 * Matt Jachowski <jachowski@stanford.edu> - Stanford University, USA *
15 * *
16 * Copyright (c) 2005: *
17 * CERN, Switzerland *
18 * *
19 * Redistribution and use in source and binary forms, with or without *
20 * modification, are permitted according to the terms listed in LICENSE *
21 * (see tmva/doc/LICENSE) *
22 **********************************************************************************/
23
24/*! \class TMVA::TNeuron
25\ingroup TMVA
26Neuron class used by TMVA artificial neural network methods
27*/
28
29#include "TMVA/TNeuron.h"
30
31#include "TMVA/MsgLogger.h"
32#include "TMVA/TActivation.h"
33#include "TMVA/Tools.h"
34#include "TMVA/TNeuronInput.h"
35#include "TMVA/Types.h"
36
37#include "TH1D.h"
38#include "ThreadLocalStorage.h"
39#include "TObjArray.h"
40
41static const Int_t UNINITIALIZED = -1;
42
43using std::vector;
44
45
46////////////////////////////////////////////////////////////////////////////////
47/// standard constructor
48
53
54////////////////////////////////////////////////////////////////////////////////
55/// destructor
56
58{
59 if (fLinksIn != NULL) delete fLinksIn;
60 if (fLinksOut != NULL) delete fLinksOut;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// initialize the neuron, most variables still need to be set via setters
65
67{
68 fLinksIn = new TObjArray();
69 fLinksOut = new TObjArray();
70 fValue = UNINITIALIZED;
71 fActivationValue = UNINITIALIZED;
72 fDelta = UNINITIALIZED;
73 fDEDw = UNINITIALIZED;
74 fError = UNINITIALIZED;
75 fActivation = NULL;
76 fForcedValue = kFALSE;
77 fInputCalculator = NULL;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// force the value, typically for input and bias neurons
82
84{
85 fValue = value;
86 fForcedValue = kTRUE;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// calculate neuron input
91
93{
94 if (fForcedValue) return;
95 fValue = fInputCalculator->GetInput(this);
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// calculate neuron activation/output
100
102{
103 if (fActivation == NULL) {
104 PrintMessage( kWARNING ,"No activation equation specified." );
105 fActivationValue = UNINITIALIZED;
106 return;
107 }
108 fActivationValue = fActivation->Eval(fValue);
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// calculate error field
113
115{
116 // no need to adjust input neurons
117 if (IsInputNeuron()) {
118 fDelta = 0.0;
119 return;
120 }
121
122 Double_t error;
123
124 // output neuron should have error set all ready
125 if (IsOutputNeuron()) error = fError;
126
127 // need to calculate error for any other neuron
128 else {
129 error = 0.0;
131 // Replaced TObjArrayIter pointer by object, as creating it on the stack
132 // is much faster (5-10% improvement seen) than re-allocating the new
133 // memory for the pointer each time. Thanks to Peter Elmer who pointed this out
134 // TObjArrayIter* iter = (TObjArrayIter*)fLinksOut->MakeIterator();
135 TObjArrayIter iter(fLinksOut);
136 while (true) {
137 synapse = (TSynapse*) iter.Next();
138 if (synapse == NULL) break;
139 error += synapse->GetWeightedDelta();
140 }
141
142 }
143
144 fDelta = error * fActivation->EvalDerivative(GetValue());
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// set input calculator
149
151{
152 if (fInputCalculator != NULL) delete fInputCalculator;
153 fInputCalculator = calculator;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// set activation equation
158
160{
161 if (fActivation != NULL) delete fActivation;
162 fActivation = activation;
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// add synapse as a pre-link to this neuron
167
169{
170 if (IsInputNeuron()) return;
171 fLinksIn->Add(pre);
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// add synapse as a post-link to this neuron
176
178{
179 if (IsOutputNeuron()) return;
180 fLinksOut->Add(post);
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// delete all pre-links
185
187{
188 DeleteLinksArray(fLinksIn);
189}
190
191////////////////////////////////////////////////////////////////////////////////
192/// delete an array of TSynapses
193
195{
196 if (links == NULL) return;
197
199 Int_t numLinks = links->GetEntriesFast();
200 for (Int_t i=0; i<numLinks; i++) {
201 synapse = (TSynapse*)links->At(i);
202 if (synapse != NULL) delete synapse;
203 }
204 delete links;
205 links = NULL;
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// set error, this should only be done for an output neuron
210
212{
213 if (!IsOutputNeuron())
214 PrintMessage( kWARNING, "Warning! Setting an error on a non-output neuron is probably not what you want to do." );
215
216 fError = error;
217}
218
219////////////////////////////////////////////////////////////////////////////////
220/// update and adjust the pre-synapses for each neuron (input neuron has no pre-synapse)
221/// this method should only be called in batch mode
222
224{
225 if (IsInputNeuron()) return;
226
228 TObjArrayIter iter(fLinksIn);
229 while (true) {
230 synapse = (TSynapse*) iter.Next();
231 if (synapse == NULL) break;
232 synapse->CalculateDelta();
233 }
234
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// update the pre-synapses for each neuron (input neuron has no pre-synapse)
239/// this method should only be called in sequential mode
240
242{
243 if (IsInputNeuron()) return;
244
246 TObjArrayIter iter(fLinksIn);
247
248 while (true) {
249 synapse = (TSynapse*) iter.Next();
250 if (synapse == NULL) break;
251 synapse->InitDelta();
252 synapse->CalculateDelta();
253 synapse->AdjustWeight();
254 }
255
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// adjust the pre-synapses' weights for each neuron (input neuron has no pre-synapse)
260/// this method should only be called in batch mode
261
263{
264 if (IsInputNeuron()) return;
265
267 TObjArrayIter iter(fLinksIn);
268
269
270 while (true) {
271 synapse = (TSynapse*) iter.Next();
272 if (synapse == NULL) break;
273 synapse->AdjustWeight();
274 }
275
276}
277
278////////////////////////////////////////////////////////////////////////////////
279/// initialize the error fields of all pre-neurons
280/// this method should only be called in batch mode
281
283{
284 // an input neuron has no pre-weights to adjust
285 if (IsInputNeuron()) return;
286
288 TObjArrayIter iter(fLinksIn);
289
290 while (true) {
291 synapse = (TSynapse*) iter.Next();
292
293 if (synapse == NULL) break;
294 synapse->InitDelta();
295 }
296
297}
298
299////////////////////////////////////////////////////////////////////////////////
300/// print an array of TSynapses, for debugging
301
303{
304 if (links == NULL) {
305 Log() << kDEBUG << "\t\t\t<none>" << Endl;
306 return;
307 }
308
310
311 Int_t numLinks = links->GetEntriesFast();
312 for (Int_t i = 0; i < numLinks; i++) {
313 synapse = (TSynapse*)links->At(i);
314 Log() << kDEBUG <<
315 "\t\t\tweighta: " << synapse->GetWeight()
316 << "\t\tw-value: " << synapse->GetWeightedValue()
317 << "\t\tw-delta: " << synapse->GetWeightedDelta()
318 << "\t\tl-rate: " << synapse->GetLearningRate()
319 << Endl;
320 }
321}
322
323////////////////////////////////////////////////////////////////////////////////
324/// print activation equation, for debugging
325
327{
328 if (fActivation != NULL) Log() << kDEBUG << fActivation->GetExpression() << Endl;
329 else Log() << kDEBUG << "<none>" << Endl;
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// print message, for debugging
334
336{
337 Log() << type << message << Endl;
338}
339
340////////////////////////////////////////////////////////////////////////////////
341
343{
344 TTHREAD_TLS_DECL_ARG2(MsgLogger,logger,"TNeuron",kDEBUG); //! message logger, static to save resources
345 return logger;
346}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
ostringstream derivative to redirect and format output
Definition MsgLogger.h:57
Interface for TNeuron activation function classes.
Definition TActivation.h:42
Interface for TNeuron input calculation classes.
void AdjustSynapseWeights()
adjust the pre-synapses' weights for each neuron (input neuron has no pre-synapse) this method should...
Definition TNeuron.cxx:262
void ForceValue(Double_t value)
force the value, typically for input and bias neurons
Definition TNeuron.cxx:83
TNeuron()
standard constructor
Definition TNeuron.cxx:49
void UpdateSynapsesSequential()
update the pre-synapses for each neuron (input neuron has no pre-synapse) this method should only be ...
Definition TNeuron.cxx:241
void PrintMessage(EMsgType, TString message)
print message, for debugging
Definition TNeuron.cxx:335
void SetActivationEqn(TActivation *activation)
set activation equation
Definition TNeuron.cxx:159
void InitNeuron()
initialize the neuron, most variables still need to be set via setters
Definition TNeuron.cxx:66
MsgLogger & Log() const
Definition TNeuron.cxx:342
void AddPostLink(TSynapse *post)
add synapse as a post-link to this neuron
Definition TNeuron.cxx:177
void SetInputCalculator(TNeuronInput *calculator)
set input calculator
Definition TNeuron.cxx:150
void PrintActivationEqn()
print activation equation, for debugging
Definition TNeuron.cxx:326
void SetError(Double_t error)
set error, this should only be done for an output neuron
Definition TNeuron.cxx:211
void CalculateValue()
calculate neuron input
Definition TNeuron.cxx:92
void CalculateActivationValue()
calculate neuron activation/output
Definition TNeuron.cxx:101
void UpdateSynapsesBatch()
update and adjust the pre-synapses for each neuron (input neuron has no pre-synapse) this method shou...
Definition TNeuron.cxx:223
void DeleteLinksArray(TObjArray *&links)
delete an array of TSynapses
Definition TNeuron.cxx:194
virtual ~TNeuron()
destructor
Definition TNeuron.cxx:57
void AddPreLink(TSynapse *pre)
add synapse as a pre-link to this neuron
Definition TNeuron.cxx:168
void DeletePreLinks()
delete all pre-links
Definition TNeuron.cxx:186
void InitSynapseDeltas()
initialize the error fields of all pre-neurons this method should only be called in batch mode
Definition TNeuron.cxx:282
void CalculateDelta()
calculate error field
Definition TNeuron.cxx:114
void PrintLinks(TObjArray *links) const
print an array of TSynapses, for debugging
Definition TNeuron.cxx:302
Synapse class used by TMVA artificial neural network methods.
Definition TSynapse.h:42
Iterator of object array.
Definition TObjArray.h:117
TObject * Next() override
Return next object in array. Returns 0 when no more objects in array.
An array of TObjects.
Definition TObjArray.h:31
Basic string class.
Definition TString.h:138
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:148
static const Int_t UNINITIALIZED
Definition TNeuron.cxx:41