Society of Robots - Robot Forum

Software => Software => Topic started by: katherina on September 27, 2007, 01:20:58 PM

Title: C++ PROGRAMMING FOR A SINGLE LAYER PERCEPTRON LEARNING...HELP PLZ...
Post by: katherina on September 27, 2007, 01:20:58 PM
i need help as i have to pass this up in 2 days  time...I have to do a program for a single layer perceptron learning for BOOLEAN LOGIC of an "OR GATE".

I can't seem to display my output...
n i wanted to display how many times(Epoch) the perceptron trained the program until the desired output is the same with the actual output.
how do i make a program to know how many times did the program is trained until the answer is correct?

please correct me on anything that u find wrong... espcially the program.

This is the one that our lecturer gave us, the perceptron learning is developed using EXCEL... I have to deelop something that can output the things just like in this EXCEL program but using other languages... and i have chosen to use C++ language..

 :)


and this is my C++ progam...:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main ()

{
    float w [2];
    int x1, x2, Yd;                           // to declare the inputs
    float Out, Yp, e, teta, alpha, ww1, ww2;  // to declare the variables used

    cout << "Please insert 'INITIAL WEIGHTS' (between -0.5 and 0.5)" << endl
         << "------------------------------------------------------" << endl;
    cout << "w1 = ";
    cin >> w[1];
    cout << "w2 = ";
    cin >> w[2];;
    cout << "Please insert the value for 'teta' and 'alpha'"<< endl;
    cout << "-----------------------------------------------" << endl;
    cout << "teta = ";
    cin >> teta;
    cout<< "alpha = ";
    cin >> alpha;
   
    /* define and initialize training data */
   
    int train [4] [3] = {   0, 0, 0,
                            0, 1, 1,
                            1, 0, 1,
                            1, 1, 1 };
           
    for (int i = 0; i < 4; ++i)
    {
    x1 = train[0]; //input for x1
    x2 = train[1]; //input for x2
    Yd = train[2]; // desired output
   //find weighted sum of inputs and threshold values
    Out = x1*w[1] + x2*w[2] - teta;
}
 cout  << Out;
}

Title: Re: C++ PROGRAMMING FOR A SINGLE LAYER PERCEPTRON LEARNING...HELP PLZ...
Post by: MadMax on September 28, 2007, 05:31:54 PM
okay:

1. after a cin>> command, you should type cin.get();

the function of cin.get() is to discard the last piece of input you've put into a certain variable. This is very usefull, because when you press 'enter' on your keyboard, the ASCII code for 'enter' is added to your input

2.
Code: [Select]
    cout << "w1 = ";
    cin >> w[1];
    cout << "w2 = ";
    cin >> w[2];;

you use the ';' sign two times after cin>> w[2];

so it should be:

Code: [Select]
    cout << "w1 = ";
    cin >> w[1];
    cout << "w2 = ";
    cin >> w[2];

3. you declare this variable:

Code: [Select]
int x1
and at the end of the program, you say that x1 should be the entire array of 'train'

Code: [Select]
x1 = train; //input for x1
x2 = train[1]; //input for x2
Yd = train[2]; // desired output
//find weighted sum of inputs and threshold values
Out = x1*w[1] + x2*w[2] - teta;

4. You use C++ to program your program. However, you use 3 c-libraries, why aren't you using: "stdlib.h", "time.h" and "math.h" I also can't see the point in using time.h because you aren't using any of it's functions. And math.h is also unnesecary because you aren't using any complex mathmetics (sin, cos, tan functions etc.)

5. Either your shift key is stuck, or you should read this article: clicky (http://en.wikipedia.org/wiki/Caps_lock)
;-)

6. Last but not least, you use a loop to define the variable 'out' a couple of times. However, your 'cout<< Out;' command is placed AFTER the for loop. Was this your intention?
Title: Re: C++ PROGRAMMING FOR A SINGLE LAYER PERCEPTRON LEARNING...HELP PLZ...
Post by: katherina on September 30, 2007, 10:54:37 PM
tq 4 d help...  ;D