Ejemplo n.º 1
0
        public static InstanceValues CreateRandomInstanceValues(int[] layerSizes, double[] inputs, Random random)
        {
            int nodeBiasAmount   = 0;
            int connectionAmount = 0;

            for (int i = 1; i < layerSizes.Length; i++)
            {
                nodeBiasAmount   += layerSizes[i];
                connectionAmount += layerSizes[i - 1] * layerSizes[i];
            }

            double[] nodeBiases = new double[nodeBiasAmount];
            for (int i = 0; i < nodeBiasAmount; i++)
            {
                nodeBiases[i] = random.NextDouble();
            }
            double[] connectionWeights = new double[connectionAmount];
            for (int i = 0; i < connectionAmount; i++)
            {
                connectionWeights[i] = random.NextDouble();
            }

            InstanceValues instanceValues = new InstanceValues(layerSizes, inputs, nodeBiases, connectionWeights);

            return(instanceValues);
        }
Ejemplo n.º 2
0
        public static double[] CalculateOutputs(InstanceValues instanceValues)
        {
            int[]    layerSizes        = instanceValues.layerSizes;
            double[] inputs            = instanceValues.inputs;
            double[] nodeBiases        = instanceValues.nodeBiases;
            double[] connectionWeights = instanceValues.connectionWeights;

            int nodeBiasCounter         = 0;
            int connectionWeightCounter = 0;

            double[] currentNodeValues = new double[layerSizes[0]];

            for (int i = 0; i < layerSizes[0]; i++)
            {
                currentNodeValues[i] = inputs[i];
            }

            for (int i = 1; i < layerSizes.Length; i++)
            {
                // for every layer of nodes except the input layer
                int      amountOfNewNodes = layerSizes[i];
                double[] newNodeBiases    = new double[amountOfNewNodes];
                Array.Copy(nodeBiases, nodeBiasCounter, newNodeBiases, 0, amountOfNewNodes);
                nodeBiasCounter += amountOfNewNodes;

                int      amountOfConnections           = layerSizes[i - 1] * layerSizes[i];
                double[] connectionWeightsForThisLayer = new double[amountOfConnections];
                Array.Copy(connectionWeights, connectionWeightCounter, connectionWeightsForThisLayer, 0, amountOfConnections);
                connectionWeightCounter += amountOfConnections;
                currentNodeValues        = CalculateNextLayer(currentNodeValues, newNodeBiases, connectionWeightsForThisLayer);
            }
            return(currentNodeValues);
        }
Ejemplo n.º 3
0
 public InstanceValues(InstanceValues instanceValues)
 {
     this.layerSizes        = instanceValues.layerSizes;
     this.inputs            = instanceValues.inputs;
     this.nodeBiases        = instanceValues.nodeBiases;
     this.connectionWeights = instanceValues.connectionWeights;
 }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            //test();

            int[]          layerSizes     = new int[] { 2, 3, 3, 2 };
            double[]       inputValues    = new double[] { 1d, 1d };
            InstanceValues instanceValues = InstanceValues.CreateRandomInstanceValues(layerSizes, inputValues, new Random());

            double[] outputs = Instance.CalculateOutputs(instanceValues);
            Console.WriteLine(outputs[0] + " | " + outputs[1]);

            outputs = Instance.CalculateOutputs(instanceValues.Reproduce(1, 0.2d, new Random()));
            Console.WriteLine(outputs[0] + " | " + outputs[1]);
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        public static InstanceValues CreateBaseInstanceValues(int[] layerSizes, double[] inputs, double nodeBaseBias, double connectionBaseWeight)
        {
            int nodeBiasAmount   = 0;
            int connectionAmount = 0;

            for (int i = 1; i < layerSizes.Length; i++)
            {
                nodeBiasAmount   += layerSizes[i];
                connectionAmount += layerSizes[i - 1] * layerSizes[i];
            }

            double[] nodeBiases = new double[nodeBiasAmount];
            Array.Fill <double>(nodeBiases, nodeBaseBias);
            double[] connectionWeights = new double[connectionAmount];
            Array.Fill <double>(connectionWeights, connectionBaseWeight);

            InstanceValues instanceValues = new InstanceValues(layerSizes, inputs, nodeBiases, connectionWeights);

            return(instanceValues);
        }
Ejemplo n.º 6
0
        public InstanceValues Reproduce(int WeightsToChange, double maxChange, Random random)
        {
            InstanceValues returnValues     = new InstanceValues(this);
            int            nodeAmount       = nodeBiases.Length;
            int            connectionAmount = connectionWeights.Length;

            for (int i = 0; i < WeightsToChange; i++)
            {
                int weightToChange = random.Next(nodeAmount + connectionAmount);
                if (weightToChange < nodeAmount)
                {
                    returnValues.nodeBiases[weightToChange] += (random.NextDouble() - 0.5d) * 2 * maxChange;
                }
                else
                {
                    returnValues.connectionWeights[weightToChange - nodeAmount] += (random.NextDouble() - 0.5d) * 2 * maxChange;
                }
            }
            return(returnValues);
        }
Ejemplo n.º 7
0
        private static void test()
        {
            Console.WriteLine("automatically created instancevalues with base values:");

            int[]          layerSizess     = new int[] { 2, 3, 3, 2 };
            double[]       inputValuess    = new double[] { 1d, 1d };
            InstanceValues instanceValuess = InstanceValues.CreateBaseInstanceValues(layerSizess, inputValuess, 0, 1);

            double[] outputss = Instance.CalculateOutputs(instanceValuess);
            Console.WriteLine(outputss[0] + " | " + outputss[1]);

            Console.WriteLine("automatically created instancevalues with random values:");

            int[]          layerSizes     = new int[] { 2, 3, 3, 2 };
            double[]       inputValues    = new double[] { 1d, 1d };
            InstanceValues instanceValues = InstanceValues.CreateRandomInstanceValues(layerSizes, inputValues, new Random());

            double[] outputs = Instance.CalculateOutputs(instanceValues);
            Console.WriteLine(outputs[0] + " | " + outputs[1]);

            Console.ReadKey();
        }