Beispiel #1
0
        /// <summary> Base constructor that sets the <paramref name="neuralNetwork"/> and <paramref name="index"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> that this <see cref="NeuronLayer"/> belongs to. </param>
        /// <param name="index"> The index of this <see cref="NeuronLayer"/> within the <see cref="NeuralNetwork"/>. </param>
        private NeuronLayer(IReadOnlyNeuralNetwork neuralNetwork, uint index)
        {
            // Set the neural network.
            NeuralNetwork = neuralNetwork ?? throw new ArgumentNullException(nameof(neuralNetwork));

            // Set the index.
            Index = index;
        }
Beispiel #2
0
        /// <summary> Creates a new <see cref="NeuronLayer"/> belonging to the given <paramref name="neuralNetwork"/> with the given <paramref name="index"/> and <see cref="Neuron"/> <paramref name="count"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> that this <see cref="NeuronLayer"/> belongs to. </param>
        /// <param name="index"> The index of this <see cref="NeuronLayer"/> within the <see cref="NeuralNetwork"/>. </param>
        /// <param name="count"> The number of <see cref="Neuron"/>s in this <see cref="NeuronLayer"/>. </param>
        /// <param name="random"> The <see cref="Random"/> object used to randomise the starting biases of the <see cref="Neuron"/>s. </param>
        public NeuronLayer(IReadOnlyNeuralNetwork neuralNetwork, uint index, int count, Random random) : this(neuralNetwork, index)
        {
            // Ensure the neuron count is not zero.
            if (count <= 0)
            {
                throw new ArgumentException("Cannot make neuron layer with no or negative neurons.");
            }

            // Initialise the neurons.
            neurons = randomiseNeurons(this, count, random);
        }
Beispiel #3
0
        /// <summary> Calculates and returns the highest number of <see cref="Neurons.Neuron"/>s in the given <paramref name="neuralNetwork"/>'s <see cref="neuronLayers"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="NeuralNetwork"/> whose <see cref="NeuronLayer"/>s to calculate from. </param>
        /// <returns> The highest number of <see cref="Neurons.Neuron"/>s in the given <paramref name="neuralNetwork"/>'s <see cref="neuronLayers"/>. </returns>
        private static int calculateHighestNeuronCount(IReadOnlyNeuralNetwork neuralNetwork)
        {
            int highestNeuronCount = 0;

            for (int l = 0; l < neuralNetwork.NeuronLayerCount; l++)
            {
                if (neuralNetwork.GetNeuronLayer(l).Count > highestNeuronCount)
                {
                    highestNeuronCount = neuralNetwork.GetNeuronLayer(l).Count;
                }
            }

            return(highestNeuronCount);
        }
Beispiel #4
0
        /// <summary> Creates, loads, links up, and returns a <see cref="WeightLayer"/> from the given <paramref name="networkLoader"/>. </summary>
        /// <param name="neuralNetwork"> The neural network for which this layer is created. </param>
        /// <param name="networkLoader"> The <see cref="INetworkLoader"/> used to load this <see cref="WeightLayer"/>. </param>
        /// <param name="index"> The index of this new <see cref="WeightLayer"/>. </param>
        /// <param name="previousNeuronLayer"> The previous <see cref="NeuronLayer"/> that this <see cref="WeightLayer"/> is linked to. </param>
        /// <param name="nextNeuronLayer"> The next <see cref="NeuronLayer"/> that this <see cref="WeightLayer"/> is linked to. </param>
        /// <returns> A new <see cref="WeightLayer"/> loaded from the given parameters. </returns>
        public static WeightLayer Load(IReadOnlyNeuralNetwork neuralNetwork, INetworkLoader networkLoader, uint index, ILinkableNeuronLayer previousNeuronLayer, ILinkableNeuronLayer nextNeuronLayer)
        {
            // Create the weight layer to return.
            WeightLayer weightLayer = new WeightLayer(neuralNetwork, index, previousNeuronLayer, nextNeuronLayer);

            // Get all defined connections from the file.
            Tuple <uint, float, uint>[] connections = networkLoader.GetAllWeightConnections(index);

            // Link the layers together based on the given connections.
            for (int i = 0; i < connections.Length; i++)
            {
                weightLayer.LinkNeurons(previousNeuronLayer[connections[i].Item1], nextNeuronLayer[connections[i].Item3], connections[i].Item2);
            }

            // Return the weight layer.
            return(weightLayer);
        }
