Advertisement
StoneHaos

myneuro1

Feb 8th, 2022
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2.  
  3. namespace neuro {
  4.     class NeuralNetwork {
  5.         private double?[][] network;
  6.         private double?[] results;
  7.         private int inputs;
  8.         private int hide;
  9.         private int outputs;
  10.         private int cnt;
  11.  
  12.         private Random rnd = new Random();
  13.         public NeuralNetwork(int inputs, int hide) {
  14.             this.inputs = inputs;
  15.             this.hide = hide;
  16.             this.outputs = 1;
  17.             int n = inputs + hide + outputs;
  18.             this.cnt = n;
  19.             network = new double?[n][];
  20.             results = new double?[n];
  21.             for (int i = 0; i < n; ++ i) {
  22.                 network[i] = new double?[n];
  23.                 for (int j = 0; j < n; ++ j) {
  24.                     network[i][j] = null;
  25.                 }
  26.             }
  27.             for (int i = 0; i < inputs; ++ i) {
  28.                 for (int j = 0; j < hide; ++ j) {
  29.                     network[i][inputs + j] = 1.0 / rnd.Next(1, 101);
  30.                 }
  31.             }
  32.             for (int i = 0; i < hide; ++ i) {
  33.                 for (int j = 0; j < outputs; ++ j) {
  34.                     network[inputs + i][inputs + hide + j] = 1.0 / rnd.Next(1, 101);
  35.                 }
  36.             }
  37.         }
  38.         public double F(double x) {
  39.             return 1.0 / (1.0 + Math.Exp(-x));
  40.         }
  41.         public void Init(double[] inputValues) {
  42.             if (inputValues.Length != inputs) throw new Exception();
  43.             for (int i = 0; i < cnt; ++ i) {
  44.                 results[i] = null;
  45.             }
  46.             for (int i = 0; i < inputs; ++ i) {
  47.                 results[i] = inputValues[i];
  48.             }
  49.         }
  50.         public double Count() {
  51.             for (int i = inputs; i < cnt; ++ i) {
  52.                 double s = 0;
  53.                 for (int j = 0; j < cnt; ++ j) {
  54.                     if (network[j][i] != null)
  55.                         s += (double)results[j] * (double)network[j][i];
  56.                 }
  57.                 results[i] = F(s);
  58.             }
  59.             double answer = (double)results[inputs + hide];
  60.             return answer;
  61.         }
  62.         private bool isOutputNeuron(int n) {
  63.             for (int i = 0; i < cnt; ++ i) {
  64.                 if (network[n][i] != null)
  65.                     return false;
  66.             }
  67.             return true;
  68.         }
  69.         private bool isInputNeuron(int n) {
  70.             for (int i = 0; i < cnt; ++ i) {
  71.                 if (network[i][n] != null)
  72.                     return false;
  73.             }
  74.             return true;
  75.         }
  76.  
  77.         public void Learn(double[] test, double testAnswer) {
  78.             double[] errors = new double[cnt];
  79.             Init(test);
  80.             for (int i = cnt - 1; i >= 0; -- i) {
  81.                 if (isOutputNeuron(i)) {
  82.                     errors[i] = testAnswer - Count();
  83.                     continue;
  84.                 }
  85.                 else if (isInputNeuron(i)) continue;
  86.  
  87.                 double s = 0;
  88.                 for (int j = 0; j < cnt; ++ j) {
  89.                     if (network[i][j] != null) {
  90.                         s += errors[j] * (double)network[i][j];
  91.                     }
  92.                 }
  93.                 errors[i] = (double)results[i] * (1 - (double)results[i]) * s;
  94.             }
  95.             for (int i = 0; i < cnt; ++ i) {
  96.                 if (isOutputNeuron(i)) continue;
  97.                 for (int j = 0; j < cnt; ++ j) {
  98.                     if (network[i][j] != null) {
  99.                         network[i][j] += (double)results[i] * errors[j] * (double)results[j] * (1 - (double)results[j]);
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement