Guest User

Untitled

a guest
Aug 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace NewNet
  5. {
  6.     class FNeuron
  7.     {
  8.         public bool[] friends;
  9.         public double[] w;
  10.         public double cur_sum;
  11.         public double storage;
  12.  
  13.         static Random rnd = new Random();
  14.  
  15.         public FNeuron(int netSize)
  16.         {
  17.             this.w = new double[netSize];
  18.             this.friends = new bool[netSize];
  19.             this.cur_sum = 0;
  20.         }
  21.  
  22.         public void AddLink(int j)
  23.         {
  24.             w[j] = (double)rnd.Next(0, 100) / 1000;
  25.             friends[j] = true;
  26.         }
  27.     }
  28.  
  29.     class NeuralNet
  30.     {
  31.         FNeuron[] net;
  32.         double[] delta;
  33.         int I, N, K;
  34.         int netSize;
  35.  
  36.         double speed, eps;
  37.         double feedforward_prob = 0.75;
  38.         double feedback_prob = 0.4;
  39.         double selfconn_prob = 0.3;
  40.         double new_link_prob = 0.0005;
  41.         Random rnd = new Random();
  42.  
  43.         public NeuralNet(int I, int N, int K, double speed, double eps)
  44.         {
  45.             this.I = I;
  46.             this.N = N;
  47.             this.K = K;
  48.  
  49.             this.netSize = I + N + K;      
  50.             this.net = new FNeuron[netSize];
  51.             this.speed = speed;
  52.             this.eps = eps;
  53.  
  54.             for (int i = 0; i < netSize; i++)
  55.                 net[i] = new FNeuron(netSize);
  56.             this.delta = new double[net.Length];
  57.             GenerateNet();
  58.         }
  59.  
  60.         private void GenerateNet()
  61.         {
  62.             for (int i = 0; i < I + N; i++)
  63.                 for (int j = I; j < net.Length; j++)
  64.                 {
  65.                     double p = (double)rnd.Next(0, 1000) / 1000;
  66.                     if (p <= feedforward_prob && i < j)
  67.                         net[i].AddLink(j);
  68.                 }
  69.  
  70.             for (int i = I + N + K - 1; i >= 0; i--)
  71.                 for (int j = 0; j < net.Length; j++)
  72.                 {
  73.                     double p = (double)rnd.Next(0, 1000) / 1000;
  74.                     if (p <= feedback_prob && i > j)
  75.                         net[i].AddLink(j);
  76.                 }
  77.  
  78.             for (int i = I, j = I; i < I + N; i++, j++)
  79.             {
  80.                 double p = (double)rnd.Next(0, 1000) / 1000;
  81.                 if (p <= selfconn_prob && i == j)
  82.                     net[i].AddLink(j);
  83.             }  
  84.         }
  85.  
  86.         private static double Sigmoid(double x)
  87.         {
  88.             return
  89.                 1 / (1 + Math.Pow(Math.E, -x));
  90.         }
  91.  
  92.         public double[] Compute(double[] vector)
  93.         {
  94.             for (int i = 0; i < net.Length; i++)
  95.                 net[i].cur_sum = 0;
  96.  
  97.             for (int i = 0; i < vector.Length; i++)
  98.                 net[i].cur_sum = vector[i];
  99.  
  100.             for (int i = I; i < net.Length; i++)
  101.             {
  102.                 double val = 0; double tmp = 0;
  103.                 for (int j = 0; j < net.Length; j++)
  104.                 {
  105.                     if (net[j].friends[i] && (j < i))
  106.                         val += net[j].w[i] * net[j].cur_sum;
  107.                     else if (net[j].friends[i] && (j >= i))
  108.                         tmp += net[j].w[i] * net[j].cur_sum;
  109.                 }
  110.                 net[i].cur_sum = Sigmoid(val) + net[i].storage;
  111.                 net[i].storage = tmp;
  112.             }
  113.  
  114.             double[] result = new double[K];
  115.             for (int i = 0; i < K; i++)
  116.                 result[i] = net[I + N + i].cur_sum;
  117.             return result;
  118.         }
  119.  
  120.         private void BackPropagation(double[] vector, double[] optim)
  121.         {
  122.             Compute(vector);
  123.  
  124.             for (int i = I + N; i < net.Length; i++)
  125.             {
  126.                 double yi = net[i].cur_sum;
  127.                 double val = 0;
  128.                 for (int j = 0; j < net.Length; j++)
  129.                     if (net[i].friends[j])
  130.                         val += delta[j] * net[i].w[j];
  131.  
  132.                 delta[i] = ((optim[i - I - N] - yi) + val) * yi * (1 - yi);
  133.  
  134.                 for (int j = 0; j < net.Length; j++)
  135.                     if (net[j].friends[i])
  136.                         net[j].w[i] += speed * delta[i] * net[j].cur_sum;
  137.             }
  138.  
  139.             for (int i = I + N - 1; i >= I; i--)
  140.             {
  141.                 double val = 0;
  142.                 for (int j = 0; j < net.Length; j++)
  143.                     if (net[i].friends[j])
  144.                         val += delta[j] * net[i].w[j];
  145.  
  146.                 double yi = net[i].cur_sum;
  147.                 delta[i] = val * yi * (1 - yi);
  148.  
  149.                 for (int j = 0; j < net.Length; j++)
  150.                     if (net[j].friends[i])
  151.                         net[j].w[i] += speed * delta[i] * net[j].cur_sum;
  152.             }
  153.         }
  154.  
  155.         private bool IsLearned(List<double[]> y, List<double[]> opt)
  156.         {
  157.             double sum = 0; int n = y[0].Length;
  158.             for (int q = 0; q < y.Count; q++)
  159.                 for (int i = 0; i < n; i++)
  160.                     sum += (opt[q][i] - y[q][i]) * (opt[q][i] - y[q][i]);
  161.             if (sum >= eps) return false;
  162.             return true;
  163.         }
  164.  
  165.         public int Training(List<double[]> vectors, List<double[]> opt, int maxIter, bool exit_on_epochs)
  166.         {
  167.             for (int i = 0; i < delta.Length; i++)
  168.                 delta[i] = 0;
  169.  
  170.             List<double[]> y = new List<double[]>();
  171.  
  172.             for (int i = 0; i < vectors.Count; i++)
  173.                 y.Add(Compute(vectors[i]));
  174.  
  175.             int cur_epoch = 0;
  176.             while (!IsLearned(y, opt))
  177.             {
  178.                 cur_epoch++;
  179.                 if ((cur_epoch >= maxIter) && exit_on_epochs)
  180.                     return cur_epoch;
  181.  
  182.                 for (int i = 0; i < vectors.Count; i++)
  183.                     BackPropagation(vectors[i], opt[i]);
  184.  
  185.                 for (int i = 0; i < vectors.Count; i++)
  186.                     y[i] = Compute(vectors[i]);
  187.                 Console.WriteLine(cur_epoch);
  188.  
  189.             }
  190.             New_Link(); return cur_epoch;
  191.         }
  192.  
  193.         public void New_Link()
  194.         {
  195.             double p = (double) rnd.Next(0, 10000) / 100000;
  196.             if (p <= new_link_prob)
  197.             {
  198.                 for (int i = 0; i < net.Length; i++)
  199.                     for (int j = 0; j < net.Length; j++)
  200.                         if (!net[i].friends[j])
  201.                         {
  202.                             net[i].AddLink(j);
  203.                             return;
  204.                         }
  205.             }
  206.  
  207.         }
  208.         public void PrintLinks()
  209.         {
  210.             for (int i = 0; i < netSize; i++)
  211.             {
  212.                 for (int j = 0; j < netSize; j++)
  213.                 {
  214.                     if (net[i].friends[j]) Console.Write("{0} ", 1);
  215.                     else Console.Write("{0} ", 0);
  216.                 }
  217.            
  218.                 Console.WriteLine();
  219.             }
  220.         }
  221.     }
  222.  
  223.     class Program
  224.     {
  225.         static void Print(double[] d)
  226.         {
  227.             for (int i = 0; i < d.Length; i++)
  228.                 Console.WriteLine(d[i]);
  229.         }
  230.  
  231.         static void Main(string[] args)
  232.         {
  233.             NeuralNet net = new NeuralNet(2, 10, 1, 0.3, 0.01);
  234.             List<double[]> vec = new List<double[]>();
  235.             List<double[]> res = new List<double[]>();
  236.  
  237.  
  238.             for (int i = 0; i < 10; i++)
  239.                 for (int j = 0; j < 10; j++)
  240.                 {
  241.                     vec.Add(new double[] { i, j });
  242.                     if (i == j) res.Add(new double[] { 1 });
  243.                     else res.Add(new double[] { 0 });
  244.                 }
  245.  
  246.             int a = net.Training(vec, res, 20000, false);
  247.  
  248.             for (int i = 0; i < 15; i++)
  249.                 for (int j = 0; j < 15; j++)
  250.                 {
  251.                     Console.Write(i + " " + j + " ");
  252.                     Print(net.Compute(new double[] { i, j }));
  253.                 }
  254.  
  255.  
  256.             Console.WriteLine(a);
  257.             net.PrintLinks();
  258.  
  259.         }
  260.     }
  261. }
Add Comment
Please, Sign In to add comment