Guest User

Untitled

a guest
Apr 21st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace MPApplication
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         //// Data dependent settings ////
  20.         const int numInputs = 238;
  21.         const int numOutputs = 6;
  22.         const int numPatterns = 1;
  23.         const int numHidden = 20;
  24.         const int numEpochs = 100;
  25.  
  26.         //// User defineable settings ////
  27.         const double LR_IH = 0.7;
  28.         const double LR_HO = 0.07;
  29.  
  30.         private void Go_Click(object sender, EventArgs e)
  31.         {
  32.             // initiate the weights
  33.             initWeights();
  34.  
  35.             // load in the data
  36.             initData();
  37.  
  38.             // train the network
  39.             for (int j = 0; j <= numEpochs; j++)
  40.             {
  41.                 /*
  42.                 richTextBox1.Text += "\n";
  43.                 for (int k = 0; k < 20; k++)
  44.                 {
  45.                     richTextBox1.Text += ", " + weightsIH[0, k];
  46.                 }
  47.                 richTextBox1.Text += "\n";
  48.                 */
  49.  
  50.                 for (int i = 0; i < numPatterns; i++)
  51.                 {
  52.                     //select a pattern at random
  53.                     //patNum = randNum.Next();
  54.                     patNum = i;
  55.                    
  56.                     //calculate the current network output and error for this pattern
  57.                     calcNet();
  58.  
  59.                     //change network weights
  60.                     WeightChangesHO();
  61.                     WeightChangesIH();
  62.                 }
  63.                 //display the overall network error after each epoch
  64.                 calcOverallError();
  65.                 //printf("epoch = %d RMS Error = %f\n",j,RMSerror);
  66.                 //if(j % 50 == 0)
  67.                
  68.                 this.richTextBox1.Text +="\r\nepo = " + j.ToString() + " RMS error = " + RMSerror.ToString();
  69.                 /*this.richTextBox1.Text += "\r\n";
  70.                 for (int i = 0; i < numHidden; i++)
  71.                 {
  72.                     this.richTextBox1.Text += hiddenVal[i] + ", ";
  73.                 }*/
  74.  
  75.  
  76.                 if (RMSerror < 0.0001)
  77.                     break;
  78.             }
  79.  
  80.             //training has finished //display the results
  81.             displayResults();
  82.         }
  83.  
  84.         /*
  85.          * Q - 111110 - 62
  86.          * R - 101110 - 46
  87.          * S - 011010 - 26
  88.          * T - 011110 - 30
  89.          * U - 100011 - 35
  90.          * V - 101011 - 43
  91.          * W - 011101 - 29
  92.          * X - 110011 - 51
  93.          * 3 - 110000 - 48
  94.          * 6 - 111000 - 56
  95.          */
  96.  
  97.         string[] letters = { "Q", "R", "S", "T", "U", "V", "w", "X", "3", "6" };
  98.         //double[] brailChars = { 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00 };
  99.         //double[] brailChars = { 0.62, 0.46, 0.26, 0.30, 0.35, 0.43, 0.29, 0.51, 0.48, 0.56 };
  100.         //int[] brailChars = { 62, 46, 26, 30, 35, 43, 29, 51, 48, 56 };
  101.         double[,] brailChars = {    {1.0, 1.0, 1.0, 1.0, 1.0, 0.0}, {1.0, 0.0, 1.0, 1.0, 1.0, 0.0},
  102.                                     {0.0, 1.0, 1.0, 0.0, 1.0, 0.0}, {0.0, 1.0, 1.0, 1.0, 1.0, 0.0},
  103.                                     {1.0,0.0, 0.0, 0.0, 1.0, 1.0,}, {1.0, 0.0, 1.0, 0.0, 1.0, 1.0},
  104.                                     {0.0, 1.0, 1.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 0.0, 1.0, 1.0},
  105.                                     {1.0, 1.0, 0.0, 0.0, 0.0, 0.0}, {1.0, 1.0, 1.0, 0.0, 0.0, 0.0}
  106.                                };
  107.  
  108.         //// variables ////
  109.         int patNum = 0;
  110.         double[] errThisPat = new double[numOutputs];
  111.         double[] errThisPatFi = new double[numOutputs];
  112.         double RMSerror = 0.0;
  113.         double[] outPred = new double[numOutputs];
  114.  
  115.         // the outputs of the hidden neurons
  116.         double[] hiddenVal = new double[numHidden];
  117.         double[] outVal = new double[numOutputs];
  118.  
  119.         // the weights
  120.         double[,] weightsIH = new double[numInputs, numHidden];
  121.         double[,] weightsHO = new double[numHidden, numOutputs];
  122.  
  123.         // the data
  124.         int[,] trainInputs = new int[numPatterns, numInputs];
  125.         double[,] trainOutput = new double[numPatterns, numOutputs];
  126.         Random randNum = new Random();
  127.  
  128.         //***********************************
  129.         // calculates the network output
  130.         private void calcNet()
  131.         {
  132.             //calculate the outputs of the hidden neurons
  133.             //the hidden neurons are tanh
  134.             int i = 0;
  135.             for (i = 0; i < numHidden; i++)
  136.             {
  137.                 hiddenVal[i] = 0.0;
  138.                 for (int j = 0; j < numInputs; j++)
  139.                 {
  140.                     hiddenVal[i] = hiddenVal[i] + (trainInputs[patNum, j] * weightsIH[j, i]);
  141.                 }
  142.                 hiddenVal[i] = Math.Tanh(hiddenVal[i]);
  143.                 //  hiddenVal[i] = logistic(hiddenVal[i]);
  144.             }
  145.  
  146.             //calculate the output of the network
  147.             //the output neuron is linear
  148.             for (int j = 0; j < numOutputs; j++)
  149.             {
  150.                 outPred[j] = 0.0;
  151.                 for (i = 0; i < numHidden; i++)
  152.                 {
  153.                     outPred[j] = outPred[j] + hiddenVal[i] * weightsHO[i,j];
  154.                 }
  155.                 //calculate the error
  156.                 errThisPat[j] =  trainOutput[patNum, j] - outPred[j];
  157.                 errThisPatFi[j] = outPred[j] * (1.0 - outPred[j]) * (trainOutput[patNum, j] - outPred[j]);
  158.             }
  159.         }
  160.  
  161.         //************************************
  162.         //adjust the weights hidden-output
  163.         private void WeightChangesHO()
  164.         {
  165.             for (int j = 0; j < numOutputs; j++)
  166.             {
  167.                 for (int k = 0; k < numHidden; k++)
  168.                 {
  169.                     //double weightChange = LR_HO * errThisPat[j] * hiddenVal[k];
  170.                     double weightChange = LR_HO * errThisPatFi[j];
  171.                     weightsHO[k,j] = weightsHO[k,j] + weightChange;
  172.                     //regularisation on the output weights
  173.                     if (weightsHO[k,j] < -5)
  174.                     {
  175.                         weightsHO[k,j] = -5;
  176.                     }
  177.                     else if (weightsHO[k,j] > 5)
  178.                     {
  179.                         weightsHO[k,j] = 5;
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.  
  185.         //************************************
  186.         // adjust the weights input-hidden
  187.         void WeightChangesIH()
  188.         {
  189.             for (int j = 0; j < numOutputs; j++)
  190.             {
  191.                 for (int i = 0; i < numHidden; i++)
  192.                 {
  193.  
  194.                     for (int k = 0; k < numInputs; k++)
  195.                     {
  196.                         //double x = 1 - (hiddenVal[i] * hiddenVal[i]);
  197.                         double weightChange = weightsHO[i, j] * errThisPatFi[j] * hiddenVal[i] * (1.0 - hiddenVal[i]);
  198.                         weightChange *= trainInputs[patNum, k] * LR_IH;
  199.                         weightsIH[k, i] += weightChange;
  200.                     }
  201.                 }
  202.             }
  203.         }
  204.  
  205.         //************************************
  206.         // set weights to random numbers
  207.         void initWeights()
  208.         {
  209.             for (int j = 0; j < numHidden; j++)
  210.             {
  211.                 for (int k = 0; k < numOutputs; k++)
  212.                 {
  213.                     weightsHO[j, k] = (randNum.NextDouble() - 0.5) / 2;
  214.                     //weightsHO[j, k] = randNum.NextDouble();
  215.                     //if (randNum.NextDouble() > 0.5)
  216.                     ///    weightsHO[j, k] = -weightsHO[j, k];
  217.                 }
  218.  
  219.                 for (int i = 0; i < numInputs; i++)
  220.                 {
  221.                     weightsIH[i, j] = (randNum.NextDouble() - 0.5) / 5;
  222.                     //weightsIH[i, j] = randNum.NextDouble();
  223.                     //if (randNum.NextDouble() > 0.5)
  224.                     //    weightsIH[i, j] = -weightsIH[i, j];
  225.                     //printf("Weight = %f\n", weightsIH[i, j]);
  226.                     //this.richTextBox1.Text +="\r\nWeight = " + weightsIH[i, j].ToString();
  227.                 }
  228.             }
  229.         }
  230.  
  231.         //************************************
  232.         // read in the data
  233.         void initData()
  234.         {
  235.             //printf("initialising data\n");
  236.             //this.richTextBox1.Text +="\r\ninitialising data";
  237.             // the data here is the XOR data
  238.             // it has been rescaled to the range [-1, 1]
  239.             // an extra input valued 1 is also added to act as the bias
  240.             // the output must lie in the range -1 to 1
  241.  
  242.             for (int i = 0; i < numPatterns; i++)
  243.             {
  244.                 string extension = ".bmp";
  245.                 string path = "res/";
  246.                 LetterImage l = new LetterImage(path + letters[i] + extension);
  247.  
  248.                 int[] vec = l.getPixelVector();
  249.                 for (int j = 0; j < numInputs; j++ )
  250.                     trainInputs[i, j] = vec[j];
  251.                 for (int j = 0; j < numOutputs; j++)
  252.                 {
  253.                     trainOutput[i,j] = brailChars[i,j];
  254.                 }
  255.             }
  256.         }
  257.  
  258.         //************************************
  259.         // display results
  260.         void displayResults()
  261.         {
  262.             for (int i = 0; i < numPatterns; i++)
  263.             {
  264.                 patNum = i;
  265.                 calcNet();
  266.                 //printf("pat = %d actual = %d neural model = %f\n",patNum+1,trainOutput[patNum],outPred);
  267.                 this.richTextBox1.Text += "\r\npat = " + (patNum + 1) + " actual = ";
  268.  
  269.                 String finalText = "";
  270.                 for (int j = 0; j < numOutputs; j++)
  271.                 {
  272.                     this.richTextBox1.Text += trainOutput[patNum, j].ToString() + ", ";
  273.                     finalText += outPred[j].ToString() + ", ";
  274.                 }
  275.                 this.richTextBox1.Text += " neural model = " + finalText;
  276.             }
  277.         }
  278.  
  279.         //************************************
  280.         // calculate the overall error
  281.         void calcOverallError()
  282.         {
  283.             RMSerror = 0.0;
  284.             for (int j = 0; j < numOutputs; j++)
  285.             {
  286.                 for (int i = 0; i < numPatterns; i++)
  287.                 {
  288.                     patNum = i;
  289.                     calcNet();
  290.                     RMSerror = RMSerror + (errThisPat[j] * errThisPat[j]);
  291.                 }
  292.             }
  293.             RMSerror = RMSerror / numPatterns;
  294.             RMSerror = Math.Sqrt(RMSerror);
  295.         }
  296.     }
  297. }
Add Comment
Please, Sign In to add comment