Exemplo n.º 1
0
        public void TestNetworkWorkingAtAll_TryAndOperation()
        {
            IActivationFunction func = new BipolarSigmoid(2.0);
            var layerCounts = new List<int>();
            layerCounts.Add(2);
            layerCounts.Add(2);
            layerCounts.Add(1);
            var network = new Network(2, 3, layerCounts, func, new RandomInitializer());
            //double[] input = new double[] { -3, -2, -1, 0, 1, 2, 3 };
            //double[] output = new double[] { 9, 4, 1, 0, 1, 4, 9 };

            double[][] input = new double[][] { new[] { 0.0, 0.0 }, new[] { 1.0, 0.0 }, new[] { 0.0, 1.0 }, new[] { 1.0, 1.0 } };
            double[] output = new double[] { 0.0, 1.0, 1.0, 1.0 };

            //double[] input = new double[] { 1,2,3,4,5,6,7,8,9 };
            //double[] output = new double[]{ 0,4,7,9,10,9,7,4,0 };
            var learner = new Teacher(network);

            learner.Rate = 0.2;
            learner.Momentum = 0.0;

            List<double[]> netInputs = new List<double[]>();
            List<double[]> netOutputs = new List<double[]>();

            for (int i = 0; i < input.Length; i++)
            {
                netInputs.Add(input[i]);
                netOutputs.Add(new double[]{output[i]});
            }

            for (int i = 0; i < 1000; i++)
            {
                learner.TeachOnSamples(netInputs, netOutputs);
            }

            double[] aproximated = new double[input.Length];
            Console.WriteLine("Try those Values in Excel or whatever");
            using (var writer = new StreamWriter("data_dump.txt"))
            {
                for (int i = 0; i < aproximated.Length; i++)
                {
                    aproximated[i] = network.ComputeOutputVector(input[i])[0];
                    writer.WriteLine((aproximated[i] ).ToString());
                }
            }
        }
