コード例 #1
0
        /// <summary>
        /// Copy weights from another connection struct.
        /// </summary>
        /// <param name="nnc">Source to copy from.</param>
        public void CopyWeights(NeuralNetworkLayerConnection nnc)
        {
            float[] cb = nnc.weights;

            int i = numberOfSynapses;

            while (i-- > 0)
            {
                weights[i] = cb[i];
            }
        }
コード例 #2
0
        //breed with another layer connection data class(partner), partner must have the same # of synapses
        //takes a random selection of weights and biases from partner and replaces the a %(partPartner) of this classes weights/classes with those
        //partPartner is the % of weights and biases to use from the partner, 0 being none and 1 being all the weights/biases
        /// <summary>
        /// Breed with another connection(partner) taking a %(partPartner) of randomly selected weights.
        /// </summary>
        /// <param name="partner">Partner layer.</param>
        /// <param name="partPartner">Percent(0-1) of weights to take from partner.</param>
        public void Breed(NeuralNetworkLayerConnection partner)
        {
            int i = numberOfSynapses;

            while (i-- > 0)
            {
                //randomly mix
                float val = Utils.NextFloat01();
                weights[i] = weights[i] * val + partner.weights[i] * (1.0f - val);
            }
        }
コード例 #3
0
        /// <summary>
        /// Create new NeuralNetwork from existing(src).
        /// </summary>
        /// <param name="src">Existing NeuralNetwork to copy.</param>
        public NeuralNetwork(NeuralNetwork src)
        {
            inputLayer = new NeuralNetworkLayer(src.inputLayer);

            hiddenLayers = new NeuralNetworkLayer[src.hiddenLayers.Length];
            for (int i = 0; i < hiddenLayers.Length; i++)
            {
                hiddenLayers[i] = new NeuralNetworkLayer(src.hiddenLayers[i]);
                hiddenLayers[i].Init();
            }
            outputLayer = new NeuralNetworkLayer(src.outputLayer);
            outputLayer.Init();

            //setup layer connections
            if (hiddenLayers.Length > 0)
            {
                //hidden layer connections
                hiddenConnections          = new NeuralNetworkLayerConnection[hiddenLayers.Length];
                hiddenRecurringConnections = new NeuralNetworkLayerConnection[hiddenLayers.Length];
                maxNumberOfHiddenNeurons   = 0;
                maxNumberOfSynapses        = 0;
                for (int i = 0; i < hiddenLayers.Length; i++)
                {
                    if (i == 0)
                    {
                        hiddenConnections[0] = new NeuralNetworkLayerConnection(inputLayer, hiddenLayers[0]);
                    }
                    else
                    {
                        hiddenConnections[i] = new NeuralNetworkLayerConnection(hiddenLayers[i - 1], hiddenLayers[i]);
                    }
                    //recurrent connection for hidden layer
                    if (hiddenLayers[i].recurring)
                    {
                        hiddenRecurringConnections[i] = new NeuralNetworkLayerConnection(hiddenLayers[i], hiddenLayers[i]);
                    }
                    else
                    {
                        hiddenRecurringConnections[i] = null;
                    }
                    //calc max hidden neurons
                    if (hiddenLayers[i].numberOfNeurons > maxNumberOfHiddenNeurons)
                    {
                        maxNumberOfHiddenNeurons = hiddenLayers[i].numberOfNeurons;
                    }
                    if (hiddenConnections[i].numberOfSynapses > maxNumberOfSynapses)
                    {
                        maxNumberOfSynapses = hiddenConnections[i].numberOfSynapses;
                    }
                }

                //output connection
                outputConnection = new NeuralNetworkLayerConnection(hiddenLayers[hiddenLayers.Length - 1], outputLayer);

                if (outputConnection.numberOfSynapses > maxNumberOfSynapses)
                {
                    maxNumberOfSynapses = outputConnection.numberOfSynapses;
                }
            }
            else
            {
                maxNumberOfHiddenNeurons = 0;
                maxNumberOfSynapses      = 0;

                //direct input to output connection
                outputConnection = new NeuralNetworkLayerConnection(inputLayer, outputLayer);
                if (outputConnection.numberOfSynapses > maxNumberOfSynapses)
                {
                    maxNumberOfSynapses = outputConnection.numberOfSynapses;
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Create new struct from existing.
 /// </summary>
 /// <param name="src">Existing to copy.</param>
 public NeuralNetworkLayerConnection(NeuralNetworkLayerConnection src)
 {
     numberOfSynapses = src.numberOfSynapses;
     weights          = new float[numberOfSynapses];
 }