예제 #1
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());
        }
예제 #2
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);
        }
예제 #3
0
        public void Load(byte[] data)
        {
            var memStream = new MemoryStream();
            var binForm   = new BinaryFormatter();

            memStream.Write(data, 0, data.Length);
            memStream.Seek(0, SeekOrigin.Begin);

            var obj    = (SaveNetworkModel)binForm.Deserialize(memStream);
            var layers = new List <ILayer>();

            Layers = null;

            foreach (var layer in obj.Layers)
            {
                var function = new BipolarSigmoid();
                var relu     = new ELU();

                if (layer.Type == LayerType.Convolution)
                {
                    layers.Add(new ConvolutionalLayer(layer.ActivationType, layer.OutputLength, layer.KernelSize));
                }

                if (layer.Type == LayerType.MaxPoolingLayer)
                {
                    layers.Add(new MaxPoolingLayer(layer.KernelSize));
                }

                if (layer.Type == LayerType.FullyConnected)
                {
                    layers.Add(new FullyConnectedLayer(layer.OutputLength, layer.ActivationType));
                }
            }

            InitLayers(obj.InputWidth, obj.InputHeight, layers.ToArray());

            for (var l = 0; l < Layers.Length; l++)
            {
                if (Layers[l].Type == LayerType.Convolution)
                {
                    var layer = (ConvolutionalLayer)Layers[l];

                    for (var n = 0; n < layer.NeuronsCount; n++)
                    {
                        layer.Neurons[n].Weights = obj.Layers[l].ConvNeurons[n].Weights;
                    }
                }

                if (Layers[l].Type == LayerType.FullyConnected)
                {
                    var layer = (FullyConnectedLayer)Layers[l];

                    for (var n = 0; n < layer.NeuronsCount; n++)
                    {
                        layer.Neurons[n].Weights = obj.Layers[l].FullyConnectedNeurons[n].Weights;
                    }
                }
            }
        }
예제 #4
0
        public void BipolarSigmoidPrimeTest()
        {
            var a = new Matrix(2, 2);

            a.InRandomize();
            var b = a.Duplicate();

            a = new BipolarSigmoid().Backward(a);
            b.InMap((x) => 0.5 * (1 + (-1 + 2 / (1 + Math.Exp(-x)))) * (1 - (-1 + 2 / (1 + Math.Exp(-x)))));
            Assert.IsTrue(Math.Abs(a.FrobeniusNorm() - b.FrobeniusNorm()) < 0.1,
                          new BipolarSigmoid().Type().ToString() + " Derivative.");
        }
예제 #5
0
        public void BipolarSigmoidTest()
        {
            var a = new Matrix(2, 2);

            a.InRandomize();
            var b = a.Duplicate();

            a = new BipolarSigmoid().Forward(a);
            b.InMap((x) => - 1 + 2 / (1 + Math.Exp(-x)));
            Assert.IsTrue(Math.Abs(a.FrobeniusNorm() - b.FrobeniusNorm()) < 0.1,
                          new BipolarSigmoid().Type().ToString() + " Activation.");
        }
예제 #6
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());
                }
            }
        }
예제 #7
0
파일: Layer.cs 프로젝트: xuan2261/XNet
        public Layer(int nCount, int index, ActivationSettings activationSettings)
        {
            NCount = nCount;

            Index = index;

            ActivationType = activationSettings.Type();

            // Activation Setup
            switch (activationSettings.Type())
            {
            case EActivationType.Invalid:
                Activation = null;
                throw new ArgumentException("Activation Type Invalid.");

            case EActivationType.Arctan:
                Activation = new Arctan();
                break;

            case EActivationType.BinaryStep:
                Activation = new BinaryStep();
                break;

            case EActivationType.BipolarSigmoid:
                Activation = new BipolarSigmoid();
                break;

            case EActivationType.ELU:
                Activation = new ELU((ELUSettings)activationSettings);
                break;

            case EActivationType.HardSigmoid:
                Activation = new HardSigmoid();
                break;

            case EActivationType.HardTanh:
                Activation = new HardTanh();
                break;

            case EActivationType.Identity:
                Activation = new Identity();
                break;

            case EActivationType.Logit:
                Activation = new Logit();
                break;

            case EActivationType.LReLU:
                Activation = new LReLU((LReLUSettings)activationSettings);
                break;

            case EActivationType.Mish:
                Activation = new Mish();
                break;

            case EActivationType.ReLU:
                Activation = new ReLU();
                break;

            case EActivationType.SeLU:
                Activation = new SeLU();
                break;

            case EActivationType.Sigmoid:
                Activation = new Sigmoid();
                break;

            case EActivationType.Softmax:
                Activation = new Softmax();
                break;

            case EActivationType.Softplus:
                Activation = new Softplus();
                break;

            case EActivationType.Softsign:
                Activation = new Softsign();
                break;

            case EActivationType.Tanh:
                Activation = new Tanh();
                break;

            default:
                throw new ArgumentException("Activation Type Invalid.");
            }
        }
예제 #8
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;
        }
예제 #9
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;
            //        }
            //    }
        }
예제 #10
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());
                }
            }
        }