Thread: why can't my perceptron learn correctly?

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    why can't my perceptron learn correctly?

    hi, my perceptron seems not to be able of calculating the correct output if the output is bigger than the weights , here's my code...

    Code:
    #include <stdio.h>
    #define LR 0.1
    int wantedout;
    int out;
    int input1=0;
    int input2=1;
    long weight1=50;
    long weight2=50;
    int error;
    educate_net()
    {
    out  = ((input1*weight1) + (input2*weight2));
    error = out - wantedout;
    weight1 = weight1 - (error*LR);   
    weight2 = weight2 - (error*LR);
    }
    int main(){
    printf("wanted output:");
    while(1==1){
    scanf("%d", &wantedout);
    while(wantedout != out){
    educate_net();
    printf("%d\n", out);
    }
    }
    return 0;
    }

    so, if the wantedout is bigger than the weight1 and weight 2...it just goes to infinite loop, do you know why and how can i fix it? please...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Your indentation is terrible. (You don't have any!)
    2. You use out without initializing it.
    3. You check to see if wantedout is what you want, before you ever do anything with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3

  4. #4
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    srry, but i initialise out... and i do chek wantedout... sorry about the indentitation
    Arduino rocks!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No you don't, and no you didn't. Here's your code, indented:
    Code:
    #include <stdio.h>
    #define LR 0.1
    int wantedout;
    int out;    <--- not initialized
    int input1=0;
    int input2=1;
    long weight1=50;
    long weight2=50;
    int error;
    educate_net()
    {
        out  = ((input1*weight1) + (input2*weight2));
        error = out - wantedout;
        weight1 = weight1 - (error*LR);   
        weight2 = weight2 - (error*LR);
    }
    int main(){
        printf("wanted output:");
        while(1==1){
            scanf("%d", &wantedout);
            while(wantedout != out){  <--- checking the value before...
                educate_net();        <--- ...you actually modify them
                printf("%d\n", out);
            }
        }
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by quzah View Post
    No you don't, and no you didn't. Here's your code, indented:
    Code:
    #include <stdio.h>
    #define LR 0.1
    int wantedout;
    int out;    <--- not initialized
    int input1=0;
    int input2=1;
    long weight1=50;
    long weight2=50;
    int error;
    educate_net()
    {
        out  = ((input1*weight1) + (input2*weight2));
        error = out - wantedout;
        weight1 = weight1 - (error*LR);   
        weight2 = weight2 - (error*LR);
    }
    int main(){
        printf("wanted output:");
        while(1==1){
            scanf("%d", &wantedout);
            while(wantedout != out){  <--- checking the value before...
                educate_net();        <--- ...you actually modify them
                printf("%d\n", out);
            }
        }
        return 0;
    }

    Quzah.
    ok, but, do i need to in. out when it will be calculated?
    Arduino rocks!

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Pay a little more attention to the red highlighted parts.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    ok, but, do i need to in. out when it will be calculated?
    Technically, gcc and I think probably most compilers will initialize a global to 0 for you, but that is not guaranteed by the standard, so you should not assume that a variable you didn't initialize has any particular value.

    In other words, out could be any number initially. What if wantedout == out? The user sets wantedout, but there is no way to say for sure what out is the first time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your outer loop retains the old values however, so after giving you the right results for the first loop, it breaks after that. I don't know if that's intentional. I'd guess probably not, but then I don't know what you're really trying to do with your program, so who knows?

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by MK27 View Post
    Technically, gcc and I think probably most compilers will initialize a global to 0 for you, but that is not guaranteed by the standard, so you should not assume that a variable you didn't initialize has any particular value.
    Actually, I think that is guaranteed by the standard. Regardless, it's not something you should rely on (especially as a beginning programmer).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks View Post
    Actually, I think that is guaranteed by the standard. Regardless, it's not something you should rely on (especially as a beginning programmer).
    Not if it isn't static it isn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by quzah View Post
    Not if it isn't static it isn't.


    Quzah.
    Yes - global AND static variables are initialized to zero. Or rather, everything that gets put in .bss normally.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by IceDane View Post
    Yes - global AND static variables are initialized to zero. Or rather, everything that gets put in .bss normally.
    No. Static yes, global no. (Unless you're saying a static global, but then that's covered under the heading of 'static' already.) Feel free to prove me wrong, but I'm not seeing where it guarantees that global variables are guaranteed default initialization. I could be just too tired to see/find it, but I'm not seeing it.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    another little piece of advice is that perceptrons generally need either fixed or floating point weights, inputs and outputs. integers just won't do.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by abachler View Post
    another little piece of advice is that perceptrons generally need either fixed or floating point weights, inputs and outputs. integers just won't do.
    Oh god please no don't do this...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to learn everything
    By audinue in forum General Discussions
    Replies: 18
    Last Post: 09-01-2009, 01:29 AM
  2. Replies: 15
    Last Post: 08-09-2009, 11:20 AM
  3. how long to learn c programming
    By cmay in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 05-13-2009, 12:55 AM
  4. Trying to learn...C++ in VS .net
    By drez in forum C++ Programming
    Replies: 13
    Last Post: 07-27-2003, 09:40 AM
  5. Trying to learn guitar
    By Ben_Robotics in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-10-2003, 03:15 AM