Exemplo n.º 2
0
        private void tests()
        {
            //using (var writer = new StreamWriter("1hidelayer.txt"))
            //{
            //    Init = new RandomInitializer();
            //    Activ = new BipolarSigmoid((double)2.0);
            //    LearnerRate = 0.3;
            //    LearnerMomentum = 0.0;

            //    string column = "window|hide layer 1|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
            //    writer.WriteLine(column);
            //    for (int j = 2; j < 10; j++)
            //    {
            //        for (int i = 1; i < 21; i++)
            //        {

            //            string toFile = j + "|" + i + "|";
            //            Layer = new List<int> { j };
            //            Layer.Add(i);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);

            //        }
            //    }
            //}

            using (var writer = new StreamWriter("2hidelayer100.txt"))
            {
                Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(2.0));
                LearnerRate = 0.3;
                LearnerMomentum = 0.0;
                IterationsCount = 100;
                string column = "window|hide layer 1|hide layer 2|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
                writer.WriteLine(column);
                for (int j = 2; j < 10; j++)
                {
                    for (int i = 1; i < 21; i++)
                    {
                        for (int k = 1; k < 21; k++)
                        {
                            string toFile = j + "|" + i + "|" + k + "|";
                            Layer = new List<int> { j };
                            Layer.Add(i);
                            Layer.Add(k);
                            Layer.Add(1);
                            var a = this.Owner as MainForm;
                            if (a != null)
                            {
                                a.SetSettings();
                                toFile += a.Teach(false).ToString("F6") + "|";
                                toFile += a.Predict(false, true).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
                                toFile += a.Predict(false, false).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 1).ToString("F6");
                            }
                            writer.WriteLine(toFile);
                        }
                    }
                }
            }

            using (var writer = new StreamWriter("2hidelayer200.txt"))
            {
                Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(2.0));
                LearnerRate = 0.3;
                LearnerMomentum = 0.0;
                IterationsCount = 500;
                string column = "window|hide layer 1|hide layer 2|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
                writer.WriteLine(column);
                for (int j = 2; j < 10; j++)
                {
                    for (int i = 1; i < 21; i++)
                    {
                        for (int k = 1; k < 21; k++)
                        {
                            string toFile = j + "|" + i + "|" + k + "|";
                            Layer = new List<int> { j };
                            Layer.Add(i);
                            Layer.Add(k);
                            Layer.Add(1);
                            var a = this.Owner as MainForm;
                            if (a != null)
                            {
                                a.SetSettings();
                                toFile += a.Teach(false).ToString("F6") + "|";
                                toFile += a.Predict(false, true).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
                                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
                                toFile += a.Predict(false, false).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
                                toFile += a.Predict(false, false, 1).ToString("F6");
                            }
                            writer.WriteLine(toFile);
                        }
                    }
                }
            }

            //using (var writer = new StreamWriter("1hidelayerRandomInit.txt"))
            //{
            //    Init = new RandomInitializer();
            //    Activ = new BipolarSigmoid((double)2.0);
            //    LearnerRate = 0.3;
            //    LearnerMomentum = 0.0;
            //    IterationsCount = 500;
            //    writer.WriteLine("func activ -BipolarSigmoid((double)2.0) , init - RandomInitializer()  500 iteracji");
            //    string column = "window|hide layer 1|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
            //    writer.WriteLine(column);
            //    for (int j = 2; j < 10; j++)
            //    {
            //        for (int i = 1; i < 21; i++)
            //        {

            //                string toFile = j + "|" + i + "|";
            //                Layer = new List<int> { j };
            //                Layer.Add(i);
            //                Layer.Add(1);
            //                var a = this.Owner as MainForm;
            //                if (a != null)
            //                {
            //                    a.SetSettings();
            //                    toFile += a.Teach(false).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 1).ToString("F6");
            //                }
            //                writer.WriteLine(toFile);

            //        }
            //    }
            //}
            //using (var writer = new StreamWriter("1hidelayerConstInit.txt"))
            //{
            //    Init = new ConstInitializer(5);
            //    Activ = new BipolarSigmoid((double)2.0);
            //    LearnerRate = 0.3;
            //    LearnerMomentum = 0.0;
            //    IterationsCount = 500;
            //    string column = "window|hide layer 1|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
            //    writer.WriteLine("func activ -BipolarSigmoid((double)2.0) , init - ConstInitializer(5) 500 iteracji");
            //    writer.WriteLine(column);
            //    for (int j = 2; j < 10; j++)
            //    {
            //        for (int i = 1; i < 21; i++)
            //        {
            //            string toFile = j + "|" + i + "|";
            //            Layer = new List<int> { j };
            //            Layer.Add(i);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);

            //        }
            //    }
            //}
            //using (var writer = new StreamWriter("1hidelayerSigmoid.txt"))
            //{
            //    Init = new RandomInitializer();
            //    Activ = new Sigmoid(2.0);
            //    LearnerRate = 0.3;
            //    LearnerMomentum = 0.0;
            //    IterationsCount = 500;
            //    string column = "window|hide layer 1|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1";
            //    writer.WriteLine("func activ -Sigmoid(2.0) , init - RandomInitializer() 500 iteracji");
            //    writer.WriteLine(column);
            //    for (int j = 2; j < 10; j++)
            //    {
            //        for (int i = 1; i < 21; i++)
            //        {
            //            string toFile = j + "|" + i + "|";
            //            Layer = new List<int> { j };
            //            Layer.Add(i);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);

            //        }
            //    }
            //}
            //    using (var writer = new StreamWriter("wskazniki.txt"))
            //    {
            //        Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(2.0));
            //        ////Activ = new BipolarSigmoid((double)2.0);
            //        LearnerRate = 0.3;
            //        LearnerMomentum = 0.0;
            //        IterationsCount = 100;
            //        writer.WriteLine("indicators|argument|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1");

            //        for (int j = 0; j < 6; j++)
            //        {
            //            for (int i = 0; i < 30; i++)
            //            {
            //                string toFile = "";
            //                switch (j)
            //                {
            //                    case 0:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.SMA, i));
            //                        toFile += "sma|";
            //                        break;
            //                    case 1:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.WMA, i));
            //                        toFile += "wma|";
            //                        break;
            //                    case 2:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.EMA, i));
            //                        toFile += "ema|";
            //                        break;
            //                    case 3:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.ROC, i));
            //                        toFile += "roc|";
            //                        break;
            //                    case 4:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.MACD, i));
            //                        toFile += "macd|";
            //                        break;
            //                    case 5:
            //                        Indicator.Clear();
            //                        Indicator.Add(new KeyValuePair<Indi, int>(Indicators.Indicators.Oscillator, 3 + i));
            //                        toFile += "oscil|";
            //                        break;
            //                }
            //                toFile += i + "|";
            //                Layer = new List<int> { 4 };
            //                Layer.Add(12);
            //                Layer.Add(1);
            //                var a = this.Owner as MainForm;
            //                if (a != null)
            //                {
            //                    a.SetSettings();
            //                    toFile += a.Teach(false).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                    toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                    toFile += a.Predict(false, false, 1).ToString("F6");
            //                }
            //                writer.WriteLine(toFile);
            //            }

            //        }
            //    }
            //    using (var writer = new StreamWriter("parametry.txt"))
            //    {
            //        writer.WriteLine("rate|momentum|alpha|teach error|predic error 30%|10 values|5 |1|predic error 30%|10 values|5 |1");
            //        double alpha = 0;
            //        LearnerRate = 0.0;
            //        LearnerMomentum = 0.0;
            //        IterationsCount = 100;
            //        for (int j = 0; j < 50; j++)
            //        {
            //            Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(2.0));
            //            //Activ = new BipolarSigmoid((double)2.0);

            //            string toFile = LearnerRate + "|" + LearnerMomentum + "|2.0|";
            //            Layer = new List<int> { 3 };
            //            Layer.Add(12);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);
            //            LearnerRate += 0.05;
            //        }
            //        for (int j = 0; j < 50; j++)
            //        {
            //            Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(2.0));
            //            //Activ = new BipolarSigmoid((double)2.0);
            //            LearnerRate = 0.3;
            //            string toFile = LearnerRate + "|" + LearnerMomentum + "|2.0|";
            //            Layer = new List<int> { 3 };
            //            Layer.Add(12);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);

            //            LearnerMomentum += 0.05;
            //        }
            //        for (int j = 0; j < 50; j++)
            //        {
            //            Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid(alpha));
            //            //Activ = new BipolarSigmoid((double)2.0);
            //            LearnerRate = 0.3;
            //            LearnerMomentum = 0.0;
            //            string toFile = LearnerRate + "|" + LearnerMomentum + "|" + alpha + "|";
            //            Layer = new List<int> { 3 };
            //            Layer.Add(12);
            //            Layer.Add(1);
            //            var a = this.Owner as MainForm;
            //            if (a != null)
            //            {
            //                a.SetSettings();
            //                toFile += a.Teach(false).ToString("F6") + "|";
            //                toFile += a.Predict(false, true).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, true, 1).ToString("F6") + "|";
            //                toFile += a.Predict(false, false).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 10).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 5).ToString("F6") + "|";
            //                toFile += a.Predict(false, false, 1).ToString("F6");
            //            }
            //            writer.WriteLine(toFile);
            //            alpha += 0.1;
            //        }
            //    }
        }
Exemplo n.º 3
0
        private void AssignValue()
        {
            switch (initFuncComboBox.SelectedIndex)
            {
                case (0):
                    Init = new RandomInitializer();
                    break;
                case (1):
                    Init = new OptimalRangeRandomInitializer(Activ = new BipolarSigmoid((double)alphaNumeric.Value));
                    break;
                case (2):
                    Init = new ConstInitializer(double.Parse(constValueTextBox.Text));
                    break;
                default:
                    Init = null;
                    break;
            }

            if (activFuncComboBox.SelectedIndex == 1)
            {
                Activ = new BipolarSigmoid((double)alphaNumeric.Value);
            }
            else
            {
                Activ = new Sigmoid((double)alphaNumeric.Value);
            }

            LearnerRate = (double)rateNumeric.Value;
            LearnerMomentum = (double)momentumNumeric.Value;

            int firstLayer = (int)windowSize.Value;
            firstLayer += AddIndicators();

            Layer = new List<int> { firstLayer };
            if (layerCountCheckBox.Checked)
            {
                for (int i = 0; i < (int)layersNumeric.Value - 2; i++)
                {
                    Layer.Add((int)windowSize.Value * 2);
                }
            }
            else
            {
                for (int i = 0; i < (int)layersNumeric.Value - 2; i++)
                {
                    var neuron = new NeuronCounts { Text = "Layer no. " + i + " count" };
                    DialogResult res = neuron.ShowDialog(this);
                    if (res == DialogResult.OK)
                    {
                        Layer.Add(neuron.Value);
                    }
                }
            }

            Layer.Add(1);

            int iterations;
            if (!int.TryParse(iterationsTextBox.Text, out iterations))
            {
                iterations = 1000;
            }
            IterationsCount = iterations;
        }
Exemplo n.º 4
0
        public void TestTimeSeriesPrediction()
        {
            const int windowSize = 5;
            const int predictionSize = 1;
            IActivationFunction func = new BipolarSigmoid(2.0);
            var layerCounts = new List<int>();
            layerCounts.Add(windowSize);
            layerCounts.Add(2*windowSize);
            layerCounts.Add(1);
            var network = new Network(5, 3, layerCounts, func, new RandomInitializer());
            //double[] input = new double[] { -3, -2, -1, 0, 1, 2, 3 };
            //double[] output = new double[] { 9, 4, 1, 0, 1, 4, 9 };

            double[] readedData = new double[50];
            double[] data;
            double min, max;
            double range;
            using(var reader = new StreamReader(File.OpenRead("sinusoid.csv")))
            {
                int i = 0;
                readedData[i] = double.Parse(reader.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
                min = readedData[i];
                max = readedData[i];

                try
                {
                    for (i = 1; i < 50; i++)
                    {
                        readedData[i] = double.Parse(reader.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
                        if (min > readedData[i])
                            min = readedData[i];
                        if (max < readedData[i])
                            max = readedData[i];
                    }
                }
                catch (Exception)
                {
                }
                if (i > 0)
                {
                    data = new double[i];
                    Array.Copy(readedData, data, i);
                }
                else return;
            }
            range =  (max - min);
            int learningSamples = data.Length - windowSize - predictionSize;

            double[][] input = new double[learningSamples][];
            double[][] output = new double[learningSamples][];

            for (int i = 0; i < learningSamples; i++)
            {
                input[i] = new double[windowSize];
                output[i] = new double[1];

                for (int j = 0; j < windowSize; j++)
                {
                    input[i][j] = TransformData(data[i + j], min, range);
                }
                output[i][0] = TransformData(data[i + windowSize], min, range);
            }
            //double[] input = new double[] { 1,2,3,4,5,6,7,8,9 };
            //double[] output = new double[]{ 0,4,7,9,10,9,7,4,0 };
            var learner = new Teacher(network);

            learner.Rate = 0.1;
            learner.Momentum = 0.0;
            var solution = new double[data.Length - windowSize, 2];
            var netInput = new double[windowSize];

            for (int i = 0; i < 1000; i++)
            {
                var ins = new List<double[]>();
                var outs = new List<double[]>();

                for (int ii = 0; ii < input.Length; ii++)
                {
                    ins.Add(input[ii]);
                    outs.Add(output[ii]);
                }
                learner.TeachOnSamples(ins, outs);
                for (int j = 0; j < data.Length - windowSize; j++)
                {
                    for (int k = 0; k < windowSize; k++)
                    {
                        netInput[k] = TransformData(data[j + k], min, range) ;
                    }
                    solution[j,1]= TransformBack(network.ComputeOutputVector(netInput)[0], min, max );//(network.ComputeOutputVector(netInput)[0]) / range + min;
                }
            }

            double[] aproximated = new double[data.Length - windowSize];
            Console.WriteLine("Try those Values in Excel or whatever");
            using (var writer = new StreamWriter("data_dump_time_series.txt"))
            {
                for (int i = 0; i < aproximated.Length; i++)
                {
                    aproximated[i] = solution[i,1];
                    writer.WriteLine((aproximated[i]).ToString());
                }
            }
        }
Exemplo n.º 5
0
        public void TestXMLSerializationAndDeserialization()
        {
            IActivationFunction func = new BipolarSigmoid(2.0);
            var layerCounts = new List<int>();
            layerCounts.Add(5);
            layerCounts.Add(10);
            layerCounts.Add(1);
            var network = new Network(5, 3, layerCounts, func, new RandomInitializer());

            var serializer = new DataContractSerializer(typeof(Network), new[] { func.GetType(), typeof(RandomInitializer) });

            var settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";//4 spaces
            string file = "serialized_net.xml";
            using (var writer = XmlWriter.Create(file, settings))
            {
                serializer.WriteObject(writer, network);
            }

            Network newNet = (Network)serializer.ReadObject(XmlReader.Create(file));
            Assert.IsNotNull(newNet);
            Assert.AreEqual(5, newNet.Inputs);
            Assert.AreEqual(3, newNet.LayersCount);
            Assert.IsTrue(0 < newNet[0][0].Weights[0]);
            Assert.AreEqual(typeof(BipolarSigmoid), newNet[0][0].ActivationFunction.GetType());
        }
Exemplo n.º 6
0
        public void TestXMLSerialization()
        {
            IActivationFunction func = new BipolarSigmoid(2.0);
            var layerCounts = new List<int>();
            layerCounts.Add(5);
            layerCounts.Add(10);
            layerCounts.Add(1);
            var network = new Network(5, 3, layerCounts, func, new RandomInitializer());

            var serializer = new DataContractSerializer(typeof(Network),new[]{func.GetType(), typeof(RandomInitializer)});

            var fileName = "serialized_net.xml";

            var settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";//4 spaces

            using (var writer = XmlWriter.Create(fileName, settings))
            {
                serializer.WriteObject(writer, network);
            }

            var fInfo = new FileInfo(fileName);
            Assert.IsTrue(fInfo.Exists);
            Assert.IsTrue(fInfo.Length > 0);
        }