private INeuron[] CreateNeurons(int neuronsCount, int inputCount, int outputCount, double bias, double excess, IActivation activation) { var neurons = new INeuron[neuronsCount]; for (var i = 0; i < _inputCount; i++) { neurons[i] = new InputNeuron { B = bias, }; } for (var i = _inputCount; i < neuronsCount - _outputCount; i++) { neurons[i] = new HiddenNeuron { Activation = activation, L = 0.5 + 0.25 * 1, B = bias, }; } for (var i = neuronsCount - _outputCount; i < neuronsCount; i++) { neurons[i] = new OutputNeuron { Activation = activation, L = 0.5 + 0.25 * 3, B = bias, }; } return(neurons); }
private void InitNeuron() { _neurons = new INeuron[Architecture.NumberOfNeurons]; for (int i = 0; i < Architecture.NumberOfNeurons; i++) { switch (Architecture.Type) { case NeuronType.Input: // Has input size of 1 _neurons[i] = new InputNeuron(); break; case NeuronType.Multiplication: _neurons[i] = new MultiplicationNeuron(Architecture.InputSize); break; case NeuronType.Normalization: _neurons[i] = new NormalizationNeuron(Architecture.InputSize); break; case NeuronType.Function: // Has input size of 1 _neurons[i] = new FunctionNeuron(Architecture.InputSize, _numberOfVariables); break; case NeuronType.Output: _neurons[i] = new OutputNeuron(Architecture.InputSize); break; default: throw new ArgumentOutOfRangeException(nameof(Architecture.Type), Architecture.Type, null); } } }
private static void CalculateOutput(OutputNeuron output) { var incomingSum = output.Incoming.Sum(i => i.From.Value * i.Weight); var withBias = incomingSum + (output.BiasSynapse.From.Value * output.BiasSynapse.Weight); output.Value = output.ActivationFunction.Activate(withBias); }
public OutputLayer(Activation activation, TrainingInfo trainInfo, int size) { var neurons = new OutputNeuron[size]; for (int i = 0; i < size; i++) neurons[i] = new OutputNeuron(activation, trainInfo); this.neurons = neurons; }
private NeuralNetwork.NeuralNetwork CreateNn() { NeuralNetwork.NeuralNetwork nn; nn = new NeuralNetwork.NeuralNetwork(); InputLayer inputLayer = nn.CreateInputLayer(); //inHeading = inputLayer.CreateNeuron("heading"); //inVelocityAngle = inputLayer.CreateNeuron("v_angle"); //inVelocityLength = inputLayer.CreateNeuron("v_length"); inNextCheckpointDistance0 = inputLayer.CreateNeuron("c_dist0"); inNextCheckpointDistance1 = inputLayer.CreateNeuron("c_dist1"); inNextCheckpointDistance2 = inputLayer.CreateNeuron("c_dist2"); inNextCheckpointDistance3 = inputLayer.CreateNeuron("c_dist3"); inNextCheckpointAngle0 = inputLayer.CreateNeuron("c_angle0"); inNextCheckpointAngle1 = inputLayer.CreateNeuron("c_angle1"); inNextCheckpointAngle2 = inputLayer.CreateNeuron("c_angle2"); inNextCheckpointAngle3 = inputLayer.CreateNeuron("c_angle3"); inNextCheckpointAngle4 = inputLayer.CreateNeuron("c_angle4"); inNextCheckpointAngle5 = inputLayer.CreateNeuron("c_angle5"); inNextNextCheckpointAngle0 = inputLayer.CreateNeuron("nnc_angle0"); inNextNextCheckpointAngle1 = inputLayer.CreateNeuron("nnc_angle1"); inNextNextCheckpointAngle2 = inputLayer.CreateNeuron("nnc_angle2"); inNextNextCheckpointAngle3 = inputLayer.CreateNeuron("nnc_angle3"); inNextNextCheckpointAngle4 = inputLayer.CreateNeuron("nnc_angle4"); inNextNextCheckpointAngle5 = inputLayer.CreateNeuron("nnc_angle5"); //inNextCheckpointDistance = inputLayer.CreateNeuron("c_dist"); OutputLayer outputLayer = nn.CreateOutputLayer(); outHeading0 = outputLayer.CreateNeuron("o_heading0"); outHeading1 = outputLayer.CreateNeuron("o_heading1"); outHeading2 = outputLayer.CreateNeuron("o_heading2"); outHeading3 = outputLayer.CreateNeuron("o_heading3"); outHeading4 = outputLayer.CreateNeuron("o_heading4"); outHeading5 = outputLayer.CreateNeuron("o_heading5"); outThrust0 = outputLayer.CreateNeuron("o_thrust0"); outThrust1 = outputLayer.CreateNeuron("o_thrust1"); outThrust2 = outputLayer.CreateNeuron("o_thrust2"); outThrust3 = outputLayer.CreateNeuron("o_thrust3"); outThrust4 = outputLayer.CreateNeuron("o_thrust4"); outThrust5 = outputLayer.CreateNeuron("o_thrust5"); outThrust6 = outputLayer.CreateNeuron("o_thrust6"); for (int i = 0; i < 3; i++) { HiddenLayer hiddenLayer = nn.CreateHiddenLayer(); for (int j = 0; j < 32; j++) { HiddenNeuron hiddenNeuron = hiddenLayer.CreateNeuron(string.Format("hidden[{0}][{1}]", i, j)); } } nn.CreateFullConnections(); nn.InitWithRandomValues(); return(nn); }
public void NeuralNetwork_Neuron_OutputNeuronIOTest() { OutputNeuron ON = new OutputNeuron(1, 1, 3); ON.InValue = new double[] { 0.20202, 0.30303, 0.40404 }; ON.Propogate(); Assert.AreEqual(ON.OutValue, 0.71281391251731641); }
public void OutputNeuron_DoesNothing() { // Arrange var outputNeuron = new OutputNeuron(); // Assert Assert.IsNotNull(outputNeuron.Connections); Assert.IsTrue(outputNeuron.Connections.Count == 0); }
/// <summary> /// /// </summary> /// <param name="_inSize"></param> /// <param name="_numHLay"></param> /// <param name="_hidSize"></param> /// <param name="_outSize"></param> public MultilayerNeuralNet(int _inSize, int _numHLay, int _hidSize, int _outSize) { InputLayerSize = _inSize; NumHiddenLayers = _numHLay; HiddenLayerSize = _hidSize; OutputLayerSize = _outSize; //Instantiate Jagged Arrays of Neurons int TotalLayers = NumHiddenLayers + 2; Neurons = new Neuron[TotalLayers][]; Neurons[0] = new InputNeuron[InputLayerSize]; for (int i = 0; i < NumHiddenLayers; i++) { Neurons[i + 1] = new HiddenNeuron[HiddenLayerSize]; } Neurons[TotalLayers - 1] = new OutputNeuron[OutputLayerSize]; //Instantiate the layers based on the inputs for (int i = 0; i < InputLayerSize; i++) { Neurons[0][i] = new InputNeuron(HiddenLayerSize); } for (int i = 0; i < NumHiddenLayers; i++) { for (int j = 0; j < HiddenLayerSize; j++) { if (i == 0) { Neurons[i + 1][j] = new HiddenNeuron(InputLayerSize, HiddenLayerSize); } if (i == (NumHiddenLayers - 1)) { Neurons[i + 1][j] = new HiddenNeuron(HiddenLayerSize, OutputLayerSize); } else { Neurons[i + 1][j] = new HiddenNeuron(HiddenLayerSize, HiddenLayerSize); } } } for (int i = 0; i < OutputLayerSize; i++) { Neurons[NumHiddenLayers + 1][i] = new OutputNeuron(HiddenLayerSize); } //Get input values }
public static void Reset() { First = new InputNeuron(); Second = new InputNeuron(); A = new HiddenNeuron(First, Second); B = new HiddenNeuron(First, Second); Output = new OutputNeuron(A, B); }
public OutputLayer(Activation activation, TrainingInfo trainInfo, int size) { var neurons = new OutputNeuron[size]; for (int i = 0; i < size; i++) { neurons[i] = new OutputNeuron(activation, trainInfo); } this.neurons = neurons; }
public OutputLayer(HiddenLayer h, NeuralNetwork neuralNetwork) { network = neuralNetwork; hiddenLayer = h; neurons = new OutputNeuron[outputNeurons]; for (int i = 0; i < outputNeurons; i++) { neurons[i] = new OutputNeuron(this); } }
public override Neuron[] CreateNeurons() { int len = info.neurons.Count; Neuron[] neurons = new Neuron[len]; for (int i = 0; i < len; i++) { NeuronInfo n = info.neurons[i]; neurons[i] = new OutputNeuron(n.activation, n.cost); } return(neurons); }
public IList <Neuron> AddOutputNeurons( int count, ActivationFunction activationFunction, IEnumerable <NeuronValueModifier> neuronValueModifiers = null) { var sampleNeuron = new OutputNeuron(-1, activationFunction) { ValueModifiers = neuronValueModifiers?.ToArray() }; return(AddNeurons(sampleNeuron, count)); }
public void OutputLayer_InstantiatedWithOutputLabels_MakesCorrectLayer() { OutputNeuron n1 = new OutputNeuron(1); OutputNeuron n2 = new OutputNeuron(2); List <Neuron> expectedList = new List <Neuron> { n1, n2 }; List <Neuron> actualList = outputLayer.Neurons; CollectionAssert.AreEqual(expectedList, actualList, new OutputNeuronListComparer()); }
public void Both_inputs_high() { var neuronA = new InputNeuron(1, 1); var neuronB = new InputNeuron(1, 1); var neuronC = new OutputNeuron(.9, 1, 0); var network = new Network(); network.Connect(neuronA, neuronC); network.Connect(neuronB, neuronC); Assert.IsTrue(network.Execute()); }
public void Both_inputs_low() { var neuronA = new InputNeuron(0, 1); var neuronB = new InputNeuron(0, 1); var neuronC = new OutputNeuron(1.5, 1, 0); var network = new Network(); network.Connect(neuronA, neuronC); network.Connect(neuronB, neuronC); Assert.IsFalse(network.Execute()); }
public void Both_inputs_different() { var neuronA = new InputNeuron(0, 1); var neuronB = new InputNeuron(1, 1); var neuronC = new OutputNeuron(.9, 1, 0); var network = new Network(); network.Connect(neuronA, neuronC); network.Connect(neuronB, neuronC); Assert.IsTrue(network.Execute()); }
public void NeuralNetwork_Neuron_SoftmaxTest1() { double[] input = { 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0 }; double[] expectedOutput = { 0.024, 0.064, 0.175, 0.475, 0.024, 0.064, 0.175 }; double[] output = OutputNeuron.Softmax(input); for (int i = 0; i < output.Length; i++) { output[i] = Math.Round(output[i], 3); Assert.AreEqual(output[i], expectedOutput[i]); } }
public void OutputLayer_WithSortOrderAndNeurons_InitializesProperty() { // Arrange var outputNeuron = new OutputNeuron(); _layer = new OutputLayer(5, new List <IOutputNeuron>() { outputNeuron }); // Assert Assert.IsTrue(_layer.Neurons.Count() == 1); Assert.IsTrue(_layer.Neurons.First() == outputNeuron); }
public void GetOutput(out float[] output) { var outputs = neurons.FindAll(x => x is OutputNeuron); float[] result = new float[outputs.Count]; for (int i = 0; i < outputs.Count; i++) { OutputNeuron output_neuron = (OutputNeuron)outputs[i]; result[i] = output_neuron.Value; } output = result; }
public void GetOutput_Condition_ReturnsCorrectOutputValue() { double[] inputValues = { 5, 2 }; NetworkStructure networkStructure = new NetworkStructure(networkSettings, weights); networkStructure.UpdateInput(inputValues); networkStructure.MakeOutput(); OutputNeuron outputNeuron = networkStructure.GetOutput(); double expectedResult = 37.5; double actualResult = outputNeuron.Output; Assert.AreEqual(expectedResult, actualResult); }
/// <summary> ///Evaluates all of the outputs of the neural network. /// </summary> /// <param name="inputs">Enumerable of all the input values</param> /// <returns>dictionary with names of outputs mapping to their values.</returns> public Dictionary <string, double> Evaluate(IEnumerable <double> inputs) { for (int i = 1; i < NumInputs; i++) { Neurons.TryGetValue(i, out Neuron inputNeuron); inputNeuron.Value = inputs.ElementAt(i - 1); } Dictionary <string, double> outputs = new Dictionary <string, double>(); for (int i = NumInputs; i < NumInputs + NumOutputs; i++) { Neurons.TryGetValue(i, out Neuron OutputNeuron); outputs.Add(outputNames[i - NumInputs], OutputNeuron.GetValue()); } return(outputs); }
private void LearningTest(bool signal, double expected) { var neuron = Substitute.For <ISender>(); neuron.Signal.Returns(signal); neuron.Index.Returns(0); var outputNeuron = new OutputNeuron( 0, new[] { 0.5d }, new[] { neuron }); outputNeuron.Learn(0.01d); outputNeuron.WeightingFactors[0].Should().Be(expected); }
public void Init(int[] layers) { var neurons = new INeuron[layers.Length][]; _inputValues = new double[layers[0]]; for (var i = 0; i < layers.Length; i++) { var layerSize = layers[i]; neurons[i] = new INeuron[layerSize]; for (var j = 0; j < layerSize; j++) { INeuron neuron; if (i == 0) { neuron = new InputNeuron(); _inputNeurons.Add(neuron); } else if (i < layers.Length - 1) { neuron = new FunctionNeuronProcess(_layerFunc); } else { neuron = new OutputNeuron(_outputFunc); _outputNeurons.Add(neuron); } neurons[i][j] = neuron; _network.AddNeuron(neuron); if (i > 0) { for (var k = 0; k < layers[i - 1]; k++) { var synapse = new Synapse(neurons[i - 1][k], neuron); synapse.Weight = 1; _synapses.Add(synapse); _network.AddSynapse(synapse); } } } } }
public PilotC(NeuralNetwork.NeuralNetwork nn) { if (nn == null) { this.nn = CreateNn(); } else { this.nn = nn; inNextCheckpointDistance0 = (InputNeuron)nn.InputLayer.Neurons[0]; inNextCheckpointDistance1 = (InputNeuron)nn.InputLayer.Neurons[1]; inNextCheckpointDistance2 = (InputNeuron)nn.InputLayer.Neurons[2]; inNextCheckpointDistance3 = (InputNeuron)nn.InputLayer.Neurons[3]; inNextCheckpointAngle0 = (InputNeuron)nn.InputLayer.Neurons[4]; inNextCheckpointAngle1 = (InputNeuron)nn.InputLayer.Neurons[5]; inNextCheckpointAngle2 = (InputNeuron)nn.InputLayer.Neurons[6]; inNextCheckpointAngle3 = (InputNeuron)nn.InputLayer.Neurons[7]; inNextCheckpointAngle4 = (InputNeuron)nn.InputLayer.Neurons[8]; inNextCheckpointAngle5 = (InputNeuron)nn.InputLayer.Neurons[9]; inNextNextCheckpointAngle0 = (InputNeuron)nn.InputLayer.Neurons[10]; inNextNextCheckpointAngle1 = (InputNeuron)nn.InputLayer.Neurons[11]; inNextNextCheckpointAngle2 = (InputNeuron)nn.InputLayer.Neurons[12]; inNextNextCheckpointAngle3 = (InputNeuron)nn.InputLayer.Neurons[13]; inNextNextCheckpointAngle4 = (InputNeuron)nn.InputLayer.Neurons[14]; inNextNextCheckpointAngle5 = (InputNeuron)nn.InputLayer.Neurons[15]; //inNextCheckpointDistance = (InputNeuron)nn.InputLayer.Neurons[1]; outHeading0 = (OutputNeuron)nn.OutputLayer.Neurons[0]; outHeading1 = (OutputNeuron)nn.OutputLayer.Neurons[1]; outHeading2 = (OutputNeuron)nn.OutputLayer.Neurons[2]; outHeading3 = (OutputNeuron)nn.OutputLayer.Neurons[3]; outHeading4 = (OutputNeuron)nn.OutputLayer.Neurons[4]; outHeading5 = (OutputNeuron)nn.OutputLayer.Neurons[5]; outThrust0 = (OutputNeuron)nn.OutputLayer.Neurons[6]; outThrust1 = (OutputNeuron)nn.OutputLayer.Neurons[7]; outThrust2 = (OutputNeuron)nn.OutputLayer.Neurons[8]; outThrust3 = (OutputNeuron)nn.OutputLayer.Neurons[9]; outThrust4 = (OutputNeuron)nn.OutputLayer.Neurons[10]; outThrust5 = (OutputNeuron)nn.OutputLayer.Neurons[11]; outThrust6 = (OutputNeuron)nn.OutputLayer.Neurons[12]; } }
/// <summary> /// /// </summary> /// <param name="_inSize"></param> /// <param name="_hidSize"></param> /// <param name="_outSize"></param> public SingleHiddenLayerNN(int _inSize, int _hidSize, int _outSize) { InputLayerSize = _inSize; HiddenLayerSize = _hidSize; OutputLayerSize = _outSize; for (int i = 0; i < InputLayerSize; i++) { int NumSynapse = HiddenLayerSize; InputNeuron InpN = new InputNeuron(NumSynapse); inputNeurons.Add(InpN); } for (int i = 0; i < HiddenLayerSize; i++) { int NumSynapse = OutputLayerSize; int NumInputSynapse = InputLayerSize; HiddenNeuron HidN = new HiddenNeuron(NumInputSynapse, NumSynapse); hiddenNeurons.Add(HidN); } for (int i = 0; i < OutputLayerSize; i++) { int NumInputSynapse = HiddenLayerSize; OutputNeuron OutN = new OutputNeuron(NumInputSynapse); outputNeurons.Add(OutN); } for (int i = 0; i < InputLayerSize; i++) { Console.WriteLine("Please input Value " + (i + 1)); inputNeurons[i]._inputValue = double.Parse(Console.ReadLine()); } NetworkForwardInput(); NetworkForwardHidden(); double[] FinalOutPut = NetworkForwardOutput(); FinalOutPutDisplay(); }
static void BasicTest() { var inputNeuron = new InputNeuron(); var calcNeuron1 = new FunctionNeuronProcess(ds => ds * 2); var calcNeuron2 = new FunctionNeuronProcess(ds => ds * 2); var outputNeuron = new OutputNeuron(ds => ds * 2); var network = new NeuronalNetwork(); network.AddNeuron(inputNeuron); network.AddNeuron(calcNeuron1); network.AddNeuron(calcNeuron2); network.AddNeuron(outputNeuron); var synapse11 = network.CreateSynapse(inputNeuron, calcNeuron1); var synapse12 = network.CreateSynapse(inputNeuron, calcNeuron2); var synapse21 = network.CreateSynapse(calcNeuron1, outputNeuron); var synapse22 = network.CreateSynapse(calcNeuron2, outputNeuron); synapse11.Weight = 2; synapse12.Weight = 2; synapse21.Weight = 2; synapse22.Weight = 2; Console.WriteLine("Write any Value"); var input = string.Empty; do { network.Reset(); Console.Write("> "); input = Console.ReadLine(); if (double.TryParse(input, out double value)) { inputNeuron.Value = value; Console.Write("Calculating.."); network.FeedForward(); Console.WriteLine("Finished. Result: " + outputNeuron.Value); } } while (input != string.Empty); }
private void NeuronTest(bool signal, double expected) { // Arrange var n1 = Substitute.For <ISender>(); n1.Signal.Returns(signal); n1.Index.Returns(0); var n2 = Substitute.For <ISender>(); n2.Signal.Returns(false); n2.Index.Returns(0); var outputNeuron = new OutputNeuron(0, new[] { FACTOR }, new[] { n1, n2 }); // Act outputNeuron.AcceptSignal(); // Assert outputNeuron.Value.Should().Be(expected); }
/* * Forward pass * Parameter - data for input */ public double forwardPass(double[] data) { for (int j = 0; j < inputLayer.Count; j++) { InputNeuron inNeuron = (InputNeuron)inputLayer[j]; inNeuron.Input = data[j]; inputLayer[j] = inNeuron; } for (int j = 0; j < hiddenLayer.Count; j++) { HiddenNeuron hidNeuron = (HiddenNeuron)hiddenLayer[j]; hidNeuron.Soma = 0.0; hiddenLayer[j] = hidNeuron; } for (int j = 0; j < outputLayer.Count; j++) { OutputNeuron outNeuron = (OutputNeuron)outputLayer[j]; outNeuron.Soma = 0.0; outputLayer[j] = outNeuron; } return(forwardPass()); }
public void Both_inputs_low() { var neuronA = new InputNeuron(0, 1); var neuronB = new InputNeuron(0, 1); var hiddenLayerA = new Neuron(1.5, -1); var hiddenLayerB = new Neuron(.5, 1); var result = new OutputNeuron(.5, 1, 0); var network = new Network(); network.Connect(neuronA, hiddenLayerA); network.Connect(neuronA, hiddenLayerB); network.Connect(neuronB, hiddenLayerA); network.Connect(neuronB, hiddenLayerB); network.Connect(hiddenLayerA, result); network.Connect(hiddenLayerB, result); Assert.IsFalse(network.Execute()); }
public void Both_inputs_different() { var neuronA = new InputNeuron(0, 1); var neuronB = new InputNeuron(1, 1); var hiddenLayerA = new Neuron(1.5, -1); var hiddenLayerB = new Neuron(.5, 1); var result = new OutputNeuron(.5, 1, 0); var network = new Network(); network.Connect(neuronA, hiddenLayerA); network.Connect(neuronA, hiddenLayerB); network.Connect(neuronB, hiddenLayerA); network.Connect(neuronB, hiddenLayerB); network.Connect(hiddenLayerA, result); network.Connect(hiddenLayerB, result); Assert.IsTrue(network.Execute()); }
public Layer(string layerType, short TransferFunction, int neuronNum) { neurons = new ArrayList(neuronNum); for (int i = 0; i < neuronNum; i++) { if (layerType == "input") { neurons.Add(new InputNeuron()); } else if (layerType == "output") { OutputNeuron n = new OutputNeuron(); n.setTransfetFuntion(TransferFunction); neurons.Add(n); } else if (layerType == "hidden") { Neuron n = new Neuron(); n.setTransfetFuntion(TransferFunction); neurons.Add(n); } } }
private void InitNetwork() { inputLayer = new List <Neuron>(); if (NeuralDefines.USE_HIDDEN_LAYER) { hiddenLayer = new List <Neuron>(); } outputLayer = new List <Neuron>(); inputLayer.Add(new InputNeuron(() => this.agentHealthInput / 100.0f)); inputLayer.Add(new InputNeuron(() => this.enemyHealthInput / 100.0f)); inputLayer.Add(new InputNeuron(() => this.enemyVisibilityInput)); if (NeuralDefines.USE_HIDDEN_LAYER) { for (int i = 0; i < NeuralDefines.HIDDEN_LAYER_SIZE; ++i) { Neuron neuron = new HiddenNeuron(NeuralDefines.SIGMOID_FUNCTION); foreach (Neuron input in inputLayer) { neuron.AddInput(input); } hiddenLayer.Add(neuron); } } foreach (OutputVariable var in Enum.GetValues(typeof(OutputVariable))) { Neuron neuron = new OutputNeuron(NeuralDefines.SIGMOID_FUNCTION); List <Neuron> previousLayer = NeuralDefines.USE_HIDDEN_LAYER ? hiddenLayer : inputLayer; foreach (Neuron input in previousLayer) { neuron.AddInput(input); } outputLayer.Add(neuron); } }