Example #1
0
        public static double[,] GetWeights(
            [ExcelArgument(Description = "Name of neural network for which to get weights.")] string name,
            [ExcelArgument(Description = "Neural network layer number, starting with 1.")] int layer)
        {
            var perceptron = ObjectStore.Get <INeuralNetwork>(name);

            if (layer > perceptron.Weights.Length || layer <= 0)
            {
                throw new NNXException($"Layer for neural network {name} must be between " +
                                       $"1 and {perceptron.Weights.Length}; was {layer}.");
            }

            var result = perceptron.Weights[layer - 1].ToVertical2DArray();

            ResizeOutputToArray(result);
            return(result);
        }
Example #2
0
        public static string TrainMultilayerPerceptron(
            [ExcelArgument(Description = "Name of perceptron object to create.")] string name,
            [ExcelArgument(Description = "Name of the trainer that will train this neural network.")] string trainerName,
            [ExcelArgument(Description = "Matrix of training inputs.")] object[,] inputs,
            [ExcelArgument(Description = "Matrix of training targets.")] object[,] targets,
            [ExcelArgument(Description = "Number of nodes in each hidden layer, not including biases. " +
                                         "Must be an array of integers.")] double[] hiddenLayerSizes)
        {
            var inputTargets = PrepareInputTargetSet(inputs, targets);

            var inputWidth  = inputs.GetLength(1);
            var targedWidth = targets.GetLength(1);
            var trainer     = ObjectStore.Get <ITrainer>(trainerName);

            var intHiddenLayerSizes = hiddenLayerSizes.Select(h => (int)h).ToArray();
            var nn = new MultilayerPerceptron(inputWidth, targedWidth, intHiddenLayerSizes);

            trainer.Train(inputTargets, nn);

            ObjectStore.Add(name, nn);
            return(name);
        }