List<Neuron> getNeighborhoodNeurons(Tuple<int, int> neighborhoodTemplate, Tuple<int, int> position, Neuron[,] network) { List<Neuron> neighborhoodNeurons = new List<Neuron>(); for (int i = 1; i <= neighborhoodTemplate.Item1; i++) { for (int j = 1; j <= neighborhoodTemplate.Item2; j++) { if (!(position.Item2 - j < 0)) { if (!(position.Item1 - i < 0)) { neighborhoodNeurons.Add(network[position.Item1 - i, position.Item2 - j]); } if (!(position.Item1 + i >= network.GetLength(0) - 1)) { neighborhoodNeurons.Add(network[position.Item1 + i, position.Item2 - j]); } } if (!(position.Item2 + j >= network.GetLength(1) - 1)) { if (!(position.Item1 - i < 0)) { neighborhoodNeurons.Add(network[position.Item1 - i, position.Item2 + j]); } if (!(position.Item1 + i >= network.GetLength(0) - 1)) { neighborhoodNeurons.Add(network[position.Item1 + i, position.Item2 + j]); } } } if (!(position.Item1 + i >= network.GetLength(0) - 1)) { neighborhoodNeurons.Add(network[position.Item1 + i, position.Item2]); } if (!(position.Item1 - i < 0)) { neighborhoodNeurons.Add(network[position.Item1 - i, position.Item2]); } } for (int j = 1; j <= neighborhoodTemplate.Item2; j++) { if (!(position.Item2 - j < 0)) { neighborhoodNeurons.Add(network[position.Item1, position.Item2 - j]); } if (!(position.Item2 + j >= network.GetLength(1) - 1)) { neighborhoodNeurons.Add(network[position.Item1, position.Item2 + j]); } } return neighborhoodNeurons; }
public void processOutput(Tuple<int, int> neighborhoodTemplate, Tuple<int, int> position, Neuron[,] network) { double output = 0; List<Neuron> neighborhoodNeurons = getNeighborhoodNeurons(neighborhoodTemplate, position, network); output += this.input * weight[0]; output += this.bias * weight[1]; output += processATemplate(neighborhoodNeurons) * weight[2]; output += processBTemplate(neighborhoodNeurons) * weight[3]; output = 1 / (1 + Math.Exp(-output)); this.output = output; }
public void processOutputTest() { Neuron[,] testNeurons = new Neuron[5,5]; double expected; double actual; for (int i = 0; i < testNeurons.GetLength(0); i++) { for (int j = 0; j < testNeurons.GetLength(1); j++) { testNeurons[i, j] = new Neuron(1); testNeurons[i, j].setInput(1); testNeurons[i, j].setWeight(new double[] { 1, 1, 1, 1 }); } } Neuron testedNeuron = testNeurons[2, 2]; testedNeuron.processOutput(new Tuple<int, int>(1, 1), new Tuple<int, int>(2, 2), testNeurons); expected = 1 / (1 + Math.Exp(-10)); actual = testedNeuron.getOutput(); Assert.AreEqual(expected, actual); testedNeuron = testNeurons[0, 0]; testedNeuron.processOutput(new Tuple<int, int>(1, 1), new Tuple<int, int>(0, 0), testNeurons); expected = 1 / (1 + Math.Exp(-5)); actual = testedNeuron.getOutput(); Assert.AreEqual(expected, actual); testedNeuron = testNeurons[4, 4]; testedNeuron.processOutput(new Tuple<int, int>(1, 1), new Tuple<int, int>(4, 4), testNeurons); expected = 1 / (1 + Math.Exp(-5)); actual = testedNeuron.getOutput(); Assert.AreEqual(expected, actual); testedNeuron = testNeurons[0, 4]; testedNeuron.processOutput(new Tuple<int, int>(1, 1), new Tuple<int, int>(0, 4), testNeurons); expected = 1 / (1 + Math.Exp(-5)); actual = testedNeuron.getOutput(); Assert.AreEqual(expected, actual); testedNeuron = testNeurons[4, 0]; testedNeuron.processOutput(new Tuple<int, int>(1, 1), new Tuple<int, int>(4, 0), testNeurons); expected = 1 / (1 + Math.Exp(-5)); actual = testedNeuron.getOutput(); Assert.AreEqual(expected, actual); }
public NeuralNetwork(int dimensionX, int dimensionY, int templateDimensionX, int templateDimensionY, Bitmap inputImage, double bias) { this.network = new Neuron[dimensionX, dimensionY]; this.neighborhoodTemplate = new Tuple<int, int>(templateDimensionX, templateDimensionY); for (int i = 0; i < dimensionX; i++) { for (int j = 0; j < dimensionY; j++) { network[i, j] = new Neuron(bias); Color clr = inputImage.GetPixel(i, j); if (clr.R + clr.G + clr.B == 0) { network[i, j].setInput(1); } else { network[i, j].setInput(0); } } } }