Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace neuro {
- class NeuralNetwork {
- private double?[][] network;
- private double?[] results;
- private int inputs;
- private int[] hides;
- private int cnthides;
- private int outputs;
- private int cnt;
- private Random rnd = new Random();
- public NeuralNetwork(int inputs, int outputs, params int[] hides) {
- this.inputs = inputs;
- this.hides = hides;
- this.outputs = outputs;
- int cnthides = 0;
- for (int i = 0; i < hides.Length; ++ i) {
- cnthides += hides[i];
- }
- int n = inputs + cnthides + outputs;
- this.cnt = n;
- this.cnthides = cnthides;
- network = new double?[n][];
- results = new double?[n];
- for (int i = 0; i < n; ++ i) {
- network[i] = new double?[n];
- for (int j = 0; j < n; ++ j) {
- network[i][j] = null;
- }
- }
- if (hides.Length == 0) {
- for (int i = 0; i < inputs; ++ i) {
- for (int j = inputs; j < cnt; ++ j) {
- network[i][j] = random();
- }
- }
- }
- else {
- for (int i = 0; i < inputs; ++ i) {
- for (int j = 0; j < hides[0]; ++ j) {
- network[i][inputs + j] = random();
- }
- }
- int a = inputs;
- int b = inputs + hides[0];
- int t = inputs + hides[0];
- for (int i = 1; i < hides.Length; ++ i) {
- for (int k = 0; k < hides[i]; ++ k) {
- for (int j = a; j < b; ++ j) {
- network[j][t] = random();
- }
- ++t;
- }
- a = b;
- b += hides[i];
- }
- for (int k = 0; k < outputs; ++ k) {
- for (int j = a; j < b; ++ j) {
- network[j][t] = random();
- }
- ++t;
- }
- }
- }
- public double random() {
- return 1.0 / rnd.Next(1, 101);
- }
- public double F(double x) {
- return 1.0 / (1.0 + Math.Exp(-x));
- }
- public void Init(double[] inputValues) {
- if (inputValues.Length != inputs) throw new Exception();
- for (int i = 0; i < cnt; ++ i) {
- results[i] = null;
- }
- for (int i = 0; i < inputs; ++ i) {
- results[i] = inputValues[i];
- }
- }
- public double[] Count() {
- for (int i = inputs; i < cnt; ++ i) {
- double s = 0;
- for (int j = 0; j < cnt; ++ j) {
- if (network[j][i] != null)
- s += (double)results[j] * (double)network[j][i];
- }
- results[i] = F(s);
- }
- double[] answers = new double[outputs];
- int t = inputs + cnthides;
- for (int i = 0; i < outputs; ++ i) {
- answers[i] = (double)results[t];
- ++ t;
- }
- return answers;
- }
- public double[] Count(double[] inputValues) {
- Init(inputValues);
- return Count();
- }
- private bool isOutputNeuron(int n) {
- for (int i = 0; i < cnt; ++ i) {
- if (network[n][i] != null)
- return false;
- }
- return true;
- }
- private bool isInputNeuron(int n) {
- for (int i = 0; i < cnt; ++ i) {
- if (network[i][n] != null)
- return false;
- }
- return true;
- }
- public void Learn(double[] test, double[] testAnswer) {
- double[] errors = new double[cnt];
- Init(test);
- for (int i = cnt - 1; i >= 0; -- i) {
- if (isOutputNeuron(i)) {
- double[] res = Count();
- errors[i] = testAnswer[i - (inputs + cnthides)] - res[i - (inputs + cnthides)];
- continue;
- }
- else if (isInputNeuron(i)) continue;
- double s = 0;
- for (int j = 0; j < cnt; ++ j) {
- if (network[i][j] != null) {
- s += errors[j] * (double)network[i][j];
- }
- }
- errors[i] = (double)results[i] * (1 - (double)results[i]) * s;
- }
- for (int i = 0; i < cnt; ++ i) {
- if (isOutputNeuron(i)) continue;
- for (int j = 0; j < cnt; ++ j) {
- if (network[i][j] != null) {
- network[i][j] += (double)results[i] * errors[j] * (double)results[j] * (1 - (double)results[j]);
- //network[i][j] += (double)results[i] * errors[j] * 0.85;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment