MatteoTosato87

AND M-Adeline example with my C# AI framework

Dec 22nd, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. ---------- Very simple neural network in 'AND' configuration ------------------
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. using aneuro32.Nn.Structure.FeedForward;
  9. using aneuro32.Functions;
  10. using aneuro32.Learning.Supervised.Propagation;
  11. using aneuro32.Misc;
  12.  
  13. namespace M_Adeline_testApp
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             double[][] input =
  20.             {
  21.                 new double[] {0,0},
  22.                 new double[] {0,1},
  23.                 new double[] {1,0},
  24.                 new double[] {1,1}
  25.             };
  26.             double[][] target =
  27.             {
  28.                 new double[] {0},
  29.                 new double[] {0},
  30.                 new double[] {0},
  31.                 new double[] {1}
  32.             };
  33.  
  34.             // Build M-Adeline network, a very simple neural network configuration
  35.            
  36.             MAdeline MyNet = new MAdeline(2);
  37.            
  38.             // Add neuron threshold, important where input values are zero
  39.            
  40.             MyNet.AddBiasWeight();
  41.            
  42.             // Connects neurons, initilize all
  43.            
  44.             MyNet.Build();
  45.            
  46.             // Instantius training algorithm
  47.            
  48.             WindrowHoff trainingAlg = new WindrowHoff(
  49.                 ref MyNet,
  50.                 input,
  51.                 target,
  52.                 activation_mode.sigmoid,
  53.                 0.001,
  54.                 0.5
  55.                 );
  56.             bool retVal;
  57.  
  58.             // Training
  59.            
  60.             do
  61.             {
  62.                 retVal = trainingAlg.iteration();
  63.                 if (retVal != true)
  64.                 {
  65.                     Console.WriteLine("Training error: " + trainingAlg.NetworkError);
  66.                     break;
  67.                 }
  68.             } while (!trainingAlg.isTrained);
  69.            
  70.             // Testing
  71.            
  72.             int j = 0;
  73.             Console.WriteLine("Net test:");
  74.             foreach (double[] _in in input)
  75.             {
  76.                 MyNet.SetInput(input[j]);
  77.                 MyNet.Exec();
  78.                 Console.WriteLine("\nPattern n° "+j);
  79.                 Console.WriteLine("Input: " + input[j][0] + " " + input[j][1]);
  80.                 Console.WriteLine("Output: " + MyNet.output.OutputValue.ToString("0.00") + " Ideal: " + target[j][0]);
  81.                 j++;
  82.             }
  83.             Console.WriteLine("\nNet final error: "+trainingAlg.CurrentMeanSquareError.ToString("0.######"));
  84.             Console.WriteLine("Total epoch: "+trainingAlg.CurrentEpoch);
  85.             List<double> weights = MyNet.SaveWeights();
  86.            
  87.             // Display weights:
  88.            
  89.             Console.WriteLine("\nNet final weights: ");
  90.             foreach (double d in weights)
  91.             {
  92.                 Console.WriteLine(d.ToString("0.######"));
  93.             }
  94.             Console.ReadLine();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment