Пример #1
0
        /// <summary>
        /// Constructs a new neural network.
        /// </summary>
        /// <param name="numberOfInputs">Number of inputs.</param>
        /// <param name="layerSizes">
        /// Number of neurons in each hidden layer.
        /// The last layer is the output layer.
        /// </param>
        /// <param name="activationFunctions">Activation functions for every layer.</param>
        /// <exception cref="ArgumentException"></exception>
        public NeuralNetwork(int numberOfInputs, int[] layerSizes, ActivationFunction[] activationFunctions)
        {
            if (numberOfInputs < 1)
            {
                throw new ArgumentException("Number of input neurons less than one!");
            }

            if (layerSizes.Length < 1)
            {
                throw new ArgumentException("Number of layers less than one!");
            }

            if (activationFunctions.Length != layerSizes.Length)
            {
                throw new ArgumentException("Number of activation functions doesn't match that of layers");
            }

            foreach (var layerSize in layerSizes)
            {
                if (layerSize < 1)
                {
                    throw new ArgumentException("Number of neurons in layer less than one!");
                }
            }

            _numberOfInputs = numberOfInputs;
            _layerSizes     = new int[layerSizes.Length];
            layerSizes.CopyTo(_layerSizes, 0);
            _activationFunctions = new ActivationFunction[activationFunctions.Length];
            for (int i = 0; i < activationFunctions.Length; i++)
            {
                _activationFunctions[i] = new ActivationFunction(activationFunctions[i]);
            }
            _weights    = new Matrix[layerSizes.Length];
            _weights[0] = new Matrix(numberOfInputs, layerSizes[0]);
            for (int i = 1; i < layerSizes.Length; i++)
            {
                _weights[i] = new Matrix(layerSizes[i - 1], layerSizes[i]);
            }
            _activityA            = new Matrix[layerSizes.Length];
            _activityZ            = new Matrix[layerSizes.Length];
            _keepTrainingFunction = DefaultKeepTraining;
            _errorFunction        = DefaultError;
        }
Пример #2
0
 /// <summary>
 /// Constructs a new neural network based on a given one.
 /// </summary>
 /// <param name="input">The given neural network.</param>
 public NeuralNetwork(NeuralNetwork input)
 {
     _numberOfInputs = input._numberOfInputs;
     _layerSizes     = new int[input._layerSizes.Length];
     input._layerSizes.CopyTo(_layerSizes, 0);
     _activationFunctions = new ActivationFunction[input._activationFunctions.Length];
     for (int i = 0; i < input._activationFunctions.Length; i++)
     {
         _activationFunctions[i] = new ActivationFunction(input._activationFunctions[i]);
     }
     _weights = new Matrix[input._layerSizes.Length];
     for (int i = 0; i < input._activationFunctions.Length; i++)
     {
         _weights[i] = new Matrix(input._weights[i]);
     }
     _activityA            = new Matrix[input._layerSizes.Length];
     _activityZ            = new Matrix[input._layerSizes.Length];
     _keepTrainingFunction = input._keepTrainingFunction;
     _errorFunction        = input._errorFunction;
 }