Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. class Net
  2.     {
  3.         private Layer[] layers;
  4.  
  5.         public int InputCount { get; }
  6.  
  7.         public int OutputCount { get; }
  8.  
  9.         public Net(int inputCount, params Layer[] layers)
  10.         {
  11.             InputCount = inputCount;
  12.             int prevCount = inputCount;
  13.             this.layers = layers;
  14.             foreach (var layer in layers)
  15.             {
  16.                 layer.Init(prevCount);
  17.                 prevCount = layer.NeuronCount;
  18.             }
  19.             OutputCount = prevCount;
  20.  
  21.             Random rnd = new Random();
  22.             foreach (var layer in layers)
  23.             {
  24.                 for (int i = 0; i < layer.NeuronCount; i++)
  25.                 {
  26.                     for (int j = 0; j < layer.PrevLayerNeuronCount; j++)
  27.                     {
  28.                         layer.Weights[i, j] = rnd.NextDouble();
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.  
  34.         public double[] Forward(double[] input)
  35.         {
  36.             if (input.Length != InputCount)
  37.                 throw new ArgumentException("неверное кол-во входных параметров", nameof(input));
  38.  
  39.             double[] output = input;
  40.             foreach (var layer in layers)
  41.             {
  42.                 output = layer.Forward(output);
  43.             }
  44.             return output;
  45.         }
  46.  
  47.         public double[] Backward(double[] output)
  48.         {
  49.             if (output.Length != OutputCount)
  50.                 throw new ArgumentException("неверное кол-во входных параметров", nameof(output));
  51.  
  52.             double[] input = output;
  53.             foreach (var layer in layers.Reverse())
  54.             {
  55.                 input = layer.Forward(input);
  56.             }
  57.             return input;
  58.         }
  59.  
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement