Beispiel #1
0
        static void Main(string[] args)
        {
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            var trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            var train = new ResilientPropagation(network, trainingSet);
            var epoch = 1;
            do
            {
                train.Iteration();

            } while (train.Error > 0.01);

            train.FinishTraining();

            foreach (var pair in trainingSet)
            {
                var output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @", " + pair.Input[1] + @" , actual=" + output[0] + @", ideal=" + pair.Ideal[0]);
            }

            EncogFramework.Instance.Shutdown();
            Console.ReadLine();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //create a neural network withtout using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));

            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            int epoch = 1;
            do
            {
                train.Iteration();
                Console.WriteLine($"Epoch #{epoch} Error: {train.Error}");
                epoch++;
            } while (train.Error > 0.01);
            train.FinishTraining();

            Console.WriteLine("Neural Network Results:");
            foreach (IMLDataPair iPair in trainingSet)
            {
                IMLData output = network.Compute(iPair.Input);
                Console.WriteLine($"{iPair.Input[0]}, {iPair.Input[0]}, actual={output[0]}, ideal={iPair.Ideal[0]}");
            }

            EncogFramework.Instance.Shutdown();

            Console.ReadKey();
        }
        private static void Main(string[] args)
        {
            // create a neural network, without using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

            // train the neural network
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);

            train.FinishTraining();

            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }

            EncogFramework.Instance.Shutdown();
        }
