public HierarchicalNetwork(params int[] neuronsCount)
        {
            ncnt = new int[neuronsCount.Length];
            Array.Copy(neuronsCount, ncnt, ncnt.Length);

            // neurons
            neurons = new Neuron[ncnt.Length][];
            for (var i = 0; i < ncnt.Length; ++i)
            {
                neurons[i] = new Neuron[ncnt[i]];
                for (var j = 0; j < ncnt[i]; ++j)
                {
                    if (i == 0)
                    {
                        neurons[i][j] = new Neuron(1);
                        neurons[i][j].SetWeights(1.0);
                        neurons[i][j].Threshold = 0.0;
                        neurons[i][j].TransferFunction = (v => v);
                    }
                    else
                    {
                        neurons[i][j] = new Neuron(ncnt[i - 1]);
                        for (var k = 0; k < ncnt[i - 1]; ++k)
                        {
                            neurons[i][j].Inputs[k] = neurons[i - 1][k];
                            neurons[i][j].Weights[k] = rnd.NextDouble() / 10.0;
                        }
                        neurons[i][j].Threshold = rnd.NextDouble() / 10.0;
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public Neuron(Neuron neuron)
 {
     m_inputsCount = neuron.m_inputsCount;
     m_weights = (double[])neuron.m_weights.Clone();
     m_function = neuron.m_function;
     m_threshold = neuron.m_threshold;
 }
Ejemplo n.º 3
0
 public NetworkLayer(int inputSize, int neuronCount)
 {
     layer = new Neuron[neuronCount];
     Random ran = new Random();
     for (int x = 0; x < layer.Length; x++)
         layer[x] = new Neuron(inputSize+1, ran);
 }
Ejemplo n.º 4
0
 public Layer(Layer layer)
 {
     m_inputsCount = layer.m_inputsCount;
     m_neuronsCount = layer.m_neuronsCount;
     m_function = layer.m_function;
     m_neurons = new Neuron[m_neuronsCount];
     for (int i = 0; i < m_neuronsCount; i++)
         m_neurons[i] = new Neuron(layer[i]);
     m_output = (double[])layer.m_output.Clone();
 }
Ejemplo n.º 5
0
        public Layer(int inputsCount, int neuronsCount, double[] thresholds, IActivationFunction function)
        {
            m_inputsCount = inputsCount;
            m_neuronsCount = neuronsCount;
            m_function = function;
            m_neurons = new Neuron[neuronsCount];

            for (int i = 0; i < neuronsCount; i++)
                m_neurons[i] = new Neuron(m_inputsCount, m_function, thresholds[i]);

            m_output = new double[neuronsCount];
        }
Ejemplo n.º 6
0
 public Neuron(Neuron neuron) : this()
 {
     InnovationNo = neuron.InnovationNo;
     Bias         = neuron.Bias;
 }
Ejemplo n.º 7
0
 public Synapse(Neuron input, Neuron output, int innovationno) : this(input, output)
 {
     InnovationNo = innovationno;
 }
Ejemplo n.º 8
0
 public Synapse(Neuron toneuron, double data) // input synapse for first layer
 {
     ToNeuron = toneuron; PushedData = data;
     Weight   = 1;
 }
Ejemplo n.º 9
0
 public void Activate(Neuron Activator,float ActivationAmount)
 {
     curActivationAmount = input;
    // if (curActivationAmount >= Threshold||isOutput)
    // {
         //Console.WriteLine(curActivationAmount);
         float x = isOutput ? Threshold : Threshold;
         Fire(((float)(1/(1+Math.Pow(Math.E,(-curActivationAmount-x)/1f)))) * 2 - 1);
         //curActivationAmount = 0;
         
    // }
    // else
    // {
         //Fire(0.5f);
    //     Console.WriteLine("Value " + curActivationAmount + " didn't make it against "+Threshold);
    // }
     if (NeuronRecieveEvent != null)
     {
         NeuronRecieveEvent(this, input);
     }
     //NeuronRecieveEvent(this, ActivationAmount);
   //  Console.WriteLine("Neuron Triggered: " + ActivationAmount);
 }
Ejemplo n.º 10
0
 public Dentrite(Neuron input, Neuron output)
 {
     weight       = random.NextDouble() * (0.5 - (-0.5)) + (-0.5);
     inputNeuron  = input;
     outputNeuron = output;
 }
Ejemplo n.º 11
0
 public Synapse(Neuron toNeuron, double output)
 {
     _toNeuron = toNeuron;
     OutputVal = output;
     Weight    = 1;
 }
Ejemplo n.º 12
0
 public Connection(Neuron prevNeuron, Neuron nextNeuron)
 {
     prevLayerNeuron = prevNeuron;
     nextLayerNeuron = nextNeuron;
     weight          = 2.0f * Random.NextDouble() - 1.0f;
 }
Ejemplo n.º 13
0
        public double ErrorFeedback(Neuron input)
        {
            Weight w = _weights.Find(delegate(Weight t) { return(t.Input == input); });

            return(_error * Derivative * w.Value);
        }
Ejemplo n.º 14
0
 public Connection(Neuron neuron, float weight)         //Konstruktor
 {
     this.neuron = neuron;
     this.weight = weight;
 }
Ejemplo n.º 15
0
        public void AddOutputNeuron(Neuron outputneuron)
        {
            Synapse synapse = new Synapse(this, outputneuron);

            Outputs.Add(synapse); outputneuron.Inputs.Add(synapse);
        }
Ejemplo n.º 16
0
 public Link(Neuron neuron)
 {
     Neuron = neuron;
 }
Ejemplo n.º 17
0
        // main function of the neural network to train and store the outputs using backprop & feedforward
        public void compute()
        {
            int epoch = 0; // used for iterating through tests N times

            // hidden layer of neurons
            Neuron hidden1 = new Neuron();
            Neuron hidden2 = new Neuron();
            Neuron hidden3 = new Neuron();
            Neuron hidden4 = new Neuron();

            //output neuron
            Neuron output = new Neuron();

            output.randomizeWeights();

            hidden1.randomizeWeights();
            hidden2.randomizeWeights();
            hidden3.randomizeWeights();
            hidden4.randomizeWeights();

            //double errorRate = 1;

            //output.error = 1;
            while (epoch < 100) // iterate based on the output error OR use epochs > 100 for reliable numbers
            {
                //errorRate = 0;
                epoch++;
                for (int i = 0; i < 16; i++)
                {
                    // set each neurons inputs to the training set
                    hidden1.inputs = new double[] { inputs[i, 0], inputs[i, 1], inputs[i, 2], inputs[i, 3] };
                    hidden2.inputs = new double[] { inputs[i, 0], inputs[i, 1], inputs[i, 2], inputs[i, 3] };
                    hidden3.inputs = new double[] { inputs[i, 0], inputs[i, 1], inputs[i, 2], inputs[i, 3] };
                    hidden4.inputs = new double[] { inputs[i, 0], inputs[i, 1], inputs[i, 2], inputs[i, 3] };

                    // set the outputnode inputs to the results of the 4 neurons sigmoid transfer
                    output.inputs = new double[] { hidden1.output(), hidden2.output(), hidden3.output(), hidden4.output() };

                    // display the training set and the "output of the output" neuron
                    // we set the output nodes inputs above, and now we're using those inputs * weights + bias formula in the output function
                    textBox1.AppendText(inputs[i, 0] + " " + inputs[i, 1] + " " + inputs[i, 2] + " " + inputs[i, 3] + " = " + output.output() + "\r\n");
                    outputResults.Add(output.output());
                    neuronOutputs.Add(new double[] { hidden1.output(), hidden2.output(), hidden3.output(), hidden4.output() });

                    // calculate the error
                    output.error = sigmoid.derivative(output.output()) * (answers[i] - output.output());
                    output.tweakWeights();

                    // calculate the neurons error rates based on their individual outputs * the error rate of the output neuron * the weights of the output neuron
                    // the sigmoid deriv is used for adjusting the weights in order to reduce the error rate of each neuron
                    hidden1.error = sigmoid.derivative(hidden1.output()) * output.error * output.weights[0];
                    hidden2.error = sigmoid.derivative(hidden2.output()) * output.error * output.weights[1];
                    hidden3.error = sigmoid.derivative(hidden3.output()) * output.error * output.weights[2];
                    hidden4.error = sigmoid.derivative(hidden4.output()) * output.error * output.weights[3];

                    // the line below is irrelevant since our sigmoid function takes care of the error rate
                    //double error = answers[i] - output.output();

                    // tweak the neurons weights using the error rates by adding the error rate * input value to the weights
                    hidden1.tweakWeights();
                    hidden2.tweakWeights();
                    hidden3.tweakWeights();
                    hidden4.tweakWeights();

                    //errorRate += Math.Abs(error);
                }
            }
        }
Ejemplo n.º 18
0
        public Network Copy()
        {
            /* Needed for breadth first graph traversal */
            Queue <Neuron> queue            = new Queue <Neuron>();
            Dictionary <Neuron, Neuron> map = new Dictionary <Neuron, Neuron> ();

            /* Cloned network */
            Network copyNetwork = new Network();

            /* We will start traversal from the actuator */
            Neuron originalActuator = this.actuator;

            Neuron copyActuator = new Neuron();

            copyNetwork.SetActuator(copyActuator);

            /* Add the first in line for processing, the original actuator */
            queue.Enqueue(originalActuator);
            map.Add(originalActuator, copyActuator);


            List <Neuron> copyioNodes = new List <Neuron>();
            List <Neuron> copyiNodes  = new List <Neuron>();
            List <Neuron> copyoNodes  = new List <Neuron>();
            List <Neuron> copySensors = new List <Neuron>();

            foreach (Neuron s in sensors)
            {
                Neuron temp = new Neuron();
                map.Add(s, temp);
                queue.Enqueue(s);
                copySensors.Add(temp);
                copyiNodes.Add(temp);
            }

            while (queue.Count > 0)
            {
                /* Node we are currently processing */
                Neuron originalNode = queue.Dequeue();

                List <Synapse> inputSynapses  = originalNode.GetInputs();
                List <Synapse> outputSynapses = originalNode.GetOutputs();

                /* Clone of the original node that is being processed */
                Neuron copyNode = map[originalNode];

                foreach (Synapse inputSynapse in inputSynapses)
                {
                    /* Get the real input node */
                    Neuron inputOriginal = inputSynapse.GetSource();

                    /* If the copy of the inputOriginal doesn't exist, create it. Otherwise just connect */
                    if (!map.ContainsKey(inputOriginal))
                    {
                        Neuron inputCopy = new Neuron();
                        copyioNodes.Add(inputCopy);
                        Synapse synapseCopy = new Synapse(inputCopy, copyNode, inputSynapse.getWeight());
                        inputCopy.AddOutput(synapseCopy);
                        copyNode.AddInput(synapseCopy);
                        map.Add(inputOriginal, inputCopy);
                        queue.Enqueue(inputOriginal);
                    }
                    else
                    {
                        /* Get the copy of the real node since it exists */
                        Neuron inputCopy = map[inputOriginal];

                        bool alreadyExists = false;
                        foreach (Synapse n in inputCopy.GetOutputs())
                        {
                            if (n.GetSource().Equals(inputCopy) && n.GetDestination().Equals(copyNode))
                            {
                                alreadyExists = true;
                                break;
                            }
                        }
                        if (!alreadyExists)
                        {
                            /* Create input synapse between cloned nodes */
                            Synapse copySynapse = new Synapse(inputCopy, copyNode, inputSynapse.getWeight());
                            copyNode.AddInput(copySynapse);
                            inputCopy.AddOutput(copySynapse);
                        }
                    }
                }
                foreach (Synapse outputSynapse in outputSynapses)
                {
                    /* Get the real output node */
                    Neuron outputOriginal = outputSynapse.GetDestination();

                    /* If the copy of the outputOriginal doesn't exist, create it. Otherwise just connect */
                    if (!map.ContainsKey(outputOriginal))
                    {
                        Neuron outputCopy = new Neuron();
                        copyioNodes.Add(outputCopy);
                        Synapse syn = new Synapse(copyNode, outputCopy, outputSynapse.getWeight());
                        copyNode.AddOutput(syn);
                        outputCopy.AddInput(syn);
                        map.Add(outputOriginal, outputCopy);
                        queue.Enqueue(outputOriginal);
                    }
                    else
                    {
                        /* Get the copy of the real node since it exists */
                        Neuron outputCopy = map[outputOriginal];

                        bool alreadyExists = false;
                        foreach (Synapse n in outputCopy.GetInputs())
                        {
                            if (n.GetSource().Equals(copyNode) && n.GetDestination().Equals(outputCopy))
                            {
                                alreadyExists = true;
                                break;
                            }
                        }
                        if (!alreadyExists)
                        {
                            /* Create output synapse between cloned nodes */
                            Synapse copySynapse = new Synapse(copyNode, outputCopy, outputSynapse.getWeight());
                            copyNode.AddOutput(copySynapse);
                            outputCopy.AddInput(copySynapse);
                        }
                    }
                }
            }

            /* Set newly created sensors */
            copyNetwork.SetSensors(copySensors);

            /* Within cloned network, remove sensors from ioNodes, add to iNodes*/
            //for (Neuron copySensor : copySensors) {
            //   copyioNodes.remove(copySensor);
            //   copyiNodes.add(copySensor);
            // }

            /* Add all nodes (except sensors, and actuator) to iNodes and oNodes */
            foreach (Neuron ioNode in copyioNodes)
            {
                copyiNodes.Add(ioNode);
                copyoNodes.Add(ioNode);
            }

            /* Add actuator to oNodes */
            copyoNodes.Add(copyActuator);

            copyNetwork.SetInputNodes(copyiNodes);
            copyNetwork.SetOutputNodes(copyoNodes);
            copyNetwork.SetInputOutputNodes(copyioNodes);

            // Crap code ahead ================================ WOO HOO WATCH ME ==========================================
            // TODO: Erase this souts once we create a few working networks
            //        System.out.println("Length of ioNodes " + copyioNodes.size());
            //        System.out.println("Length of iNodes  " + copyiNodes.size() + " it should be ioNodes + 3");
            //        System.out.println("Length of oNodes  " + copyoNodes.size() + " it should be ioNodes + 1");
            //        System.out.println(copyNetwork.actuator);
            //        System.out.println(copyActuator);
            //        System.out.println(copyActuator.weightedSum);
            //        for (Neuron n : copyNetwork.iNodes)
            //        {
            //            System.out.println(n.weightedSum);
            //            System.out.println(n.getOutputs());
            //            System.out.println(n.getInputs());
            //        }
            // Crap code finished ============================= WOO HOO WATCH ME ==========================================

            return(copyNetwork);
        }
Ejemplo n.º 19
0
 public void AddNeuron(Neuron neuron) => Neurons.Add(neuron);
Ejemplo n.º 20
0
 public void SetActuator(Neuron actuator)
 {
     this.actuator = actuator;
     this.oNodes.Add(actuator);
 }
Ejemplo n.º 21
0
        public static Network Decode(string input)
        {
            List<Neuron> outputs = new List<Neuron>(outputNeuronAmount);
            int outputoffset = (int)(inputNeuronAmount * HiddenLayerNeuronAmount+ (HiddenLayerNeuronAmount*2)*HiddenLayerNeuronAmount*(HiddenLayerAmount-1)+(HiddenLayerNeuronAmount+outputNeuronAmount)*HiddenLayerNeuronAmount) * 4 ;
            char[] c = input.ToCharArray();
            BitArray bits = new BitArray(input.Length);
            for(int i = 0; i < input.Length; i++)
            {
                bits[i] = c[i] == '1' ? true : false;
            }
            byte[] bytes = ConvertToByte(bits);
            
            for (int i = 0; i < outputNeuronAmount; i++)
            {
                //Console.WriteLine(bytes.Length+" " + (outputoffset));
                float threshold = BitConverter.ToSingle(new byte[] { bytes[outputoffset + i * 4], bytes[outputoffset + i * 4 + 1], bytes[outputoffset + i * 4 + 2], bytes[outputoffset + i * 4 + 3] },0) + (new Random().Next(-10, 10) / 100.0f);
                outputs.Add(new Neuron(threshold));
                //Console.WriteLine(threshold);
            }
            int hiddenlayer3output = (int)(inputNeuronAmount * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount * 2) * HiddenLayerNeuronAmount * (HiddenLayerAmount-1)) * 4;
            List<Neuron> HiddenLayer3 = new List<Neuron>(HiddenLayerNeuronAmount);
            List<List<float>> recursiveList = new List<List<float>>(HiddenLayerNeuronAmount);
            for (int i = 0; i < HiddenLayerNeuronAmount; i++)
            {
                float threshold = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount-4], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount - 3], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount - 2], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount - 1] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                //Console.WriteLine(threshold>1||threshold<-1);
                float[] NextLayerWeights = new float[outputNeuronAmount];
                for (int x = 0; x < outputNeuronAmount; x++)
                {
                    NextLayerWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + x * 4], bytes[hiddenlayer3output +i* (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount+ x * 4 + 1], bytes[hiddenlayer3output +i* (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount+ x * 4 + 2], bytes[hiddenlayer3output +i* (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount+ x * 4 + 3] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                    //Console.WriteLine(NextLayerWeights[x]);

                }
                int hiddenlayerrecursiveoffset = hiddenlayer3output + outputNeuronAmount * 4;
                float[] RecursiveWeights = new float[HiddenLayerNeuronAmount - 1];
                for (int x = 0; x < HiddenLayerNeuronAmount - 1; x++)
                {
                    RecursiveWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + x * 4 + outputNeuronAmount*4], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + x * 4 + outputNeuronAmount * 4+1], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + x * 4 + outputNeuronAmount * 4+2], bytes[hiddenlayer3output + i * (HiddenLayerNeuronAmount + outputNeuronAmount) * HiddenLayerNeuronAmount + x * 4 + (outputNeuronAmount) * 4+3] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                }
                Neuron n = new Neuron(threshold, outputs);
                for (int x = 0; x < n.TargetSynapses.Count; x++)
                {
                    n.TargetSynapses[x].Weight = NextLayerWeights[x];

                }
                recursiveList.Add(RecursiveWeights.ToList());
                HiddenLayer3.Add(n);
            }
            int q = 0;
            int z = 0;
            foreach (Neuron n in HiddenLayer3)
            {
                n.RecursiveSynapses = new List<Synapse>();
                foreach (Neuron r in HiddenLayer3)
                {
                    if (n == r) continue;
                    //Console.WriteLine(HiddenLayer3.Count);
                    n.RecursiveSynapses.Add(new Synapse(n, r, recursiveList[q][z]));
                    z++;
                }
                z = 0;
                q++;
            }
            hiddenlayer3output = (int)(inputNeuronAmount * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount * 2) * HiddenLayerNeuronAmount * (HiddenLayerAmount - 2)) * 4;
            List<Neuron> HiddenLayer2 = new List<Neuron>(HiddenLayerNeuronAmount);
            recursiveList = new List<List<float>>(HiddenLayerNeuronAmount);
            for (int i = 0; i < HiddenLayerNeuronAmount; i++)
            {
                float threshold = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + 28], bytes[hiddenlayer3output + i * 32 + 29], bytes[hiddenlayer3output + i * 32 + 30], bytes[hiddenlayer3output + i * 32 + 31] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                //Console.WriteLine(threshold);
                //Console.WriteLine(threshold > 1 || threshold < -1);
                float[] NextLayerWeights = new float[HiddenLayerNeuronAmount];
                for (int x = 0; x < HiddenLayerNeuronAmount; x++)
                {
                    NextLayerWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount*2*HiddenLayerNeuronAmount + x * 4], bytes[hiddenlayer3output + x * 4 + 1], bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4 + 2], bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4 + 3] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                   // Console.WriteLine(NextLayerWeights[x]);
                }
                int hiddenlayerrecursiveoffset = hiddenlayer3output + HiddenLayerNeuronAmount * 4;
                float[] RecursiveWeights = new float[HiddenLayerNeuronAmount - 1];
                for (int x = 0; x < HiddenLayerNeuronAmount - 1; x++)
                {
                    RecursiveWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + x * 4 + 16], bytes[hiddenlayer3output + i * 32 + x * 4 + 17], bytes[hiddenlayer3output + i * 32 + x * 4 + 18], bytes[hiddenlayer3output + i * 32 + x * 4 + 19] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                }
                Neuron n = new Neuron(threshold, HiddenLayer3);
                for (int x = 0; x < n.TargetSynapses.Count; x++)
                {
                    n.TargetSynapses[x].Weight = NextLayerWeights[x];

                }
                recursiveList.Add(RecursiveWeights.ToList());
                HiddenLayer2.Add(n);
            }
            q = 0;
            z = 0;
            foreach (Neuron n in HiddenLayer2)
            {
                n.RecursiveSynapses = new List<Synapse>();
                foreach (Neuron r in HiddenLayer2)
                {
                    if (n == r) continue;
                    //Console.WriteLine(HiddenLayer3.Count);
                    n.RecursiveSynapses.Add(new Synapse(n, r, recursiveList[q][z]));
                    z++;
                }
                z = 0;
                q++;
            }
            hiddenlayer3output = (int)(inputNeuronAmount * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount * 2) * HiddenLayerNeuronAmount * (HiddenLayerAmount - 3)) * 4;
            List<Neuron> HiddenLayer1 = new List<Neuron>(HiddenLayerNeuronAmount);
            recursiveList = new List<List<float>>(HiddenLayerNeuronAmount);
            for (int i = 0; i < HiddenLayerNeuronAmount; i++)
            {
                float threshold = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + 28], bytes[hiddenlayer3output + i * 32 + 29], bytes[hiddenlayer3output + i * 32 + 30], bytes[hiddenlayer3output + i * 32 + 31] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                //Console.WriteLine(threshold);
                //Console.WriteLine(threshold > 1 || threshold < -1);
                float[] NextLayerWeights = new float[HiddenLayerNeuronAmount];
                for (int x = 0; x < HiddenLayerNeuronAmount; x++)
                {
                    NextLayerWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output  +i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4], bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4 + 1], bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4 + 2], bytes[hiddenlayer3output + i * HiddenLayerNeuronAmount * 2 * HiddenLayerNeuronAmount + x * 4 + 3] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                    //Console.WriteLine(NextLayerWeights[x]);
                }
                int hiddenlayerrecursiveoffset = hiddenlayer3output + HiddenLayerNeuronAmount * 4;
                float[] RecursiveWeights = new float[HiddenLayerNeuronAmount - 1];
                for (int x = 0; x < HiddenLayerNeuronAmount - 1; x++)
                {
                    RecursiveWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + x * 4 + 16], bytes[hiddenlayer3output + i * 32 + x * 4 + 17], bytes[hiddenlayer3output + i * 32 + x * 4 + 18], bytes[hiddenlayer3output + i * 32 + x * 4 + 19] }, 0) + (new Random().Next(-10, 10) / 100.0f);
                }
                Neuron n = new Neuron(threshold, HiddenLayer2);
                for (int x = 0; x < n.TargetSynapses.Count; x++)
                {
                    n.TargetSynapses[x].Weight = NextLayerWeights[x];

                }
                recursiveList.Add(RecursiveWeights.ToList());
                HiddenLayer1.Add(n);
            }
            q = 0;
            z = 0;
            foreach (Neuron n in HiddenLayer1)
            {
                n.RecursiveSynapses = new List<Synapse>();
                foreach (Neuron r in HiddenLayer1)
                {
                    if (n == r) continue;
                    //Console.WriteLine(HiddenLayer3.Count);
                    n.RecursiveSynapses.Add(new Synapse(n, r, recursiveList[q][z]));
                    z++;
                }
                z = 0;
                q++;
            }
            
            hiddenlayer3output = 0;//(int)(inputNeuronAmount * HiddenLayerNeuronAmount + (HiddenLayerNeuronAmount * 2) * HiddenLayerNeuronAmount * (HiddenLayerAmount - 2)) * 4;
            List<Neuron> inputs = new List<Neuron>(inputNeuronAmount);
            //recursiveList = new List<List<float>>(HiddenLayerNeuronAmount);
            for (int i = 0; i < inputNeuronAmount; i++)
            {
                float threshold = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + 28], bytes[hiddenlayer3output + i * 32 + 29], bytes[hiddenlayer3output + i * 32 + 30], bytes[hiddenlayer3output + i * 32 + 31] }, 0) + (new Random().Next(-10, 10) / 100.0f) + (new Random().Next(-10, 10) / 100.0f);
                //Console.WriteLine(threshold);
                //Console.WriteLine(threshold > 1 || threshold < -1);
                float[] NextLayerWeights = new float[HiddenLayerNeuronAmount];
                for (int x = 0; x < HiddenLayerNeuronAmount; x++)
                {
                    NextLayerWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * inputNeuronAmount * HiddenLayerNeuronAmount + x * 4], bytes[hiddenlayer3output + i * inputNeuronAmount * HiddenLayerNeuronAmount + x * 4 + 1], bytes[hiddenlayer3output + i * inputNeuronAmount * HiddenLayerNeuronAmount + x * 4 + 2], bytes[hiddenlayer3output + i * inputNeuronAmount * HiddenLayerNeuronAmount + x * 4 + 3] }, 0)+ (new Random().Next(-10, 10) / 100.0f) + (new Random().Next(-10, 10) / 100.0f);
                    //Console.WriteLine(NextLayerWeights[x]);
                }
                //int hiddenlayerrecursiveoffset = hiddenlayer3output + outputNeuronAmount * 4;
                //float[] RecursiveWeights = new float[outputNeuronAmount - 1];
                //for (int x = 0; x < outputNeuronAmount - 1; x++)
                //{
                 //   RecursiveWeights[x] = BitConverter.ToSingle(new byte[] { bytes[hiddenlayer3output + i * 32 + x * 4 + 16], bytes[hiddenlayer3output + i * 32 + x * 4 + 17], bytes[hiddenlayer3output + i * 32 + x * 4 + 18], bytes[hiddenlayer3output + i * 32 + x * 4 + 19] }, 0);
                //}
                Neuron n = new Neuron(threshold, HiddenLayer1);
                for (int x = 0; x < n.TargetSynapses.Count; x++)
                {
                    n.TargetSynapses[x].Weight = NextLayerWeights[x];

                }
                //recursiveList.Add(RecursiveWeights.ToList());
                inputs.Add(n);
            }
            return new Network(inputNeuronAmount, HiddenLayerNeuronAmount, outputNeuronAmount) {
                Inputs = inputs,
                Outputs = outputs,
                HiddenLayer1 = HiddenLayer1,
                HiddenLayer2 = HiddenLayer2,
                HiddenLayer3 =HiddenLayer3
            
            };
        }
