C++ Neural Networks and Fuzzy Logic C++ Neural Networks and Fuzzy Logic
by Valluru B. Rao
M&T Books, IDG Books Worldwide, Inc.
ISBN: 1558515526   Pub Date: 06/01/95
  

Previous Table of Contents Next


C++ Implementation of Perceptron Network

In our C++ implementation of this network, we have the following classes: we have separate classes for input neurons and output neurons. The ineuron class is for the input neurons. This class has weight and activation as data members. The oneuron class is similar and is for the output neuron. It is declared as a friend class in the ineuron class. The output neuron class has also a data member called output. There is a network class, which is a friend class in the oneuron class. An instance of the network class is created with four input neurons. These four neurons are all connected with one output neuron.

The member functions of the ineuron class are: (1) a default constructor, (2) a second constructor that takes a real number as an argument, and (3) a function that calculates the output of the input neuron. The constructor taking one argument uses that argument to set the value of the weight on the connection between the input neuron and the output neuron. The functions that determine the neuron activations and the network output are declared public. The activations of the neurons are calculated with functions defined in the neuron classes. A threshold value is used by a member function of the output neuron to determine if the neuron’s activation is large enough for it to fire, giving an output of 1.

Header File

Listing 4.3 contains percept.h, the header file for the C++ program for the Perceptron network. percept.h contains the declarations for three classes, one for input neurons, one for output neurons, and one for network.

Listing 4.3 The percept.h header file.

//percept.h        V. Rao, H. Rao
// Perceptron model

#include <stdio.h>
#include <iostream.h>
#include <math.h>

class ineuron
{
protected:
     float weight;
     float activation;
     friend class oneuron;
public:
     ineuron() {};
     ineuron(float j) ;
     float act(float x);
};

class oneuron
{
protected:
     int output;
     float activation;
     friend class network;
public:
     oneuron() { };
     void actvtion(float x[4], ineuron *nrn);
     int outvalue(float j) ;
};

class network
{
public:
     ineuron   nrn[4];
     oneuron   onrn;
     network(float,float,float,float);

};

Implementation of Functions

The network is designed to have four neurons in the input layer. Each of them is an object of class ineuron, and these are member classes in the class network. There is one explicitly defined output neuron of the class oneuron. The network constructor also invokes the neuron constructor for each input layer neuron in the network by providing it with the initial weight for its connection to the neuron in the output layer. The constructor for the output neuron is also invoked by the network constructor, at the same time initializing the output and activation data members of the output neuron each to zero. To make sure there is access to needed information and functions, the output neuron is declared a friend class in the class ineuron. The network is declared as a friend class in the class oneuron.

Source Code for Perceptron Network

Listing 4.4 contains the source code in percept.cpp for the C++ implementation of the Perceptron model previously discussed.

Listing 4.4 Source code for Perceptron model.

//percept.cpp   V. Rao, H. Rao
//Perceptron model

#include "percept.h"
#include "stdio.h"
#include "stdlib.h"

ineuron::ineuron(float j)
{
weight= j;
}

float ineuron::act(float x)
{
float a;

a = x*weight;

return a;
}

void oneuron::actvtion(float *inputv, ineuron *nrn)
{
int i;
activation = 0;

for(i=0;i<4;i++)
     {
     cout<<"\nweight for neuron "<<i+1<<" is       "<<nrn[i].weight;
     nrn[i].activation = nrn[i].act(inputv[i]);
     cout<<"           activation is      "<<nrn[i].activation;
     activation += nrn[i].activation;
     }
cout<<"\n\nactivation is  "<<activation<<"\n";
}

int oneuron::outvalue(float j)
{
if(activation>=j)
     {
     cout<<"\nthe output neuron activation \
exceeds the threshold value of "<<j<<"\n";
     output = 1;
     }
else
     {
     cout<<"\nthe output neuron activation \
is smaller than the threshold value of "<<j<<"\n";
     output = 0;
     }

cout<<" output value is "<< output;
return (output);
}

network::network(float a,float b,float c,float d)
{
nrn[0] = ineuron(a) ;
nrn[1] = ineuron(b) ;
nrn[2] = ineuron(c) ;
nrn[3] = ineuron(d) ;
onrn = oneuron();
onrn.activation = 0;
onrn.output = 0;
}
void main (int argc, char * argv[])
{

float inputv1[]= {1.95,0.27,0.69,1.25};
float wtv1[]= {2,3,3,2}, wtv2[]= {3,0,6,2};
FILE * wfile, * infile;
int num=0, vecnum=0, i;
float threshold = 7.0;

if (argc < 2)
     {
     cerr << "Usage: percept Weightfile Inputfile";
     exit(1);
     }
// open  files

wfile= fopen(argv[1], "r");
infile= fopen(argv[2], "r");

if ((wfile == NULL) || (infile == NULL))
     {
     cout << " Can't open a file\n";
     exit(1);
     }

cout<<"\nTHIS PROGRAM IS FOR A PERCEPTRON NETWORK WITH AN INPUT LAYER OF";
cout<<"\n4 NEURONS, EACH CONNECTED TO THE OUTPUT NEURON.\n";
cout<<"\nTHIS EXAMPLE TAKES REAL NUMBERS AS INPUT SIGNALS\n";

//create the network by calling its constructor.
//the constructor calls neuron constructor as many times as the number of
//neurons in input layer of the network.

cout<<"please enter the number of weights/vectors \n";
cin >> vecnum;

for (i=1;i<=vecnum;i++)
     {
     fscanf(wfile,"%f %f %f %f\n", &wtv1[0],&wtv1[1],&wtv1[2],&wtv1[3]);
     network h1(wtv1[0],wtv1[1],wtv1[2],wtv1[3]);
     fscanf(infile,"%f %f %f %f \n",
     &inputv1[0],&inputv1[1],&inputv1[2],&inputv1[3]);
     cout<<"this is vector # " << i << "\n";
     cout << "please enter a threshold value, eg 7.0\n";
     cin >> threshold;
     h1.onrn.actvtion(inputv1, h1.nrn);
     h1.onrn.outvalue(threshold);
     cout<<"\n\n";
     }

fclose(wfile);
fclose(infile);
}


Previous Table of Contents Next

Copyright © IDG Books Worldwide, Inc.