Beispiel #4
0
            void Train()
            {
                if (Memory.Count>0)
                {
                    network.Reset();
                    double[][] InputData = new double[Memory.Count][]; //подготовка данных для обучения сети
                    double[][] SenseData = new double[Memory.Count][];
                    for (int i = 0; i < Memory.Count; i++)
                    {
                        InputData[i] = Memory[i];
                        SenseData[i] = MemorySense[i];
                    }
                    IMLDataSet trainingSet = new BasicMLDataSet(InputData, SenseData);
                    IMLTrain train = new ResilientPropagation(network, trainingSet);

                    int epoch = 1;

                    double old = 9999;
                    double d = 999;
                    do
                    {
                        train.Iteration();
                        //Console.SetCursorPosition(0, 0); //вывод информации о текущем состоянии обучения
                        //Console.Write(@"Epoch #" + epoch + @" Error:" + train.Error);
                        epoch++;
                        d = Math.Abs(old - train.Error);
                        old = train.Error;
                    } while (train.Error > 0.0001 && epoch < 3000 && d > 0.00001);

                    train.FinishTraining();

                    //double sumd=0.0; //подсчет суммарной ошибки после обучения
                    //foreach (IMLDataPair pair in trainingSet)
                    //{
                    //    IMLData output = network.Compute(pair.Input);
                    //    sumd = sumd + Math.Abs(pair.Ideal[0] - output[0]);
                    //    sumd = sumd / trainingSet.InputSize;
                    //}
                }
            }
        public int Train(DataSet dataSet)
        {
            Network = new BasicNetwork();
            Network.AddLayer(new BasicLayer(null, true, 8 * 21));
            var first = ((8 * 21 + 4) * FirstLayerParameter);
            Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)first));
            var second = ((8 * 21 + 4) * SecondLayerParameter);
            Network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, (int)second));
            Network.AddLayer(new BasicLayer(null, false, 1));
               // Network.AddLayer(new );
            Network.Structure.FinalizeStructure();
            Network.Reset();
            //IMLData x = new BasicNeuralData();
            var set = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][];
            var ideal = new double[dataSet.Signatures.Count + dataSet.Forgeries.Count][];
            for (int i = 0; i < dataSet.Signatures.Count; i++)
            {
                set[i] = dataSet.Signatures[i].Data.Cast<double>().ToArray();
                ideal[i] = new double[] {1};
            }
            for (int i = dataSet.Signatures.Count; i < dataSet.Signatures.Count  + dataSet.Forgeries.Count; i++)
            {
                set[i] = dataSet.Forgeries[i- dataSet.Signatures.Count].Data.Cast<double>().ToArray();
                ideal[i] = new double[] { 0 };
            }

            IMLDataSet trainingSet = new BasicMLDataSet(set, ideal);

            IMLTrain train = new ResilientPropagation(Network, trainingSet);

            int epoch = 1;
            var errors = new List<double>();
            do
            {
                train.Iteration();
                // Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
                errors.Add(train.Error);

            } while ( epoch < 10000);

            train.FinishTraining();

            return 1;
        }
        public List<double[]> Learn(double[][] data, double[][] ideal)
        {
            double[][] origData = (double[][])data.Clone();
            int n = data.Length;
            int m = data[0].Length;
            double[][] output = new double[n][];
            double[][] sgmNeighbours = new double[n][];
            for (var i = 0; i < n; i++)
            {
                double[] sgmN = new double[SegmentationData.SEGMENT_NEIGHBOURS];
                Array.Copy(data[i], m - SegmentationData.SEGMENT_NEIGHBOURS, sgmN, 0, SegmentationData.SEGMENT_NEIGHBOURS);
                sgmNeighbours[i] = sgmN;
                data[i] = data[i].Take(m - SegmentationData.SEGMENT_NEIGHBOURS).ToArray();
                output[i] = new double[m - SegmentationData.SEGMENT_NEIGHBOURS];
                data[i].CopyTo(output[i], 0);
            }

            IMLDataSet trainingSet = new BasicMLDataSet(data, output);

            int inputLayerSize = layersConfiguration[0] - SegmentationData.SEGMENT_NEIGHBOURS;
            int trainingLayerSize = layersConfiguration[1];
            BasicNetwork oneLayerAutoencoder = new BasicNetwork();
            oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
            oneLayerAutoencoder.Structure.FinalizeStructure();
            oneLayerAutoencoder.Reset();

            IMLTrain train = new ResilientPropagation(oneLayerAutoencoder, trainingSet);
            //IMLTrain train = new Backpropagation(oneLayerAutoencoder, trainingSet, LEARNING_RATE, MOMENTUM);

            int epoch = 1;
            List<double[]> errors = new List<double[]>();
            double[] trainError = new double[AUTOENCODER_MAX_ITER];

            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;
                //errors.Add(train.Error);
            } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            BasicNetwork encoder = new BasicNetwork();
            encoder.AddLayer(new BasicLayer(null, BIAS, oneLayerAutoencoder.GetLayerNeuronCount(0)));
            encoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
            encoder.Structure.FinalizeStructure();
            encoder.Reset();

            //przypisanie wag do encodera
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                        encoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(i, f, t));

            //Compare2Networks(oneLayerAutoencoder, encoder);

            for(int l=1; l<layersConfiguration.Count -2; l++)
            {
                inputLayerSize = layersConfiguration[l];
                trainingLayerSize = layersConfiguration[l+1];
                oneLayerAutoencoder = new BasicNetwork();
                oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
                oneLayerAutoencoder.Structure.FinalizeStructure();
                oneLayerAutoencoder.Reset();

                //liczenie outputu z dotychczasowego encodera
                double[][] input = new double[n][];
                double[][] newOutput = new double[n][];
                for(int ni = 0; ni <n; ni++)
                {
                    IMLData res = encoder.Compute(new BasicMLData(data[ni]));
                    double[] resD = new double[res.Count];
                    for(int i=0; i<res.Count; i++)
                        resD[i] = res[i];
                    input[ni] = resD;
                    newOutput[ni] = new double[res.Count];
                    input[ni].CopyTo(newOutput[ni], 0);
                }

                BasicMLDataSet newTrainingSet = new BasicMLDataSet(input, newOutput);
                train = new ResilientPropagation(oneLayerAutoencoder, newTrainingSet);
                //train = new Backpropagation(oneLayerAutoencoder, newTrainingSet, LEARNING_RATE, MOMENTUM);

                epoch = 1;
                trainError = new double[AUTOENCODER_MAX_ITER];
                do
                {
                    train.Iteration();
                    ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                    Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                    trainError[epoch - 1] = train.Error;
                    epoch++;
                } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
                errors.Add(trainError);
                train.FinishTraining();

                BasicNetwork extendedEncoder = new BasicNetwork();
                extendedEncoder.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0)));
                for (int el = 1; el < encoder.LayerCount; el++ )
                    extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el)));
                extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
                extendedEncoder.Structure.FinalizeStructure();

                //przypisanie wag do extendedencodera
                for (int i = 0; i < extendedEncoder.LayerCount - 1; i++)
                {
                    if (i < encoder.LayerCount-1)
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }
                    else
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(0, f, t));
                    }
                }
                encoder = extendedEncoder;

            }

            //tworzenie struktury ostatecznej sieci
            network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0) + SegmentationData.SEGMENT_NEIGHBOURS));
            for (int el = 1; el < encoder.LayerCount; el++)
                network.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el) + SegmentationData.SEGMENT_NEIGHBOURS));
            network.AddLayer(new BasicLayer(CurrentActivationFunction(), false, layersConfiguration[layersConfiguration.Count - 1]));
            network.Structure.FinalizeStructure();
            network.Reset();

            /*
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
            */
            //dla innych ustawic wagi 0, dla samych sobie 1

            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < network.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < network.GetLayerNeuronCount(i + 1); t++)
                    {
                        if (f < encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t < encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 1);
                        else
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }

            //uczenie koncowej sieci
            trainingSet = new BasicMLDataSet(origData, ideal);

            train = new ResilientPropagation(network, trainingSet);
            //train = new Backpropagation(network, trainingSet, LEARNING_RATE, MOMENTUM);

            epoch = 1;
            trainError = new double[FINAL_NETWORK_MAX_ITER];
            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;

            } while (train.Error > EPS && epoch < FINAL_NETWORK_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            try
            {
                string networkFileName = "autoencoder wo cmp 300 125 50 3";
                EncogDirectoryPersistence.SaveObject(new FileInfo(networkFileName), network);
                MessageBox.Show("NETWORK SAVED TO FILE " + networkFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return errors;
        }
        static void Main(string[] args)
        {
            double[][] input = CsvReader.ConvertCsvToTwoDimDoubleArrary(@"X.csv");
            double[][] idealTemp = CsvReader.ConvertCsvToTwoDimDoubleArrary(@"Y.csv");
            double[][] ideal = convertIdealArrary(idealTemp, 10);

            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 400));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 25));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 10));
            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(input, ideal);

            //IMLTrain train = new Backpropagation(network, trainingSet);
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            /*
            int epoch = 1;
            do
            {
                train.Iteration();

                if (epoch % 10 == 0)
                {
                    Console.WriteLine(@"Epoch# " + epoch + @" Error: " + train.Error);
                }

                epoch++;
            }
            while (train.Error > 0.01);
            */

            for (int epoch = 1; epoch < 1000; epoch++)
            {
                train.Iteration();

                if (epoch % 10 == 0)
                {
                    Console.WriteLine(@"Epoch# " + epoch + @" Error: " + train.Error);
                }
            }

            train.FinishTraining();

            Console.WriteLine("Training Completed");

            Console.WriteLine(@"Neural network results:");
            double successCount = 0;
            double totalCount = 0;
            foreach (IMLDataPair pair in trainingSet)
            {
                totalCount++;
                IMLData output = network.Compute(pair.Input);

                double max1 = -99;
                int index1 = -1;
                for (int i = 0; i < output.Count; i++)
                {
                    if (output[i] > max1)
                    {
                        max1 = output[i];
                        index1 = i;
                    }
                }

                double max2 = -99;
                int index2 = -2;
                for (int i = 0; i < pair.Ideal.Count; i++)
                {
                    if (pair.Ideal[i] > max2)
                    {
                        max2 = pair.Ideal[i];
                        index2 = i;
                    }
                }

                if (index1 == index2)
                {
                    successCount++;
                }
            }

            double rate = successCount / totalCount;
            Console.WriteLine(rate);

            EncogFramework.Instance.Shutdown();
            Console.ReadLine();
        }
