Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace NeuralNetwork
- {
- public class Network
- {
- private double[,] N0 { get; }
- private double[,] N1 { get; }
- private double[,] N2 { get; }
- private double[,] N3 { get; }
- private double[,] W01 { get; }
- private double[,] W12 { get; }
- private double[,] W23 { get; }
- public Network()
- {
- N0 = new double[10, 2];
- N0[9, 0] = 1;
- N1 = new double[5, 2];
- N1[4, 0] = 1;
- N2 = new double[4, 2];
- N2[3, 0] = 1;
- N3 = new double[2, 2];
- W01 = new double[10, 4];
- W12 = new double[5, 3];
- W23 = new double[4, 2];
- FillNumbers(W01);
- FillNumbers(W12);
- FillNumbers(W23);
- }
- public void Forwards(int[] input)
- {
- for (var i = 0; i < input.Length; i++)
- {
- N0[i, 0] = input[i];
- }
- ForwardsInternal(N0, W01, N1);
- ForwardsInternal(N1, W12, N2);
- ForwardsInternal(N2, W23, N3);
- }
- public void FindError(int[] result)
- {
- for (var i = 0; i < N3.GetLength(0); i++)
- {
- N3[i, 1] = result[i] - N3[i, 0];
- }
- FindErrorInternal(N2, W23, N3);
- FindErrorInternal(N1, W12, N2);
- }
- public void BackProp()
- {
- BackPropInternal(N2, W23, N3, 0.1);
- BackPropInternal(N1, W12, N2, 0.1);
- BackPropInternal(N0, W01, N1, 0.1);
- }
- private static void FillNumbers(double[,] array)
- {
- var row = array.GetLength(0);
- var col = array.GetLength(1);
- for (var i = 0; i < row * col; i++)
- {
- array[i / col, i % col] = 0.5;
- }
- }
- private static void BackPropInternal(double[,] inputs, double[,] weights, double[,] outputs,
- double studyDelimiter)
- {
- var sizeX = weights.GetLength(0);
- var sizeY = weights.GetLength(1);
- for (var y = 0; y < sizeY; y++)
- {
- for (var x = 0; x < sizeX; x++)
- {
- weights[x, y] = weights[x, y] + studyDelimiter * outputs[y, 1] *
- ActivationFuncDerivative(outputs[y, 0]) * inputs[x, 0];
- }
- }
- }
- private static void FindErrorInternal(double[,] inputs, double[,] weights, double[,] outputs)
- {
- var xSize = weights.GetLength(0) - 1;
- var ySize = weights.GetLength(1);
- for (var y = 0; y < xSize; y++)
- {
- inputs[y, 1] = 0;
- for (var x = 0; x < ySize; x++)
- {
- inputs[y, 1] = inputs[y, 1] + weights[y, x] * outputs[x, 1];
- }
- }
- }
- private static void ForwardsInternal(double[,] inputs, double[,] weights, double[,] outputs)
- {
- var sizeX = weights.GetLength(0);
- var sizeY = weights.GetLength(1);
- for (var y = 0; y < sizeY; y++)
- {
- outputs[y, 0] = 0;
- for (var x = 0; x < sizeX; x++)
- {
- outputs[y, 0] = outputs[y, 0] + inputs[x, 0] * weights[x, y];
- }
- outputs[y, 0] = ActivationFunc(outputs[y, 0]);
- }
- }
- private static double ActivationFunc(double value)
- {
- return 1 / (1 + Math.Exp(-1 * value));
- }
- private static double ActivationFuncDerivative(double value)
- {
- return value * (1 - value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement