|
#include "StdAfx.h"
#include ".\neuron.h"
#include "synapse.h"
#include <math.h>
double Neuron::momentum = 0.9;
double Neuron::learningRate = 0.05;
Neuron::~Neuron(void)
{
inlinks.RemoveAll();
outlinks.RemoveAll();
}
void Neuron::computeOutput()
{
sum=0.0;
POSITION pos = inlinks.GetHeadPosition();
Synapse* synapse = inlinks.GetAt(pos);
for (int i = 0; i < inlinks.GetCount(); i++)
{
synapse = inlinks.GetNext(pos);
sum += synapse->from->getOutput()*synapse->getWeight();
}
output = 1.0/(1.0 + exp(-sum)); // sigmoid function
}
void Neuron::computeBackpropDelta(double d) // for an output neuron
{
delta = (d - output) * output * (1.0 - output);
}
void Neuron::computeBackpropDelta() // for a hidden neuron
{
double errorSum = 0.0;
POSITION pos = outlinks.GetHeadPosition();
Synapse* synapse = outlinks.GetAt(pos);
for (int i = 0; i < outlinks.GetCount(); i++)
{
synapse = outlinks.GetNext(pos);
errorSum += synapse->to->delta * synapse->getWeight();
}
delta = output * ( 1.0 - output) * errorSum;
}
void Neuron::computeWeight()
{
POSITION pos = inlinks.GetHeadPosition();
Synapse* synapse = (Synapse*)inlinks.GetAt(pos);
for (int i = 0; i < inlinks.GetCount(); i++)
{
synapse = (Synapse*)inlinks.GetNext(pos);
synapse->data = learningRate*delta*synapse->from->getOutput()
+ momentum*synapse->data;
synapse->weight += synapse->data;
}
}
CString Neuron::print()
{
CString str;
str.Format(_T("\n%s = %f : \n"),label,output);
POSITION pos = outlinks.GetHeadPosition();
Synapse* synapse = NULL;
for (int i = 0; i < outlinks.GetCount(); i++)
{
synapse = outlinks.GetNext(pos);
str.AppendFormat(_T("%s(%.4f) "),synapse->to->label,synapse->weight);
}
OutputDebugString(str);
return str;
}
int Neuron::SetWeights(double* pWeights)
{
POSITION pos = outlinks.GetHeadPosition();
Synapse* synapse = NULL;
for (int i = 0; i < outlinks.GetCount(); i++)
{
synapse = outlinks.GetNext(pos);
synapse->weight = pWeights[i];
}
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Currently I'm working at Dept. of Electrical Engineering in University of Shahrood.
Pattern Recognition (specially OCR), Neural Networks, Image Processing and Machine Vision are my interests. However I'm a PROGRAMMER as well.
BSc: Sharif University of technology @ 2002
MSc. and PhD: Tarbiat Modarres University @ 2006 & 2010 respectively
Personal Blog:
Andisheh Online
Religious Blogs:
Shia Muslims ,
Islamic Quotes
Company Site:
Shahaab-co
My old Site:
Farsi OCR