Advertisement
Guest User

NeuralNetwork

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2.  
  3. namespace NeuralNetwork
  4. {
  5.     public class Network
  6.     {
  7.         private double[,] N0 { get; }
  8.         private double[,] N1 { get; }
  9.         private double[,] N2 { get; }
  10.         private double[,] N3 { get; }
  11.         private double[,] W01 { get; }
  12.         private double[,] W12 { get; }
  13.         private double[,] W23 { get; }
  14.  
  15.         public Network()
  16.         {
  17.             N0 = new double[10, 2];
  18.             N0[9, 0] = 1;
  19.             N1 = new double[5, 2];
  20.             N1[4, 0] = 1;
  21.             N2 = new double[4, 2];
  22.             N2[3, 0] = 1;
  23.             N3 = new double[2, 2];
  24.             W01 = new double[10, 4];
  25.             W12 = new double[5, 3];
  26.             W23 = new double[4, 2];
  27.             FillNumbers(W01);
  28.             FillNumbers(W12);
  29.             FillNumbers(W23);
  30.         }
  31.  
  32.         public void Forwards(int[] input)
  33.         {
  34.             for (var i = 0; i < input.Length; i++)
  35.             {
  36.                 N0[i, 0] = input[i];
  37.             }
  38.  
  39.             ForwardsInternal(N0, W01, N1);
  40.             ForwardsInternal(N1, W12, N2);
  41.             ForwardsInternal(N2, W23, N3);
  42.         }
  43.  
  44.         public void FindError(int[] result)
  45.         {
  46.             for (var i = 0; i < N3.GetLength(0); i++)
  47.             {
  48.                 N3[i, 1] = result[i] - N3[i, 0];
  49.             }
  50.  
  51.             FindErrorInternal(N2, W23, N3);
  52.             FindErrorInternal(N1, W12, N2);
  53.         }
  54.  
  55.         public void BackProp()
  56.         {
  57.             BackPropInternal(N2, W23, N3, 0.1);
  58.             BackPropInternal(N1, W12, N2, 0.1);
  59.             BackPropInternal(N0, W01, N1, 0.1);
  60.         }
  61.  
  62.         private static void FillNumbers(double[,] array)
  63.         {
  64.             var row = array.GetLength(0);
  65.             var col = array.GetLength(1);
  66.             for (var i = 0; i < row * col; i++)
  67.             {
  68.                 array[i / col, i % col] = 0.5;
  69.             }
  70.         }
  71.  
  72.         private static void BackPropInternal(double[,] inputs, double[,] weights, double[,] outputs,
  73.             double studyDelimiter)
  74.         {
  75.             var sizeX = weights.GetLength(0);
  76.             var sizeY = weights.GetLength(1);
  77.             for (var y = 0; y < sizeY; y++)
  78.             {
  79.                 for (var x = 0; x < sizeX; x++)
  80.                 {
  81.                     weights[x, y] = weights[x, y] + studyDelimiter * outputs[y, 1] *
  82.                                     ActivationFuncDerivative(outputs[y, 0]) * inputs[x, 0];
  83.                 }
  84.             }
  85.         }
  86.  
  87.         private static void FindErrorInternal(double[,] inputs, double[,] weights, double[,] outputs)
  88.         {
  89.             var xSize = weights.GetLength(0) - 1;
  90.             var ySize = weights.GetLength(1);
  91.             for (var y = 0; y < xSize; y++)
  92.             {
  93.                 inputs[y, 1] = 0;
  94.                 for (var x = 0; x < ySize; x++)
  95.                 {
  96.                     inputs[y, 1] = inputs[y, 1] + weights[y, x] * outputs[x, 1];
  97.                 }
  98.             }
  99.         }
  100.  
  101.         private static void ForwardsInternal(double[,] inputs, double[,] weights, double[,] outputs)
  102.         {
  103.             var sizeX = weights.GetLength(0);
  104.             var sizeY = weights.GetLength(1);
  105.             for (var y = 0; y < sizeY; y++)
  106.             {
  107.                 outputs[y, 0] = 0;
  108.                 for (var x = 0; x < sizeX; x++)
  109.                 {
  110.                     outputs[y, 0] = outputs[y, 0] + inputs[x, 0] * weights[x, y];
  111.                 }
  112.  
  113.                 outputs[y, 0] = ActivationFunc(outputs[y, 0]);
  114.             }
  115.         }
  116.  
  117.         private static double ActivationFunc(double value)
  118.         {
  119.             return 1 / (1 + Math.Exp(-1 * value));
  120.         }
  121.  
  122.         private static double ActivationFuncDerivative(double value)
  123.         {
  124.             return value * (1 - value);
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement