A simple non-linear neuron model – source code

A simple non-linear neuron model - source code


//en.neuron.cpp - a neuron model
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

const float ETA = 0.5;
const int INPUTNO1 = 2;

using std::cout;
using std::cin;
using std::endl;

struct NEURON_T
{
 float Weights[INPUTNO1];
};

NEURON_T * InitialiseNeuron( void );
void TrainNeuron( NEURON_T * );
float Summate( NEURON_T *, float * Inputs  );
float ActivationFunction( float x );
float ActivationFunctionDerivative( float x );
float CalculateError( float Output, float Awaited );
void AdjustWeights( NEURON_T *, float * Inputs, float Output, float Error );

void TestIt( NEURON_T * );

int main()
{
 srand(time(0));
 NEURON_T * TestNeuron = InitialiseNeuron();

 int i = 0;
 while (++i < 7)
 {
 TrainNeuron( TestNeuron );
 }

 i = 0;
 while (++i < 5)
 {
 TestIt( TestNeuron );
 }

 delete TestNeuron;
 return 0;
}

NEURON_T * InitialiseNeuron( void )
{
 NEURON_T * TestNeuron = new NEURON_T;
 double RandMaxPlusOne = RAND_MAX+1.0;
 TestNeuron->Weights[0] = ((float)rand()/RandMaxPlusOne)*2 - 1;
 TestNeuron->Weights[1] = ((float)rand()/RandMaxPlusOne)*2 - 1;
 return TestNeuron;
}

void TrainNeuron( NEURON_T * TestNeuron )
{
 float Inputs[INPUTNO1];
 float Awaited;

 cout    << "\nPlease provide the traits of the flower which should be favored by the neuron:\n"
 << "Interval: <-5, 5>\n"
 << "Smells good: ";
 cin    >> Inputs[0];

 cout    << "Looks good: ";
 cin    >> Inputs[1];

 cout    << "How much should the neuron like that kind of flower? ( 0 - dislikes; 1 - likes )\n";
 cin    >> Awaited;

 float Sum = Summate( TestNeuron, Inputs );
 float Output = ActivationFunction( Sum );
 float Error = CalculateError( Output, Awaited );

 cout    << "The neuron responds: " << Output << " with weights [" << TestNeuron->Weights[0] << ", " << TestNeuron->Weights[1] << "]" << endl;

 AdjustWeights( TestNeuron, Inputs, Output, Error );

 cout    << "The new, adjusted weights of the neuron are: ["<< TestNeuron->Weights[0] << ", " << TestNeuron->Weights[1] << "]" << endl;
}

float Summate( NEURON_T * TestNeuron, float * Inputs  )
{
 //Sumowanie:
 int i;
 float result = 0;
 for ( i=0; i < INPUTNO1; i++ )
 {
 result += (float) TestNeuron->Weights[i] * Inputs[i];
 }
 return result;
}

float ActivationFunction( float x )
{
 return 1./(1. + exp(-x));
}

float ActivationFunctionDerivative( float x )
{
 return ActivationFunction( x ) * (1 - ActivationFunction( x ));
}

float CalculateError( float Output, float Awaited )
{
 return Awaited - Output;
}

void AdjustWeights( NEURON_T * TestNeuron, float * Inputs, float Output, float Error )
{
 int i;
 for ( i=0; i < INPUTNO1; i++ )
 {
 TestNeuron->Weights[i] += ETA * Error * ActivationFunctionDerivative( Output ) * Inputs[i];
 }
}

void TestIt( NEURON_T * TestNeuron )
{
 float Inputs[INPUTNO1];
 cout    << "\nPlease provide the following traits of the neuron and the neuron will decide whether it likes it or not"
 << endl
 << "Smells good: ";
 cin    >> Inputs[1];
 cout    << "Looks good: ";
 cin    >> Inputs[2];

 float Sum = Summate( TestNeuron, Inputs );
 float Output = ActivationFunction( Sum );

 cout    << "The neuron responds: "
 << Output
 << ", which means that the neuron's attitude towards this flower is "
 << ((Output < 0.4) ? ("negative") : ((Output >= 0.4 && Output <= 0.6) ? ("neutral") : ("positive")));
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.