Ejemplo n.º 22
0
 public void addInput(Neuron n)
 {
     new Axon(n, this);
 }
Ejemplo n.º 23
0
 public Synapse(Neuron inputNeuron, Neuron outputNeuron)
 {
     InputNeuron  = inputNeuron;
     OutputNeuron = outputNeuron;
     Weight       = NeuralNet.GetRandom();
 }
Ejemplo n.º 24
0
 public void OnHiddenFire(Neuron Activator, float amount)
 {
     Console.WriteLine( " of hidden layer was activated with " + amount + " " + Activator.Threshold);
 }
Ejemplo n.º 25
0
 public Synapse(Neuron fromneuron, Neuron toneuron) // standard synapse
 {
     FromNeuron = fromneuron; ToNeuron = toneuron;
     Weight     = tmp.NextDouble() - 0.5;
 }
 public Perceptron(List <Tuple <double[], double> > data)
 {
     PerceptronNeuron = new Neuron();
     TrainingSet      = data;
     PerceptronNeuron.Initialize(data[0].Item1.Length);
 }
Ejemplo n.º 27
0
 public void OnOutputFire(Neuron Activator,float amount)
 {
     if (!isTraining)
     {
         //Console.WriteLine(Outputs.IndexOf(Activator) + " of output layer was activated with " + amount + " " + Activator.Threshold);
     }
 }
