Thread: Program that learns to recognize ANY letter.No questions, just code.

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

    Smile Program that learns to recognize ANY letter.No questions, just code.

    Hi, another program by me, this one recognises ANY letter you want him to recognise!
    Only bug is that you can't input a letter, you will have to do it you're self.

    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    float weight[100];
    bool percept[100];
    int input[100];
    int goal;
    int lc;
    float threshold = 0.5f;
    int successive_right = 0;
    int total_go_round = 1;
    const float learning_rate = 0.1f;
    
    
    int educate() {
    
                int sign = goal ? 1 : -1; //sign of (y-f(x))
                weight[0] += learning_rate*sign*input[0];
                weight[1] += learning_rate*sign*input[1];
                weight[2] += learning_rate*sign*input[2];
                weight[3] += learning_rate*sign*input[3];
                weight[4] += learning_rate*sign*input[4];
                weight[5] += learning_rate*sign*input[5];
                weight[6] += learning_rate*sign*input[6];
                weight[7] += learning_rate*sign*input[7];
                weight[8] += learning_rate*sign*input[8];
                weight[9] += learning_rate*sign*input[9];
                weight[10] += learning_rate*sign*input[10];
                weight[11] += learning_rate*sign*input[11];
                weight[12] += learning_rate*sign*input[12];
                weight[13] += learning_rate*sign*input[13];
                weight[14] += learning_rate*sign*input[14];
                weight[15] += learning_rate*sign*input[15];
                weight[16] += learning_rate*sign*input[16];
                weight[17] += learning_rate*sign*input[17];
                weight[18] += learning_rate*sign*input[18];
                weight[19] += learning_rate*sign*input[19];
                weight[20] += learning_rate*sign*input[20];
                weight[21] += learning_rate*sign*input[21];
                weight[22] += learning_rate*sign*input[22];
                weight[23] += learning_rate*sign*input[23];
                weight[24] += learning_rate*sign*input[24];
                weight[25] += learning_rate*sign*input[25];
                weight[26] += learning_rate*sign*input[26];
                weight[27] += learning_rate*sign*input[27];
                weight[28] += learning_rate*sign*input[28];
                weight[29] += learning_rate*sign*input[29];
                weight[30] += learning_rate*sign*input[30];
                weight[31] += learning_rate*sign*input[31];
                weight[32] += learning_rate*sign*input[32];
                weight[33] += learning_rate*sign*input[33];
                weight[34] += learning_rate*sign*input[34];
                weight[35] += learning_rate*sign*input[35];
                weight[36] += learning_rate*sign*input[36];
                weight[37] += learning_rate*sign*input[37];
                weight[38] += learning_rate*sign*input[38];
                weight[39] += learning_rate*sign*input[39];
                weight[40] += learning_rate*sign*input[40];
                weight[41] += learning_rate*sign*input[41];
                weight[42] += learning_rate*sign*input[42]; 
                weight[43] += learning_rate*sign*input[43];
                weight[44] += learning_rate*sign*input[44];
                weight[45] += learning_rate*sign*input[45];
                weight[46] += learning_rate*sign*input[46];
                weight[47] += learning_rate*sign*input[47];
                weight[48] += learning_rate*sign*input[48];
                weight[49] += learning_rate*sign*input[49];
                weight[50] += learning_rate*sign*input[50];
                weight[51] += learning_rate*sign*input[51];
                weight[52] += learning_rate*sign*input[52];
                weight[53] += learning_rate*sign*input[53];
                weight[54] += learning_rate*sign*input[54];
                weight[55] += learning_rate*sign*input[55];
                weight[56] += learning_rate*sign*input[56];
                weight[57] += learning_rate*sign*input[57];
                weight[58] += learning_rate*sign*input[58];
                weight[59] += learning_rate*sign*input[59];
                weight[60] += learning_rate*sign*input[60];
                weight[61] += learning_rate*sign*input[61];
                weight[62] += learning_rate*sign*input[62];
                weight[63] += learning_rate*sign*input[63];
                weight[64] += learning_rate*sign*input[64];
                weight[65] += learning_rate*sign*input[65];
                weight[66] += learning_rate*sign*input[66];
                weight[67] += learning_rate*sign*input[67];
                weight[68] += learning_rate*sign*input[68];
                weight[69] += learning_rate*sign*input[69];
                weight[70] += learning_rate*sign*input[70];
                weight[71] += learning_rate*sign*input[71];
                weight[72] += learning_rate*sign*input[72];
                weight[73] += learning_rate*sign*input[73];
                weight[74] += learning_rate*sign*input[74];
                weight[75] += learning_rate*sign*input[75];
                weight[76] += learning_rate*sign*input[76];
                weight[77] += learning_rate*sign*input[77];
                weight[78] += learning_rate*sign*input[78];
                weight[79] += learning_rate*sign*input[79];
                weight[80] += learning_rate*sign*input[80];
                weight[81] += learning_rate*sign*input[81];
                weight[82] += learning_rate*sign*input[82];
                weight[83] += learning_rate*sign*input[83];
                weight[84] += learning_rate*sign*input[84];
                weight[85] += learning_rate*sign*input[85];
                weight[86] += learning_rate*sign*input[86];
                weight[87] += learning_rate*sign*input[87];
                weight[88] += learning_rate*sign*input[88];
                weight[89] += learning_rate*sign*input[89];
                weight[90] += learning_rate*sign*input[90];
                weight[91] += learning_rate*sign*input[91];
                weight[92] += learning_rate*sign*input[92];
                weight[93] += learning_rate*sign*input[93];
                weight[94] += learning_rate*sign*input[94];
                weight[95] += learning_rate*sign*input[95];
                weight[96] += learning_rate*sign*input[96];
                weight[97] += learning_rate*sign*input[97];
                weight[98] += learning_rate*sign*input[98];
                weight[99] += learning_rate*sign*input[99];
        printf("%d", percept[0]);
        return;
    }
    
    int main(){
       srand(time(NULL));
       scanf("%d", &lc);
       while(total_go_round <= lc){
    
    //letter input, 10x10 pixels
          percept[0] =  (weight[0]*input[0]+weight[1]*input[1]+weight[2]*input[2]+weight[3]*input[3]+weight[4]*input[4]+weight [5]*input[5]+weight[6]*input[6]+weight[7]*input[7]+weight[8]*input[8]+weight[9]*input[9]+weight[10]*input[10]+weight[11]*input[11]+weight[12]*input[12]+weight[13]*input[13]+weight[14]*input[14]+weight[15]*input[15]+weight[16]*input[16]+weight[17]*input[17]+weight[18]*input[18]+weight[19]*input[19]+weight[20]*input[20]+weight[21]*input[21]+weight[22]*input[22]+weight[23]*input[23]+weight[24]*input[24]+weight[25]*input[25]+weight[26]*input[26]+weight[27]*input[27]+weight[28]*input[28]+weight[29]*input[29]+weight[30]*input[30]+weight[31]*input[31]+weight[32]*input[32]+weight[33]*input[33]+weight[34]*input[34]+weight[35]*input[35]+weight[36]*input[36]+weight[37]*input[37]+weight[38]*input[38]+weight[39]*input[39]+weight[40]*input[40]+weight[41]*input[41]+weight[42]*input[42]+weight[43]*input[43]+weight[44]*input[44]+weight[45]*input[45]+weight[46]*input[46]+weight[47]*input[47]+weight[48]*input[48]+weight[49]*input[49]+weight[50]*input[50]+weight[51]*input[51]+weight[52]*input[52]+weight[53]*input[53]+weight[54]*input[54]+weight[55]*input[55]+weight[56]*input[56]+weight[57]*input[57]+weight[58]*input[58]+weight[59]*input[59]+weight[60]*input[60]+weight[61]*input[61]+weight[62]*input[62]+weight[63]*input[63]+weight[64]*input[64]+weight[65]*input[65]+weight[66]*input[66]+weight[67]*input[67]+weight[68]*input[68]+weight[69]*input[69]+weight[70]*input[70]+weight[71]*input[71]+weight[72]*input[72]+weight[73]*input[73]+weight[74]*input[74]+weight[75]*input[75]+weight[76]*input[76]+weight[77]*input[77]+weight[78]*input[78]+weight[79]*input[79]+weight[80]*input[80]+weight[81]*input[81]+weight[82]*input[82]+weight[83]*input[83]+weight[84]*input[84]+weight[85]*input[85]+weight[86]*input[86]+weight[87]*input[87]+weight[88]*input[88]+weight[89]*input[89]+weight[90]*input[90]+weight[91]*input[91]+weight[92]*input[92]+weight[93]*input[93]+weight[94]*input[94]+weight[95]*input[95]+weight[96]*input[96]+weight[97]*input[97]+weight[98]*input[98]+weight[99]*input[99]) > threshold;
       printf("perceptron: ");
       printf("%d\n", percept[0]);
       scanf("%d", &goal);
         switch(goal){
         case 1:
           educate();
         break;
         default:
           educate();
    }
    total_go_round++;
    }
    return 0;
    }
    Do you like it?
    Arduino rocks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'd like it more if you had used a for loop or two . . . .
    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.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    jesus christ man

    replace all that stuff in the middle with

    Code:
    int i;
    ...
    for (i = 0; i < 100; i++)
            weight[i] += learning_rate*sign*input[i];

  4. #4
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    okok i just made it...I added loops about few seconds ago...the code is still constantly changing...
    Arduino rocks!

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
       scanf("%d", &goal);
         switch(goal){
         case 1:
           educate();
         break;
         default:
           educate();
    what's the purpose of this switch statement?

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I'm sorry Yann, but your program is broken. Completely.

    You have a hundred input nodes. What do they represent? I'm guessing it should be one set of training data such as a byte in ASCII, or a bitmap representation of a character.

    Your inputs and weights are all undefined, so I don't understand what you are trying to achieve here. Since your goal is also undefined, how are you supposed have any idea as to the fitness of anything.

    A single boolean output node can only give you a yes or no answer to something. So how can it recognise any character? You would normally have multiple output nodes outputting floating point values. And, if you want to distinguish between binary patterns representing characters, you are going to run into the linear seperability problem that I mentioned earlier.

    Also, what is your switch statement meant to do? Currently it does nothing.

    Personally, I'd suggest that you forget about neural networks for the time being and learn how to use C a bit better. Stuff like loops and data structures are pretty much essential background knowledge here. It might sound harsh, but its not an easy subject, even when you are an experienced programmer.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    what is educate(); ? T_T

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    a sry haven't seen xD

  9. #9
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    You have a hundred input nodes. What do they represent? I'm guessing it should be one set of training data such as a byte in ASCII, or a bitmap representation of a character.
    Inputs represent pixels, it is an 10x10 pixel letter(e.g. "A"):

    12345678910
    ' ' ' ' ' ' ' ' ' '
    1 - 0000110000
    2 - 0011001100
    3 - 0110000110
    4 - 0110000110
    5 - 0111111110
    6 - 0110000110
    7 - 0110000110
    8 - 0110000110
    9 - 0110000110
    10 - 0110000110


    And, there is only one output per perceptron, i don't need any more here! Why would i need more? I need one, 1 . this is letter "A", and 0 for no this isn't letter "A".

    First we input the letter, then he calculates his output, the we say do we agree or not, then he makes a correction...By this he can learn ANY symbol and adapt to ANY handwriting or handwrite, only problem is, there is one letter per neuron(perceptron), but that just makes things more interesting to work with!

    Lets say...30 perceptrons, 100 inputs, 30 outputs and we can handwrite ANYTHING on ANY language in ANY writing technique!!!

    P.S:
    My code has 60-100 lines of code less now... My original code is often very "raw" if you know what i mean(and you do, because you mention it) my only mistake here is that i posted the older version of my code, witch is now much more "stylish".
    Arduino rocks!

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

    Smile

    Here is the newer code...

    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int i;
    float weight[100];
    bool percept[100];
    int input[100];
    int goal;
    int lc;
    float threshold 0.5f;
    int successive_right = 0;
    int total_go_round = 1;
    const float learning_rate = 0.1f;
    
    
    int educate() {
                int sign = goal ? 1 : -1; //sign of (y-f(x))
            for (i = 0; i < 100; i++){
            weight[i] += learning_rate*sign*input[i];
    }
        return;
    }
    
    int main(){
       srand(time(NULL));
       scanf("%d", &lc);
       while(total_go_round <= lc){
    
    //input a letter
          percept[0] =  (weight[0]*input[0]+weight[1]*input[1]+weight[2]*input[2]+weight[3]*input[3]+weight[4]*input[4]+weight [5]*input[5]+weight[6]*input[6]+weight[7]*input[7]+weight[8]*input[8]+weight[9]*input[9]+weight[10]*input[10]+weight[11]*input[11]+weight[12]*input[12]+weight[13]*input[13]+weight[14]*input[14]+weight[15]*input[15]+weight[16]*input[16]+weight[17]*input[17]+weight[18]*input[18]+weight[19]*input[19]+weight[20]*input[20]+weight[21]*input[21]+weight[22]*input[22]+weight[23]*input[23]+weight[24]*input[24]+weight[25]*input[25]+weight[26]*input[26]+weight[27]*input[27]+weight[28]*input[28]+weight[29]*input[29]+weight[30]*input[30]+weight[31]*input[31]+weight[32]*input[32]+weight[33]*input[33]+weight[34]*input[34]+weight[35]*input[35]+weight[36]*input[36]+weight[37]*input[37]+weight[38]*input[38]+weight[39]*input[39]+weight[40]*input[40]+weight[41]*input[41]+weight[42]*input[42]+weight[43]*input[43]+weight[44]*input[44]+weight[45]*input[45]+weight[46]*input[46]+weight[47]*input[47]+weight[48]*input[48]+weight[49]*input[49]+weight[50]*input[50]+weight[51]*input[51]+weight[52]*input[52]+weight[53]*input[53]+weight[54]*input[54]+weight[55]*input[55]+weight[56]*input[56]+weight[57]*input[57]+weight[58]*input[58]+weight[59]*input[59]+weight[60]*input[60]+weight[61]*input[61]+weight[62]*input[62]+weight[63]*input[63]+weight[64]*input[64]+weight[65]*input[65]+weight[66]*input[66]+weight[67]*input[67]+weight[68]*input[68]+weight[69]*input[69]+weight[70]*input[70]+weight[71]*input[71]+weight[72]*input[72]+weight[73]*input[73]+weight[74]*input[74]+weight[75]*input[75]+weight[76]*input[76]+weight[77]*input[77]+weight[78]*input[78]+weight[79]*input[79]+weight[80]*input[80]+weight[81]*input[81]+weight[82]*input[82]+weight[83]*input[83]+weight[84]*input[84]+weight[85]*input[85]+weight[86]*input[86]+weight[87]*input[87]+weight[88]*input[88]+weight[89]*input[89]+weight[90]*input[90]+weight[91]*input[91]+weight[92]*input[92]+weight[93]*input[93]+weight[94]*input[94]+weight[95]*input[95]+weight[96]*input[96]+weight[97]*input[97]+weight[98]*input[98]+weight[99]*input[99]) > threshold;
       printf("perceptron: ");
       printf("%d\n", percept[0]);
       scanf("%d", &goal);
       educate();
    total_go_round++;
    }
    return 0;
    }
    Arduino rocks!

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    And, there is only one output per perceptron, i don't need any more here! Why would i need more? I need one, 1 . this is letter "A", and 0 for no this isn't letter "A".
    And what happens when a character is classified as a 1 and a 7? You have to somehow pick what appears to be the closest. You could always run the pattern through a sequence of perceptrons, but you would really need a way of telling which has the greatest likeness. I guess perhaps your program was never intended to do this, but then saying it can recognise any character is a bit misleading.

    Lets say...30 perceptrons, 100 inputs, 30 outputs and we can handwrite ANYTHING on ANY language in ANY writing technique!!!
    And what about linear seperability? 100 inputs linked to 30 outputs cant really do much useful. Until you deal with that your program will never work for this kind of data.

    Plus I cant think of any program that could do anything useful when all the variables are undefined.

    You still have a lot of things to do before this code will work. Good luck with it

  12. #12
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    but i don't think you understand, try it, it will work, you don't inputa character, you input sets of 0 and 1...And i sad 30 perceptrons, that is 30 letters, one perceptron just tells if input is or is not letter that he is to recognise for.
    Arduino rocks!

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    but i don't think you understand, try it, it will work, you don't inputa character, you input sets of 0 and 1..
    Trust me, I understand. Bitmap patterns such as alphabet characters are (nearly aways) not linerly seperable data. Your perceptron wont work. Did you actually pay attention to anything said in the past about linear seperability?
    And i sad 30 perceptrons, that is 30 letters, one perceptron just tells if input is or is not letter that he is to recognise for.
    I understood that too. Did you understand what I said in my last post? If you have 30 pereptrons with boolean output and 5 characters are all classified as a 'Yes' how can you tell which of those 5 is the closest match?

    If you are bent on believing your code works, then I wont argue with you anymore. I can say for definite though that it does not. Especially when all your variables are undefined. Lets just leave it at that.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mike_g View Post
    Especially when all your variables are undefined. Lets just leave it at that.
    Hint: You may want to drop that part, what with global variables always initialized to zero.

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by tabstop View Post
    Hint: You may want to drop that part, what with global variables always initialized to zero.
    Oh. I never knew that. Thanks.

    I guess maybe its partly because i spend so much time trying to avoid globals in C. Tbh, sometimes I wonder why, as my C programs never seem to end up all that big.

    Still, a load of zeros here would be equally useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about tasks! And view of code, please ^^
    By Akkernight in forum C++ Programming
    Replies: 11
    Last Post: 12-19-2008, 11:47 AM
  2. How Can I compile 3 source code in 1 program ?
    By lord_cedrich in forum C Programming
    Replies: 8
    Last Post: 12-10-2006, 05:10 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Please test my program: Code Black
    By PhoenixC++ in forum Windows Programming
    Replies: 15
    Last Post: 04-29-2004, 07:18 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM