#include "Riostream.h"
#include "TH1D.h"
#ifndef ROOT_TMVA_TNeuron
#include "TMVA/TNeuron.h"
#endif
#ifndef ROOT_TMVA_TActivation
#include "TMVA/TActivation.h"
#endif
#ifndef ROOT_TMVA_Tools
#include "TMVA/Tools.h"
#endif
#ifndef ROOT_TMVA_TNeuronInput
#include "TMVA/TNeuronInput.h"
#endif
static const Int_t UNINITIALIZED = -1;
using std::vector;
ClassImp(TMVA::TNeuron)
#ifdef _WIN32
#pragma warning ( disable : 4355 )
#endif
TMVA::TNeuron::TNeuron()
: fLogger( this, kDEBUG )
{
InitNeuron();
}
TMVA::TNeuron::~TNeuron()
{
if (fLinksIn != NULL) delete fLinksIn;
if (fLinksOut != NULL) delete fLinksOut;
}
void TMVA::TNeuron::InitNeuron()
{
fLinksIn = new TObjArray();
fLinksOut = new TObjArray();
fValue = UNINITIALIZED;
fActivationValue = UNINITIALIZED;
fDelta = UNINITIALIZED;
fError = UNINITIALIZED;
fActivation = NULL;
fForcedValue = kFALSE;
fInputCalculator = NULL;
}
void TMVA::TNeuron::ForceValue(Double_t value)
{
fValue = value;
fForcedValue = kTRUE;
}
void TMVA::TNeuron::CalculateValue()
{
if (fForcedValue) return;
fValue = fInputCalculator->GetInput(this);
}
void TMVA::TNeuron::CalculateActivationValue()
{
if (fActivation == NULL) {
PrintMessage( kWARNING ,"No activation equation specified." );
fActivationValue = UNINITIALIZED;
return;
}
fActivationValue = fActivation->Eval(fValue);
}
void TMVA::TNeuron::CalculateDelta()
{
if (IsInputNeuron()) {
fDelta = 0.0;
return;
}
Double_t error;
if (IsOutputNeuron()) error = fError;
else {
error = 0.0;
TSynapse* synapse = NULL;
TObjArrayIter* iter = (TObjArrayIter*)fLinksOut->MakeIterator();
while (true) {
synapse = (TSynapse*) iter->Next();
if (synapse == NULL) break;
error += synapse->GetWeightedDelta();
}
delete iter;
}
fDelta = error * fActivation->EvalDerivative(GetValue());
}
void TMVA::TNeuron::SetInputCalculator(TNeuronInput* calculator)
{
if (fInputCalculator != NULL) delete fInputCalculator;
fInputCalculator = calculator;
}
void TMVA::TNeuron::SetActivationEqn(TActivation* activation)
{
if (fActivation != NULL) delete fActivation;
fActivation = activation;
}
void TMVA::TNeuron::AddPreLink(TSynapse* pre)
{
if (IsInputNeuron()) return;
fLinksIn->Add(pre);
}
void TMVA::TNeuron::AddPostLink(TSynapse* post)
{
if (IsOutputNeuron()) return;
fLinksOut->Add(post);
}
void TMVA::TNeuron::DeletePreLinks()
{
DeleteLinksArray(fLinksIn);
}
void TMVA::TNeuron::DeleteLinksArray(TObjArray*& links)
{
if (links == NULL) return;
TSynapse* synapse = NULL;
Int_t numLinks = links->GetEntriesFast();
for (Int_t i=0; i<numLinks; i++) {
synapse = (TSynapse*)links->At(i);
if (synapse != NULL) delete synapse;
}
delete links;
links = NULL;
}
void TMVA::TNeuron::SetError(Double_t error)
{
if (!IsOutputNeuron())
PrintMessage( kWARNING, "Warning! Setting an error on a non-output neuron is probably not what you want to do." );
fError = error;
}
void TMVA::TNeuron::UpdateSynapsesBatch()
{
if (IsInputNeuron()) return;
TSynapse* synapse = NULL;
TObjArrayIter* iter = (TObjArrayIter*) fLinksIn->MakeIterator();
while (true) {
synapse = (TSynapse*) iter->Next();
if (synapse == NULL) break;
synapse->CalculateDelta();
}
delete iter;
}
void TMVA::TNeuron::UpdateSynapsesSequential()
{
if (IsInputNeuron()) return;
TSynapse* synapse = NULL;
TObjArrayIter* iter = (TObjArrayIter*) fLinksIn->MakeIterator();
while (true) {
synapse = (TSynapse*) iter->Next();
if (synapse == NULL) break;
synapse->InitDelta();
synapse->CalculateDelta();
synapse->AdjustWeight();
}
delete iter;
}
void TMVA::TNeuron::AdjustSynapseWeights()
{
if (IsInputNeuron()) return;
TSynapse* synapse = NULL;
TObjArrayIter* iter = (TObjArrayIter*) fLinksIn->MakeIterator();
while (true) {
synapse = (TSynapse*) iter->Next();
if (synapse == NULL) break;
synapse->AdjustWeight();
}
delete iter;
}
void TMVA::TNeuron::InitSynapseDeltas()
{
if (IsInputNeuron()) return;
TSynapse* synapse = NULL;
TObjArrayIter* iter = (TObjArrayIter*) fLinksIn->MakeIterator();
while (true) {
synapse = (TSynapse*) iter->Next();
if (synapse == NULL) break;
synapse->InitDelta();
}
delete iter;
}
void TMVA::TNeuron::PrintLinks(TObjArray* links)
{
if (links == NULL) {
fLogger << kDEBUG << "\t\t\t<none>" << Endl;
return;
}
TSynapse* synapse;
Int_t numLinks = links->GetEntriesFast();
for (Int_t i = 0; i < numLinks; i++) {
synapse = (TSynapse*)links->At(i);
fLogger << kDEBUG <<
"\t\t\tweighta: " << synapse->GetWeight()
<< "\t\tw-value: " << synapse->GetWeightedValue()
<< "\t\tw-delta: " << synapse->GetWeightedDelta()
<< "\t\tl-rate: " << synapse->GetLearningRate()
<< Endl;
}
}
void TMVA::TNeuron::PrintActivationEqn()
{
if (fActivation != NULL) fLogger << kDEBUG << fActivation->GetExpression() << Endl;
else fLogger << kDEBUG << "<none>" << Endl;
}
void TMVA::TNeuron::PrintMessage( EMsgType type, TString message)
{
fLogger << type << message << Endl;
}
Last change: Wed Jun 25 08:48:51 2008
Last generated: 2008-06-25 08:48
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.