Beispiel #5
0
        /// <summary> Creates a new <see cref="NetworkChange"/> for the given <paramref name="neuralNetwork"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> whose changes are being stored. </param>
        public NetworkChange(IReadOnlyNeuralNetwork neuralNetwork)
        {
            // Set the neural network.
            NeuralNetwork = neuralNetwork;

            // Initialise the neuron and weight changes.
            neuronLayerChanges = new NeuronLayerChange[neuralNetwork.NeuronLayerCount - 1];
            weightLayerChanges = new WeightLayerChange[neuralNetwork.WeightLayerCount];
            for (int i = 0; i < neuronLayerChanges.Length; i++)
            {
                neuronLayerChanges[i] = new NeuronLayerChange(neuralNetwork.GetNeuronLayer(i + 1));
            }
            for (int i = 0; i < weightLayerChanges.Length; i++)
            {
                weightLayerChanges[i] = new WeightLayerChange(neuralNetwork.GetWeightLayer(i));
            }
        }
Beispiel #6
0
        /// <summary> Creates a new <see cref="WeightLayer"/> for the given <paramref name="neuralNetwork"/> with the given <paramref name="index"/> and linked to the given <paramref name="nextNeuronLayer"/> and <paramref name="previousNeuronLayer"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> this <see cref="WeightLayer"/> is for. </param>
        /// <param name="index"> The index of this new <see cref="WeightLayer"/>. </param>
        /// <param name="previousNeuronLayer"> The previous <see cref="NeuronLayer"/> that this <see cref="WeightLayer"/> is linked to. </param>
        /// <param name="nextNeuronLayer"> The next <see cref="NeuronLayer"/> that this <see cref="WeightLayer"/> is linked to. </param>
        public WeightLayer(IReadOnlyNeuralNetwork neuralNetwork, uint index, ILinkableNeuronLayer previousNeuronLayer, ILinkableNeuronLayer nextNeuronLayer)
        {
            // Set the index.
            Index = index;

            // Set references.
            NeuralNetwork       = neuralNetwork;
            PreviousNeuronLayer = previousNeuronLayer;
            NextNeuronLayer     = nextNeuronLayer;

            // Link the previous and next layers to this layer.
            previousNeuronLayer.NextWeightLayer = this;
            nextNeuronLayer.PreviousWeightLayer = this;

            // Initialise dictionaries.
            previousNeuronConnectionsByID = new Dictionary <uint, Dictionary <uint, weightConnection> >(previousNeuronLayer.Count);
            nextNeuronConnectionsByID     = new Dictionary <uint, Dictionary <uint, weightConnection> >(nextNeuronLayer.Count);
        }
Beispiel #7
0
        /// <summary> Creates and loads a new <see cref="NeuronLayer"/> from the given <see cref="INetworkLoader"/>. </summary>
        /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> that this <see cref="NeuronLayer"/> is for. </param>
        /// <param name="networkLoader"> The <see cref="INetworkLoader"/> used to load the network. </param>
        /// <param name="index"> The index of the <see cref="NeuronLayer"/> within the <see cref="NeuralNetwork"/>. </param>
        /// <returns> The created and loaded <see cref="NeuronLayer"/>. </returns>
        public static NeuronLayer Load(IReadOnlyNeuralNetwork neuralNetwork, INetworkLoader networkLoader, uint index)
        {
            // The amount of neurons in the layer.
            int neuronCount = networkLoader.GetLayerNeuronCount(index);

            // Create a new empty layer.
            NeuronLayer neuronLayer = new NeuronLayer(neuralNetwork, index, neuronCount);

            // Set the neurons of the layer to the loaded neurons from the loader.
            Neuron[] newNeurons = networkLoader.LoadNeuronsFromLayerIndex(neuronLayer, index);
            for (int i = 0; i < neuronCount; i++)
            {
                neuronLayer.neurons[i] = newNeurons[i];
            }

            // Return the filled neuron layer.
            return(neuronLayer);
        }
Beispiel #8
0
 /// <summary> Creates an empty <see cref="NeuronLayer"/> belonging to the given <paramref name="neuralNetwork"/> with the given <paramref name="index"/> and <see cref="Neuron"/> <paramref name="count"/>. </summary>
 /// <param name="neuralNetwork"> The <see cref="IReadOnlyNeuralNetwork"/> that this <see cref="NeuronLayer"/> belongs to. </param>
 /// <param name="index"> The index of this <see cref="NeuronLayer"/> within the <see cref="NeuralNetwork"/>. </param>
 /// <param name="count"> The number of <see cref="Neuron"/>s in this <see cref="NeuronLayer"/>. </param>
 private NeuronLayer(IReadOnlyNeuralNetwork neuralNetwork, uint index, int count) : this(neuralNetwork, index)
 {
     // Create the neurons array with null values.
     neurons = new Neuron[count];
 }