public void gradientWeights()
        {
            const int inSize        = 3;
            const int outSize       = 2;
            const int countExamples = 1;

            var input = MatrixD.Build.DenseOfArray(
                new double[countExamples, inSize] {
                { 1, 2, 3 }
            });

            var nextGradient = MatrixD.Build.DenseOfArray(
                new double[countExamples, outSize] {
                { 4, 5 }
            });

            var weights = MatrixD.Build.DenseOfArray(
                new double[inSize, outSize] {
                { -1, 2 },
                { -3, 2 },
                { -1, 3 },
            });

            var expectedGradient = MatrixD.Build.DenseOfArray(
                new double[inSize, outSize] {
                { 4, 5 },
                { 8, 10 },
                { 12, 15 },
            });

            var layer    = new WeightLayer(inSize, outSize);
            var gradient = layer.gradientWeights(input, nextGradient);

            Assert.IsTrue(gradient.EEquals(expectedGradient));
        }
        public void forward()
        {
            const int inSize        = 3;
            const int outSize       = 2;
            const int countExamples = 1;

            var input = MatrixD.Build.DenseOfArray(
                new double[countExamples, inSize] {
                { 1, 2, 3 }
            });

            var weights = MatrixD.Build.DenseOfArray(
                new double[inSize, outSize] {
                { -1, 2 },
                { -3, 2 },
                { -1, 3 },
            });

            var expectedOutput = MatrixD.Build.DenseOfArray(
                new double[countExamples, outSize] {
                { -10, 15 }
            });

            var layer  = new WeightLayer(weights);
            var output = layer.forward(input);

            Assert.IsTrue(output.EEquals(expectedOutput));
        }
Esempio n. 3
0
        /// <summary> Creates, loads, and returns a <see cref="NeuralNetwork"/> using the given <paramref name="networkLoader"/>. </summary>
        /// <param name="networkLoader"> The <see cref="INetworkLoader"/> used to load the network. </param>
        /// <returns> The loaded <see cref="NeuralNetwork"/>. </returns>
        public static NeuralNetwork Load(INetworkLoader networkLoader)
        {
            // Get the number of neuron layers in the file.
            int neuronLayerCount = networkLoader.GetNetworkNeuronLayerCount();

            // Create an empty neural network.
            NeuralNetwork neuralNetwork = new NeuralNetwork(networkLoader.GetNetworkLearningRate(), neuronLayerCount);

            // Create the neuron layers for the neural network.
            for (uint i = 0; i < neuronLayerCount; i++)
            {
                neuralNetwork.neuronLayers[i] = NeuronLayer.Load(neuralNetwork, networkLoader, i);
            }

            // Create the weight layers for the neural network.
            for (uint i = 0; i < neuronLayerCount - 1; i++)
            {
                neuralNetwork.weightLayers[i] = WeightLayer.Load(neuralNetwork, networkLoader, i, neuralNetwork.neuronLayers[i], neuralNetwork.neuronLayers[i + 1]);
            }

            // Set the highest neuron count.
            neuralNetwork.HighestNeuronCount = calculateHighestNeuronCount(neuralNetwork);

            // Return the neural network.
            return(neuralNetwork);
        }
Esempio n. 4
0
        /// <summary> Creates and initialises each <see cref="NeuronLayer"/> in the <see cref="neuronLayers"/>. </summary>
        /// <param name="random"> The <see cref="Random"/> object used to randomise the weights and biases of the network. </param>
        /// <param name="layerSizes"> The desired sizes of each <see cref="NeuronLayer"/>. </param>
        private void initialiseLayers(Random random, int[] layerSizes)
        {
            // Create the neuron layers.
            for (uint i = 0; i < layerSizes.Length; i++)
            {
                neuronLayers[i] = new NeuronLayer(this, i, layerSizes[i], random);
            }

            // Create the weight layers. Note that the input and output layers only have one weight layer.
            for (uint i = 0; i < layerSizes.Length - 1; i++)
            {
                // Create the weight layer.
                weightLayers[i] = new WeightLayer(this, i, neuronLayers[i], neuronLayers[i + 1]);

                // Link the layers.
                weightLayers[i].LinkAllNeurons(random);
            }
        }
        public void backwardLearn()
        {
            const double learnRate     = 0.5;
            const int    inSize        = 3;
            const int    outSize       = 2;
            const int    countExamples = 1;

            var input = MatrixD.Build.DenseOfArray(
                new double[countExamples, inSize] {
                { 1, 2, 3 }
            });

            var nextGradient = MatrixD.Build.DenseOfArray(
                new double[countExamples, outSize] {
                { 4, 5 }
            });

            var weights = MatrixD.Build.DenseOfArray(
                new double[inSize, outSize] {
                { -1, 2 },
                { -3, 2 },
                { -1, 3 },
            });

            var expectedWeights = MatrixD.Build.DenseOfArray(
                new double[inSize, outSize] {
                { -3, -0.5 },
                { -7, -3 },
                { -7, -4.5 },
            });

            var layer = new WeightLayer(weights);

            layer.backwardLearn(input, nextGradient, learnRate);
            Assert.IsTrue(expectedWeights.EEquals(layer.Weights));
        }