25.

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++ Program for a Hopfield Network

For convenience every C++ program has two components: One is the header file with all of the class declarations and lists of include library files; the other is the source file that includes the header file and the detailed descriptions of the member functions of the classes declared in the header file. You also put the function main in the source file. Most of the computations are done by class member functions, when class objects are created in the function main, and calls are made to the appropriate functions. The header file has an .h (or .hpp) extension, as you know, and the source file has a .cpp extension, to indicate that it is a C++ code file. It is possible to have the contents of the header file written at the beginning of the .cpp file and work with one file only, but separating the declarations and implementations into two files allows you to change the implementation of a class(.cpp) without changing the interface to the class (.h).

Header File for C++ Program for Hopfield Network

Listing 4.1 contains Hop.h, the header file for the C++ program for the Hopfield network. The include files listed in it are the stdio.h, iostream.h, and math.h. The iostream.h file contains the declarations and details of the C++ streams for input and output. A network class and a neuron class, are declared in Hop.h. The data members and member functions are declared within each class, and their accessibility is specified by the keywords protected or public.

Listing 4.1 Header file for C++ program for Hopfield network.

 //Hop.h      V. Rao, H. Rao //Single layer Hopfield Network with 4 neurons #include <stdio.h> #include <iostream.h> #include <math.h> class neuron { protected:      int activation;      friend class network; public:      int weightv[4];      neuron() {};      neuron(int *j) ;      int act(int, int*); }; class network { public:      neuron   nrn[4];      int output[4];      int threshld(int) ;      void activation(int j[4]);      network(int*,int*,int*,int*); }; 

Notes on the Header File Hop.h

Notice that the data item activation in the neuron class is declared as protected. In order to make the member activation of the neuron class accessible to the network class, the network is declared a friend class in the class neuron. Also, there are two constructors for the class neuron. One of them creates the object neuron without initializing any data members. The other creates the object neuron and initializes the connection weights.

Source Code for the Hopfield Network

Listing 4.2 contains the source code for the C++ program for a Hopfield network in the file Hop.cpp. The member functions of the classes declared in Hop.h are implemented here. The function main contains the input patterns, values to initialize the weight matrix, and calls to the constructor of network class and other member functions of the network class.

Listing 4.2 Source code for C++ program for Hopfield network.

 //Hop.cpp   V. Rao, H. Rao //Single layer Hopfield Network with 4 neurons #include "hop.h" neuron::neuron(int *j) { int i; for(i=0;i<4;i++)      {      weightv[i]= *(j+i);      } } int neuron::act(int m, int *x) { int i; int a=0; for(i=0;i<m;i++)      {      a += x[i]*weightv[i];      } return a; } int network::threshld(int k) { if(k>=0)      return (1); else      return (0); } network::network(int a[4],int b[4],int c[4],int d[4]) { nrn[0] = neuron(a) ; nrn[1] = neuron(b) ; nrn[2] = neuron(c) ; nrn[3] = neuron(d) ; } void network::activation(int *patrn) { int i,j; for(i=0;i<4;i++)      {      for(j=0;j<4;j++)           {           cout<<"\n nrn["<<i<<"].weightv["<<j<<"] is "                <<nrn[i].weightv[j];           }      nrn[i].activation = nrn[i].act(4,patrn);      cout<<"\nactivation is "<<nrn[i].activation;      output[i]=threshld(nrn[i].activation);      cout<<"\noutput value is  "<<output[i]<<"\n";      } } void main () { int patrn1[]= {1,0,1,0},i; int wt1[]= {0,-3,3,-3}; int wt2[]= {-3,0,-3,3}; int wt3[]= {3,-3,0,-3}; int wt4[]= {-3,3,-3,0}; cout<<"\nTHIS PROGRAM IS FOR A HOPFIELD NETWORK WITH A SINGLE LAYER OF"; cout<<"\n4 FULLY INTERCONNECTED NEURONS. THE NETWORK SHOULD RECALL THE"; cout<<"\nPATTERNS 1010 AND 0101 CORRECTLY.\n"; //create the network by calling its constructor. // the constructor calls neuron constructor as many times as the number of // neurons in the network. network h1(wt1,wt2,wt3,wt4); //present a pattern to the network and get the activations of the neurons h1.activation(patrn1); //check if the pattern given is correctly recalled and give message for(i=0;i<4;i++)      {      if (h1.output[i] == patrn1[i])           cout<<"\n pattern= "<<patrn1[i]<<           "  output = "<<h1.output[i]<<"  component matches";      else           cout<<"\n pattern= "<<patrn1[i]<<           "  output = "<<h1.output[i]<<           "  discrepancy occurred";      } cout<<"\n\n"; int patrn2[]= {0,1,0,1}; h1.activation(patrn2); for(i=0;i<4;i++)      {      if (h1.output[i] == patrn2[i])           cout<<"\n pattern= "<<patrn2[i]<<           "  output = "<<h1.output[i]<<"  component matches";      else           cout<<"\n pattern= "<<patrn2[i]<<           "  output = "<<h1.output[i]<<           "  discrepancy occurred";        } } 


Previous Table of Contents Next

Copyright © IDG Books Worldwide, Inc.



C++ Neural Networks and Fuzzy Logic
C++ Neural Networks and Fuzzy Logic
ISBN: 1558515526
EAN: 2147483647
Year: 1995
Pages: 139

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net