public ArrayList Evaluate(ArrayList ANNInput) { ArrayList ANNOutput = new ArrayList(); for (int layer = 0; layer < Layers; layer++) { for (int neuron = 0; neuron < NeuronsPerLayer[layer]; neuron++) { int neuronindex = NeuronIndex(layer, neuron); if (layer == 0) { sNeuron Neuron = ((sNeuron)Neurons[neuronindex]); Neuron.Value = ((double)ANNInput[neuronindex]) + Neuron.Bias; Neuron.Output = ActivationFn(Neuron.Value, ActivationFunction[layer]); Neurons[neuronindex] = Neuron; } else { double sum = 0; for (int neuron0 = 0; neuron0 < NeuronsPerLayer[layer - 1]; neuron0++) { int n0index = NeuronIndex(layer - 1, neuron0); int weigthindex = WeigthIndex(layer - 1, neuron0, neuron); sum += ((sNeuron)Neurons[n0index]).Output * ((double)Weigths[weigthindex]); } sNeuron Neuron = ((sNeuron)Neurons[neuronindex]); Neuron.Value = sum + Neuron.Bias; Neuron.Output = ActivationFn(Neuron.Value, ActivationFunction[layer]); Neurons[neuronindex] = Neuron; if (layer == (Layers - 1)) { ANNOutput.Add(((sNeuron)Neurons[neuronindex]).Output); } } } } return(ANNOutput); }
public oANN(string pFileName) { int linenumber = 0; System.IO.TextReader tr = null; String line; ArrayList Parameters = new ArrayList(); NeuronsPerLayer = new int[5]; ActivationFunction = new int[5]; AFParameters = new int[20]; Neurons = new ArrayList(); Weigths = new ArrayList(); // how to code this differently with a FSM? try { tr = new System.IO.StreamReader(pFileName); while ((line = tr.ReadLine()) != null) { Parameters.Add(double.Parse(line)); linenumber++; } tr.Close(); for (int i = 0; i < 20; i++) { AFParameters[i] = (int)((double)Parameters[i]); } for (int i = 0; i < 5; i++) { NeuronsPerLayer[i] = (int)((double)Parameters[20 + i]); } int NumBiases = NeuronsPerLayer[0] + NeuronsPerLayer[1] + NeuronsPerLayer[2] + NeuronsPerLayer[3] + NeuronsPerLayer[4]; int NumWeigths = (NeuronsPerLayer[0] * NeuronsPerLayer[1]) + (NeuronsPerLayer[1] * NeuronsPerLayer[2]) + (NeuronsPerLayer[3] * NeuronsPerLayer[4]); Layers = 5; if (NeuronsPerLayer[4] == 0) { Layers = 4; } if (NeuronsPerLayer[3] == 0) { Layers = 3; } if (NeuronsPerLayer[2] == 0) { Layers = 2; } if (NeuronsPerLayer[1] == 0) { Layers = 1; } for (int i = 0; i < 5; i++) { ActivationFunction[i] = (int)((double)Parameters[25 + i]); } for (int i = 0; i < NumBiases; i++) { sNeuron Neuron = new sNeuron(); Neuron.Bias = (double)Parameters[30 + i]; Neuron.Value = 0; Neuron.Output = 0; Neurons.Add(Neuron); } for (int i = 0; i < NumWeigths; i++) { Weigths.Add(Parameters[30 + NumBiases + i]); } } catch (Exception exc) { linenumber = 0; Framework.Logger(99, "An error ocurred: " + exc.Message); } finally { if (tr != null) { tr.Close(); } } }