title: Neural network [ocr]
body: <p>I come looking for general tips about the program I'm writing now.</p>
  
  <p>The goal is:
  Use neural network program to recognize 3 letters [D,O,M] (or display "nothing is recognized" if i input anything other than those 3).</p>
  
  <p>Here's what I have so far:</p>
  
  <p>A class for my single neuron</p>
  
  <pre><code>public class neuron
  {
      double[] weights;
      public neuron()
      {
          weights = null;
      }
      public neuron(int size)
      {
          weights = new double[size + 1];
          Random r = new Random();
          for (int i = 0; i &lt;= size; i++)
          {
              weights[i] = r.NextDouble() / 5 - 0.1;
          }
      }
      public double output(double[] wej)
      {
          double s = 0.0;
          for (int i = 0; i &lt; weights.Length; i++) s += weights[i] * wej[i];
          s = 1 / (1 + Math.Exp(s));
          return s;
      }
  }
  </code></pre>
  
  <p>A class for a layer:</p>
  
  <pre><code>public class layer 
  {
      neuron[] tab;
      public layer()
      {
          tab = null;
      }
      public layer(int numNeurons, int numInputs)
      {
          tab = new neuron[numNeurons];
          for (int i = 0; i &lt; numNeurons; i++)
          {
              tab[i] = new neuron(numInputs);
          }
      }
      public double[] compute(double[] wejscia)
      {
          double[] output = new double[tab.Length + 1];
          output[0] = 1;
          for (int i = 1; i &lt;= tab.Length; i++)
          {
              output[i] = tab[i - 1].output(wejscia);
          }
          return output;
      }
  }
  </code></pre>
  
  <p>And finally a class for a network</p>
  
  <pre><code>public class network
  {
      layer[] layers = null;
      public network(int numLayers, int numInputs, int[] npl)
      {
          layers = new layer[numLayers];
          for (int i = 0; i &lt; numLayers; i++)
          {
              layers[i] = new layer(npl[i], (i == 0) ? numInputs : (npl[i - 1]));
          }
  
      }
      double[] compute(double[] inputs)
      {
          double[] output = layers[0].compute(inputs);
          for (int i = 1; i &lt; layers.Length; i++)
          {
              output = layers[i].compute(output);
  
          }
          return output;
      }
  }
  </code></pre>
  
  <p>Now for the algorythm I chose:</p>
  
  <p>I have a picture box, size 200x200, where you can draw a letter (or read one from jpg file).</p>
  
  <p>I then convert it to my first array(get the whole picture) and 2nd one(cut the non relevant background around it) like so:</p>
  
  <pre><code>Bitmap bmp2 = new Bitmap(this.pictureBox1.Image);
          int[,] binaryfrom = new int[bmp2.Width, bmp2.Height];
  
          int minrow=0, maxrow=0, mincol=0, maxcol=0;
          for (int i = 0; i &lt; bmp2.Height; i++)
          {
              for (int j = 0; j &lt; bmp2.Width; j++)
              {
                  if (bmp2.GetPixel(j, i).R == 0)
                  {
                      binaryfrom[i, j] = 1;
                      if (minrow == 0) minrow = i;
                      if (maxrow &lt; i) maxrow = i;
                      if (mincol == 0) mincol = j;
                      else if (mincol &gt; j) mincol = j;
                      if (maxcol &lt; j) maxcol = j;
                  }
                  else
                  {
                      binaryfrom[i, j] = 0;
                  }
              }
          }
  
  
          int[,] boundaries = new int[binaryfrom.GetLength(0)-minrow-(binaryfrom.GetLength(0)-(maxrow+1)),binaryfrom.GetLength(1)-mincol-(binaryfrom.GetLength(1)-(maxcol+1))];
  
          for(int i = 0; i &lt; boundaries.GetLength(0); i++)
          {
              for(int j = 0; j &lt; boundaries.GetLength(1); j++)
              {
                  boundaries[i, j] = binaryfrom[i + minrow, j + mincol];
  
              }
          }
  </code></pre>
  
  <p>And convert it to my final array of 12x8 like so (i know I could shorten this a fair bit, but wanted to have every step in different loop so I can see what went wrong easier[if anything did]):</p>
  
  <pre><code>int[,] finalnet = new int[12, 8];
  
          int k = 1;
          int l = 1;
  
          for (int i = 0; i &lt; finalnet.GetLength(0); i++)
          {
              for (int j = 0; j &lt; finalnet.GetLength(1); j++)
              {
                  finalnet[i, j] = 0;
              }
          }
  
          while (k &lt;= finalnet.GetLength(0))
              {
                  while (l &lt;= finalnet.GetLength(1))
                  {
                      for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i &lt; (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
                      {
                          for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (l - 1); j &lt; (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * l; j++)
                          {
                              if (boundaries[i, j] == 1) finalnet[k-1, l-1] = 1;
                          }
                      }
                      l++;
                  }
                  l = 1;
                  k++;
              }
          int a = boundaries.GetLength(0);
          int b = finalnet.GetLength(1);
         if((a%b) != 0){
  
              k = 1;
  
              while (k &lt;= finalnet.GetLength(1))
              {
                  for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i &lt; boundaries.GetLength(0); i++)
                  {
                      for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (k - 1); j &lt; (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * k; j++)
                      {
                          if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, k - 1] = 1;
                      }
  
                  }
                  k++;
              }
          }
  
          if (boundaries.GetLength(1) % finalnet.GetLength(1) != 0)
          {
              k = 1;
  
              while (k &lt;= finalnet.GetLength(0))
              {
                  for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i &lt; (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
                  {
                      for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j &lt; boundaries.GetLength(1); j++)
                      {
                          if (boundaries[i, j] == 1) finalnet[k - 1, finalnet.GetLength(1) - 1] = 1;
                      } 
                  }
                  k++;
              }
  
              for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i &lt; boundaries.GetLength(0); i++)
              {
                  for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j &lt; boundaries.GetLength(1); j++)
                  {
                      if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, finalnet.GetLength(1) - 1] = 1;
                  }
              }
          }
  </code></pre>
  
  <p>The result is a 12x8 (I can change it in the code to get it from some form controls) array of 0 and 1, where 1 form the rough shape of a letter you drawn.</p>
  
  <p>Now my questions are:
  Is this a correct algorythm?
  Is my function </p>
  
  <pre><code>1/(1+Math.Exp(x))
  </code></pre>
  
  <p>good one to use here?
  What should be the topology? 2 or 3 layers, and if 3, how many neurons in hidden layer? I have 96 inputs (every field of the finalnet array), so should I also take 96 neurons in the first layer? Should I have 3 neurons in the final layer or 4(to take into account the "not recognized" case), or is it not necessary?</p>
  
  <p>Thank you for your help.</p>
  
  <p>EDIT: Oh, and I forgot to add, I'm gonna train my network using Backpropagation algorythm.</p>