Beispiel #8
0
        public static void TrainNet()
        {
            var tmp = new np4load(@"..\..\..\NeuralNetworks\Cardiology\инфаркт_миокарда.np4");

            TrainLogger tLog = TrainLogger.GetTrainLogger();

            Dictionary<string, object> dLog = new Dictionary<string, object>();

            var stopParam = tmp.GetTrainingStopParams();
            int iterCount = 2500;//13943;

            dLog.Add("event", "init");
            dLog.Add("NeuralNetName", "инфаркт_миокарда.np4");
            dLog.Add("Path", @"..\..\..\NeuralNetworks\Cardiology\инфаркт_миокарда.np4");
            dLog.Add("Анкета", "Кардиология");
            dLog.Add("IterationCount", iterCount);
            dLog.Add("Time", DateTime.Now);

            BasicMLDataSet tData = (BasicMLDataSet) tmp.GetTrainingData();
            dLog.Add("TrainDataSize", tData.Count);
            //записать входные данные и проскалированные
            tLog.WriteEvent(dLog);

            BasicNetwork neuralNet = tmp.GetNeuralNetwork();

            MediatorNsimLinearScale scale = new MediatorNsimLinearScale();
            DataProcessorConf dp = tmp.GetDataProcessor();
            scale.DataProcessorM(dp);

            //SearchHashForm search = new SearchHashForm();
            //Guid[] gidForm = search.GetGIDForm(tData);

            BasicMLDataSet tDataScale = scale.ProcessDataSet(tData);

            IMLTrain trainMetod;
            switch (tmp.GetNameTrainMetod())
            {
                case "ResilientPropagation":
                    {
                        trainMetod = new ResilientPropagation(neuralNet, tDataScale);
                        break;
                    }
                default:
                    {
                        trainMetod = new ResilientPropagation(neuralNet, tDataScale);
                        break;
                    }
            }

            //trainMetod.Iteration(stopParam.Iterations);

            trainMetod.Iteration(iterCount);
            //var arrWeight = neuralNet.DumpWeights().Split(',');

            //using (FileLogger fl = FileLogger.GetLogger())
            //{
            //    fl.WriteString(neuralNet.DumpWeights());
            //    for (int i = 0; i < tDataScale.Data.Count; i++)//var item in tDataScale.Data)
            //    {
            //        fl.WriteString("Входной вектор: " + GetString(tData.Data[i].InputArray));

            //        var res = neuralNet.Compute(tDataScale.Data[i].Input);

            //        fl.WriteString("Результат: " + GetString(res.Data));

            //        var sRes = scale.RestoreIdealVector(res);

            //        fl.WriteString("Результат востановленный: " + GetString(sRes.Data));
            //        fl.WriteString("\n");
            //    }
            //}

            trainMetod.FinishTraining();
            //int countW = neuralNet.EncodedArrayLength();
            //double[] w = new double[countW];
            //neuralNet.EncodeToArray(w);

            Console.WriteLine("!!!");
            //for (int i = 0; i < stopParam.Iterations; i++)
            //{
            //    trainMetod.Iteration(
            //}
        }
        private static EncogTrainingResponse TrainNetwork2(BasicNetwork network, TrainingData training, double maxError, CancellationToken cancelToken, double? maxSeconds = null)
        {

            //TODO: When the final layer is softmax, the error seems to be higher.  Probably because the training outputs need to be run through softmax

            const int MAXITERATIONS = 5000;

            INeuralDataSet trainingSet = new BasicNeuralDataSet(training.Input, training.Output);
            ITrain train = new ResilientPropagation(network, trainingSet);

            DateTime startTime = DateTime.UtcNow;
            TimeSpan? maxTime = maxSeconds != null ? TimeSpan.FromSeconds(maxSeconds.Value) : (TimeSpan?)null;

            bool success = false;

            //List<double> log = new List<double>();
            int iteration = 1;
            double error = double.MaxValue;
            while (true)
            {
                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }

                train.Iteration();

                error = train.Error;
                //log.Add(error);

                iteration++;

                if (double.IsNaN(error))
                {
                    break;
                }
                else if (error < maxError)
                {
                    success = true;
                    break;
                }
                else if (iteration >= MAXITERATIONS)
                {
                    break;
                }
                else if (maxTime != null && DateTime.UtcNow - startTime > maxTime)
                {
                    break;
                }
            }

            //string logExcel = string.Join("\r\n", log);       // paste this into excel and chart it to see the error trend

            train.FinishTraining();

            return new EncogTrainingResponse(network, success, error, iteration, (DateTime.UtcNow - startTime).TotalSeconds);
        }