private void populateNeuralNet(ArrayList Structure, float [] Weights, float [] Biases) { //short[] neurons = structure; net.initNeuralNet(Structure); float[] w = Weights; // create links int totalLinks = 0; // calculate total number of links for (int i = 0; i < Structure.Count - 1; i++) { totalLinks += ((short[, ])(Structure[i]))[0, 0] * ((short[, ])(Structure[i + 1]))[0, 0]; } // initialize WeightsCollection WeightsMatrix weights = new WeightsMatrix(totalLinks); for (int i = 0; i < totalLinks; i++) { weights.addWeight(w[i], i); } // initialize Links int linkcount = 0; for (short layerCount = 0; layerCount + 1 < Structure.Count; layerCount++) { // get adjacent layers NeuralNet.Layer layerOne = net.getLayer(layerCount); NeuralNet.Layer layerTwo = net.getLayer(layerCount + 1); // traverse nerons of second layer for (int toNeuronCount = 0; toNeuronCount < layerTwo.getNeuronCount(); toNeuronCount++) { // traverse all neurons of first layer for (int fromNeuronCount = 0; fromNeuronCount < layerOne.getNeuronCount(); fromNeuronCount++) { // create a link NeuralNet.Link link = new NeuralNet.Link(layerOne.getNeuron(fromNeuronCount), layerTwo.getNeuron(toNeuronCount)); link.setWeight(weights.getWeight(linkcount++)); } } } // biases float[] biases = Biases; // initialize neurons with biases int count = 0; for (short layerCount = 1; layerCount < net.layers.Count; layerCount++) { for (int neuronCount = 0; neuronCount < net.getLayer(layerCount).getNeuronCount(); neuronCount++) { net.getLayer(layerCount).getNeuron(neuronCount).setBias(biases[count++]); } } }
public void runNeuralNet() { lock (this) { // execute neurons for (int inputCount = 0; inputCount < getInputDataLength(); inputCount++) { float[] inputRow = getInputRow(inputCount); //new float[]{-0.73626F}; // input data to input layer neurons NeuralNet.Layer inputLayer = net.getLayer(0); // for each input value short neuronCount; for (short inputValCount = 0; inputValCount < inputRow.Length; inputValCount++) { // for each input follow the following process //set input value in everyToLink from this neuron neuronCount = inputValCount; object[] toLinks = (inputLayer.getNeuron(neuronCount).getToLinks()).ToArray(); for (int toLinkCount = 0; toLinkCount < toLinks.Length; toLinkCount++) { ((NeuralNet.Link)(toLinks[toLinkCount])).setInputValue(inputRow[inputValCount]); } // input layer neuron initialized with input } // Inputs Dissimnated // Execute next layer neurons now for (int layerCount = 1; layerCount < net.layers.Count; layerCount++) { for (neuronCount = 0; neuronCount < net.getLayer(layerCount).getNeuronCount(); neuronCount++) { net.getLayer(layerCount).getNeuron(neuronCount).execute(); } // One Neuron Executed } // One Layer Executed } // inputs are finished } // lock ends }
/// <summary> /// The main entry point for the application. /// </summary> public float[] getNNOutput(int denormType) // denormType 0 (NO Denorm) OR 1(0~1) OR 2 (-1~1) { int numOutputs = net.getLayer(net.layers.Count - 1).getNeuronCount(); float[] f = new float[numOutputs]; for (int count = 0; count < numOutputs; count++) { NeuralNet.Layer layer = net.getLayer(net.layers.Count - 1); NeuralNet.OutputNeuron o = (NeuralNet.OutputNeuron)layer.getNeuron(count); f[count] = (float)Convert.ToDecimal((o.getOutputValues())[0]); } if (denormType == 0) { return(f); } else if (denormType == 1) { return(Denormalize(f, true)); } else { return(Denormalize(f, false)); } }