Ejemplo n.º 28
0
 public Synapse(Neuron fromneuron, Neuron toneuron) // standard synapse
 {
     FromNeuron     = fromneuron; ToNeuron = toneuron;
     Weight         = rnd.NextDouble() * (MaxInitWeight - MinInitWeight) + MinInitWeight;
     SynapsesCount += 1;
 }
Ejemplo n.º 29
0
 public void OnInputFire(Neuron Activator, float amount)
 {
     Console.WriteLine(Inputs.IndexOf(Activator) + " of input layer was activated with " + amount + " " + Activator.Threshold);
 }
Ejemplo n.º 30
0
        static void Main(string[] args)
        {
            int[] H =
            {
                1, 0, 1,
                1, 1, 1,
                1, 0, 1
            };
            int[] answers =
            {
                1, 0, 1,
                1, 1, 1,
                1, 0, 1
            };

            int[,] letters =
            {
                {
                    0, 1, 0,
                    0, 1, 0,
                    0, 1, 0
                },
                {
                    1, 1, 1,
                    1, 0, 1,
                    1, 1, 1
                },{
                    1, 0, 1,
                    1, 1, 1,
                    1, 0, 1
                },
                {
                    1, 0, 1,
                    1, 0, 1,
                    1, 1, 1
                },
                {
                    1, 1, 1,
                    1, 0, 1,
                    1, 0, 1
                }
            };


            Neuron neuron = new Neuron(H, answers);

            neuron.Train();
            Console.WriteLine("It took {0} generations", neuron.generation);
            Console.ReadKey();
            Console.Clear();
            for (int i = 0; i < letters.GetLength(0); i++)
            {
                int[] array = new int[letters.GetLength(1)];
                for (int j = 0; j < letters.GetLength(1); j++)
                {
                    array[j] = letters[i, j];
                }
                bool isH = neuron.Test(array);
                if (isH)
                {
                    Console.WriteLine("It is a H!");
                }
            }
            Console.ReadKey();
        }