Advertisement
captmicro

Untitled

Nov 21st, 2011
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define NEURON_MAX_IO 64
  4. #define MNETWORK_MAX_HIDDEN_LAYERS 8
  5.  
  6. typedef struct {
  7.     unsigned int inputs[NEURON_MAX_IO];
  8.     float input_weights[NEURON_MAX_IO];
  9.     unsigned int input_count;
  10.     float threshold;
  11.     unsigned int output;
  12.    
  13.     unsigned int learn;
  14.     unsigned int learn_input;
  15. } NEURON;
  16.  
  17. typedef struct {
  18.     NEURON *neurons;
  19.     unsigned int width;
  20.     unsigned int height;
  21. } LAYER;
  22.  
  23. typedef struct {
  24.     LAYER input;
  25.     LAYER hidden[8];
  26.     unsigned int hidden_count;
  27.     LAYER output;
  28. } NEURALNETWORK;
  29.  
  30. void SimulateNeuron(NEURON *n)
  31. {
  32.     float total = 0.0f;
  33.     unsigned int i = 0;
  34.     for (i = 0; i < n->input_count; i++)
  35.         totaly = n->inputs[i] * n->input_weights[i];
  36.    
  37.     if (total > n->threshold) n->output = 1;
  38.     else n->output = 0;
  39.    
  40.     if ((learn > 0) && (output != learn_input))
  41.     {
  42.         //delta rule
  43.         float Lr = 0.01f; //learning rate
  44.         float Wi = 0.0f; //weight of ith input
  45.         for (i = 0; i < n->input_count; i++)
  46.             n->input_weights[i] += Lr * (learn_input - n->output) * n->inputs[i];
  47.     }
  48. }
  49.  
  50. int main()
  51. {
  52.     unsigned int tst =
  53.     {
  54.         1,1,1,
  55.         0,1,0,
  56.         0,1,0,
  57.     };
  58.     unsigned int imt =
  59.     {
  60.         1,1,1,
  61.         0,1,0,
  62.         0,1,0,
  63.     };
  64.     unsigned int imh =
  65.     {
  66.         1,0,1,
  67.         1,1,1,
  68.         1,0,1,
  69.     };
  70.     unsigned int out =
  71.     {
  72.         0,
  73.         0,
  74.         0,
  75.     }
  76.     unsigned int t =
  77.     {
  78.         1,
  79.         1,
  80.         1,
  81.     }
  82.     unsigned int h =
  83.     {
  84.         0,
  85.         0,
  86.         0,
  87.     }
  88.    
  89.     NEURON out0;
  90.     out0.input_count = 3;
  91.     out0.inputs[0] = 0;
  92.     out0.inputs[1] = 0;
  93.     out0.inputs[2] = 0;
  94.     out0.threshold = 1.0f;
  95.     out0.input_weights[0] = 0.5f;
  96.     out0.input_weights[1] = 0.5f;
  97.     out0.input_weights[2] = 0.5f;
  98.    
  99.     NEURON out1;
  100.     out1.input_count = 3;
  101.     out1.inputs[0] = 0;
  102.     out1.inputs[1] = 0;
  103.     out1.inputs[2] = 0;
  104.     out1.threshold = 1.0f;
  105.     out1.input_weights[0] = 0.5f;
  106.     out1.input_weights[1] = 0.5f;
  107.     out1.input_weights[2] = 0.5f;
  108.    
  109.     NEURON out2;
  110.     out2.input_count = 3;
  111.     out2.inputs[0] = 0;
  112.     out2.inputs[1] = 0;
  113.     out2.inputs[2] = 0;
  114.     out2.threshold = 1.0f;
  115.     out2.input_weights[0] = 0.5f;
  116.     out2.input_weights[1] = 0.5f;
  117.     out2.input_weights[2] = 0.5f;
  118.    
  119.     //learn
  120.     out0.learn = 1;
  121.     out1.learn = 1;
  122.     out2.learn = 1;
  123.     //train for T
  124.     out0.inputs[0] = imt[0];
  125.     out0.inputs[1] = imt[1];
  126.     out0.inputs[2] = imt[2];
  127.     out1.inputs[0] = imt[3];
  128.     out1.inputs[1] = imt[4];
  129.     out1.inputs[2] = imt[5];
  130.     out2.inputs[0] = imt[6];
  131.     out2.inputs[1] = imt[7];
  132.     out2.inputs[2] = imt[8];
  133.     out0.learn_input = t[0];
  134.     out1.learn_input = t[1];
  135.     out2.learn_input = t[2];
  136.     unsigned int tj = 100;
  137.     for (; tj > 0; tj--) {
  138.         SimulateNeuron(&out0);
  139.         SimulateNeuron(&out1);
  140.         SimulateNeuron(&out2);
  141.     }
  142.     //train for H
  143.     out0.inputs[0] = imh[0];
  144.     out0.inputs[1] = imh[1];
  145.     out0.inputs[2] = imh[2];
  146.     out1.inputs[0] = imh[3];
  147.     out1.inputs[1] = imh[4];
  148.     out1.inputs[2] = imh[5];
  149.     out2.inputs[0] = imh[6];
  150.     out2.inputs[1] = imh[7];
  151.     out2.inputs[2] = imh[8];
  152.     out0.learn_input = h[0];
  153.     out1.learn_input = h[1];
  154.     out2.learn_input = h[2];
  155.     unsigned int hj = 100;
  156.     for (; hj > 0; hj--) {
  157.         SimulateNeuron(&out0);
  158.         SimulateNeuron(&out1);
  159.         SimulateNeuron(&out2);
  160.     }
  161.     //stop learning
  162.     out0.learn = 0;
  163.     out1.learn = 0;
  164.     out2.learn = 0;
  165.    
  166.     //try to detect what img is
  167.     out0.inputs[0] = img[0];
  168.     out0.inputs[1] = img[1];
  169.     out0.inputs[2] = img[2];
  170.     out1.inputs[0] = img[3];
  171.     out1.inputs[1] = img[4];
  172.     out1.inputs[2] = img[5];
  173.     out2.inputs[0] = img[6];
  174.     out2.inputs[1] = img[7];
  175.     out2.inputs[2] = img[8];
  176.     unsigned int j = 100;
  177.     unsigned int cnt[2] = {0, 0};
  178.     for (; j > 0; j--) {
  179.         SimulateNeuron(&out0);
  180.         SimulateNeuron(&out1);
  181.         SimulateNeuron(&out2);
  182.         if (out0.output == h[0] && out1.output == h[1] && out2.output == h[2])
  183.             cnt[0] = cnt[0] + 1;
  184.         else if (out0.output == t[0] && out1.output == t[1] && out2.output == t[2])
  185.             cnt[1] = cnt[1] + 1;
  186.     }
  187.    
  188.     printf("Detected H %d times.\nDetected T %d times.\n", cnt[0], cnt[1]);
  189.     return 0;
  190. }
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement