Advertisement
HITOA

Example neural network stuff

Jun 7th, 2021
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1.     abstract class BaseLayer
  2.     {
  3.         public int inputSize;
  4.         public int outputSize;
  5.         public float[] weight;
  6.  
  7.         public abstract void Init();
  8.         public abstract float[] Compute(float[] input);
  9.         public abstract float Activate(float input);
  10.     }
  11.     class Layer : BaseLayer
  12.     {
  13.         public Layer(int inputSize, int outputSize)
  14.         {
  15.             this.inputSize = inputSize;
  16.             this.outputSize = outputSize;
  17.  
  18.             weight = new float[inputSize * outputSize];
  19.  
  20.             Init();
  21.         }
  22.  
  23.         public override void Init()
  24.         {
  25.             Random rnd = new Random();
  26.  
  27.             for (int i = 0; i < weight.Length; i++)
  28.             {
  29.                 weight[i] = (float)rnd.NextDouble();
  30.             }
  31.         }
  32.  
  33.         public override float[] Compute(float[] input)
  34.         {
  35.             float[] outputs = new float[outputSize];
  36.  
  37.             for (int i = 0; i < outputSize; i++)
  38.             {
  39.                 float r = 0;
  40.  
  41.                 for (int j = 0; j < inputSize; j++)
  42.                 {
  43.                     r += input[j] * weight[i * inputSize + j];
  44.                 }
  45.  
  46.                 outputs[i] = Activate(r);
  47.             }
  48.  
  49.             return outputs;
  50.         }
  51.  
  52.         public override float Activate(float input)
  53.         {
  54.             return 1 / (1 + (float)Math.Pow(Math.E, input));
  55.         }
  56.     }
  57.     class Network
  58.     {
  59.         int inputSize;
  60.         public List<BaseLayer> layers;
  61.  
  62.         public Network(int inputSize)
  63.         {
  64.             this.inputSize = inputSize;
  65.             this.layers = new List<BaseLayer>();
  66.         }
  67.  
  68.         public void AddLayer<T>(int size) where T : BaseLayer
  69.         {
  70.             if (layers.Count > 0)
  71.             {
  72.                 layers.Add((T)Activator.CreateInstance(typeof(T), new object[] { layers.Last().outputSize, size}));
  73.                 return;
  74.             }
  75.  
  76.             layers.Add((T)Activator.CreateInstance(typeof(T), new object[] { inputSize, size }));
  77.         }
  78.  
  79.         public float[] Compute(float[] input)
  80.         {
  81.             float[] output = input;
  82.  
  83.             foreach(BaseLayer layer in layers)
  84.             {
  85.                 output = layer.Compute(output);
  86.             }
  87.  
  88.             return output;
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement