public void Train_RuningTraining_NetworkIsTrained()
        {
            var network = new SimpleNeuralNetwork(3);

            var layerFactory = new NeuralLayerFactory();

            network.AddLayer(layerFactory.CreateNeuralLayer(3, new RectifiedActivationFuncion(), new WeightedSumFunction()));
            network.AddLayer(layerFactory.CreateNeuralLayer(1, new SigmoidActivationFunction(0.7), new WeightedSumFunction()));

            network.PushExpectedValues(
                new double[][] {
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 1 },
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 0 },
                new double[] { 0 },
            });

            network.Train(
                new double[][] {
                new double[] { 150, 2, 0 },
                new double[] { 1002, 56, 1 },
                new double[] { 1060, 59, 1 },
                new double[] { 200, 3, 0 },
                new double[] { 300, 3, 1 },
                new double[] { 120, 1, 0 },
                new double[] { 80, 1, 0 },
            }, 10000);

            network.PushInputValues(new double[] { 1054, 54, 1 });
            var outputs = network.GetOutput();
        }
        public override PatternResult Train(List <Pattern> patterns, int iterations)
        {
            if (patterns.Count == 0)
            {
                return(null);
            }

            DoubleVectorList inputs  = new DoubleVectorList();
            DoubleVectorList outputs = new DoubleVectorList();

            foreach (Pattern p in patterns)
            {
                inputs.Add(p.Inputs.Scale(0.02, 0.98));
                outputs.Add(p.Outputs);
            }

            double error = sann.Train(inputs, outputs, iterations);

            return(new PatternResult(error));
        }
Beispiel #3
0
        static void Train_predict_and_save(string filepath)
        {
            // train & testing data
            var training_data = new Dictionary <int, Tuple <float[], float[]> >
            {   //                                               ys                  xs
                [0] = new Tuple <float[], float[]>(new float[] { 0, 1 }, new float[] { 1 }),
                [1] = new Tuple <float[], float[]>(new float[] { 1, 0 }, new float[] { 1 }),
                [2] = new Tuple <float[], float[]>(new float[] { 0, 0 }, new float[] { 0 }),
                [3] = new Tuple <float[], float[]>(new float[] { 1, 1 }, new float[] { 0 })
            };

            var snn = new SimpleNeuralNetwork(2, 0.4f, Activation.FunctionsEnum.Sigmoid);

            snn.Add(4);
            snn.Add(1);

            // train
            Console.WriteLine("Entrenamiento:\n");
            Random random = new Random();
            int    j      = 0;

            Console.WriteLine("Training ...");
            for (int i = 0; i < 100000; i++)
            {
                j = random.Next(4);
                snn.Train(training_data[j].Item1, training_data[j].Item2);
            }

            // predict
            Console.WriteLine("\nPredicciones:\n");
            for (int i = 0; i < 4; i++)
            {
                var res = snn.Predict(training_data[i].Item1);
                Console.WriteLine(string.Format("xs [ {0}, {1} ] = {2}", training_data[i].Item1[0], training_data[i].Item1[1], res[0]));
            }


            SimpleNeuralNetwork.Save(snn, filepath);
            Console.WriteLine("\nRed Neuronal guardada !!.\n");
        }
        static void Main(string[] args)
        {
            var network = new SimpleNeuralNetwork(1);

            var layerFactory = new NeuralLayerFactory();

            network.AddLayer(layerFactory.CreateNeuralLayer(2, new RectifiedActivationFuncion(), new WeightedSumFunction()));

            network.AddLayer(layerFactory.CreateNeuralLayer(1, new SigmoidActivationFunction(0.4), new WeightedSumFunction()));

            double[][] expectedValues = new double[samples][];
            double[][] trainingValues = new double[samples][];

            for (int i = 0; i < samples; i++)
            {
                Random rng  = new Random();
                Random rng2 = new Random();

                int val1 = rng.Next(rng.Next() % 1000);
                int val2 = rng2.Next(i % 900);

                expectedValues[i] = new double[] { (val1 + val2) % 2 };
                trainingValues[i] = new double[] { val1, val2 };
                Console.WriteLine($"val1: {val1} val2: {val2} sum: { (val1 + val2) % 2 }");
            }

            network.PushExpectedValues(expectedValues);



            network.Train(trainingValues, 5000);



            network.PushInputValues(new double[] { 1054, 54 });
            var outputs = network.GetOutput();

            Console.WriteLine($"network output: {string.Join(", ", outputs)}");
            Console.ReadKey();
        }
        public void TrainNetwork_6Inputs_3HiddenLayer_2Outputs()
        {
            var network      = new SimpleNeuralNetwork(6, 1.95); // six input nuerons
            var layerFactory = new NeuralLayerFactory();

            network.AddLayer(layerFactory.CreateNeuralLayer(3, new SigmoidActivationFunction(0.7), new WeightedSumFunction())); // three hidden layers
            network.AddLayer(layerFactory.CreateNeuralLayer(2, new LazyOutputFunction(), new WeightedSumFunction()));           // two output layers
            network.PushExpectedValues(
                new double[][] {
                new double[] { 0.25, 0.20 },
                new double[] { 0.10, 0.05 },
                new double[] { 0.16, 0.30 },
                new double[] { 0.30, 0.10 },
                new double[] { 0.25, 0.20 },
                new double[] { 0.10, 0.05 },
                new double[] { 0.16, 0.30 },
                new double[] { 0.30, 0.10 },
            });
            network.Train(
                new double[][] {
                new double[] { 150, 0, 0, 34, 35, 56 },
                new double[] { 190, 23, 56, 0, 29, 529 },
                new double[] { 290, 3, 108, 24, 189, 20 },
                new double[] { 290, 67, 6, 0, 1, 0 },
                new double[] { 150, 0, 0, 34, 35, 56 },
                new double[] { 190, 23, 56, 0, 29, 529 },
                new double[] { 290, 3, 108, 24, 189, 20 },
                new double[] { 290, 67, 6, 0, 1, 0 },
            }, 10000);

            network.PushInputValues(new double[] { 150, 0, 0, 34, 35, 56 });

            var outputs = network.GetOutput();

            Console.WriteLine(outputs[0].ToString() + " " + outputs[1].ToString() + "\n\n" + JsonConvert.SerializeObject(network, Formatting.Indented));
        }