Ejemplo n.º 1
0
        void InitNeuralNet()
        {
            LinearLayer inputLayer = new LinearLayer(1);
            SigmoidLayer hiddenLayer = new SigmoidLayer(neuronCount);
            SigmoidLayer outputLayer = new SigmoidLayer(1);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(learningRate);

            TrainingSet trainingSet = new TrainingSet(inputVectorSize, outVectorSize);
            for (int i = 0; i < curve.Points.Count; i++)
            {
                double xVal = curve.Points[i].X;
                for (double input = xVal - 0.05; input < xVal + 0.06; input += 0.01)
                {
                    trainingSet.Add(new TrainingSample(new double[] { input }, new double[] { curve.Points[i].Y }));
                }
            }

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs args)
                {
                    trainingProgressBar.Value = (int)(args.TrainingIteration * 100d / cycles);
                    Application.DoEvents();
                });
            network.Learn(trainingSet, cycles);
        }
Ejemplo n.º 2
0
        public static void Treinar(string papel, string nomeRedeNeural, List<DadosBE> dadosBE, int numeroNeuronios, double taxaAprendizado, int ciclos, int numeroDivisoesCrossValidation, int shift, double versao)
        {
            if (dadosBE.Count == 0)
                return;

            List<Treinamento> treinamentos = DataBaseUtils.DataBaseUtils.SelecionarTreinamentos_V3(dadosBE, versao);
            treinamentos = treinamentos.Where(trein => trein.DivisaoCrossValidation != shift).ToList();

            int inputLayerCount = treinamentos.First().Input.Count();
            int outputLayerCount = treinamentos.First().Output.Count();

            BackpropagationNetwork network;
            //int numeroNeuronios = 4;
            //double taxaAprendizado = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(inputLayerCount);
            ActivationLayer hiddenLayer = new SigmoidLayer(numeroNeuronios);
            ActivationLayer outputLayer = new SigmoidLayer(outputLayerCount);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(taxaAprendizado);

            TrainingSet trainingSet = new TrainingSet(inputLayerCount, outputLayerCount);
            //foreach (KeyValuePair<double[], double[]> kvp in dadosPorJanelamento)
            //{
            //    trainingSet.Add(new TrainingSample(kvp.Key, kvp.Value));
            //}
            foreach (Treinamento treinamento in treinamentos)
            {
                trainingSet.Add(new TrainingSample(treinamento.Input.ToArray(), treinamento.Output.ToArray()));
            }

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    //trainingProgressBar.Value = (int)(argsNw.TrainingIteration * 100d / cycles);
                    //Application.DoEvents();
                });

            network.Learn(trainingSet, ciclos);
            double erroGeralRede = 0;
            foreach (Treinamento treinamento in treinamentos)
            {
                double[] previsao = network.Run(treinamento.Input.ToArray());
                double erroRede = 1 - Math.Min(previsao.First(), treinamento.Output.First()) / Math.Max(previsao.First(), treinamento.Output.First());
                erroGeralRede += erroRede;
            }
            erroGeralRede = erroGeralRede / treinamentos.Count;

            using (Stream stream = File.Open(diretorioRedes + "\\RedesPrevisaoFinanceira\\" + nomeRedeNeural + ".ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }
        }
Ejemplo n.º 3
0
 public Cell()
 {
     LinearLayer inputLayer = new LinearLayer(INPUT_SIZE);
     SigmoidLayer hiddenLayer = new SigmoidLayer(neuronCount);
     SigmoidLayer outputLayer = new SigmoidLayer(OUTPUT_SIZE);
     new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
     new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
     network = new BackpropagationNetwork(inputLayer, outputLayer);
     setBirthDate();
 }
Ejemplo n.º 4
0
        public TrainingClass(int inputs, int[] hiddenNeuronCount, int outputs, double learningRate)
        {
            LinearLayer inputLayer = new LinearLayer(inputs);
            SigmoidLayer hiddenLayer = new SigmoidLayer(hiddenNeuronCount[0]);
            new BackpropagationConnector(inputLayer, hiddenLayer);
            var lastLayer = hiddenLayer;

            // Deep learning
            for (var i = 1; i < hiddenNeuronCount.Length; i++)
            {
                SigmoidLayer tempLayer = new SigmoidLayer(hiddenNeuronCount[i]);
                new BackpropagationConnector(lastLayer, tempLayer);
                lastLayer = tempLayer;
            }

            SigmoidLayer outputLayer = new SigmoidLayer(outputs);
            new BackpropagationConnector(lastLayer, outputLayer);

            MainWindow.Network = new BackpropagationNetwork(inputLayer, outputLayer);
            MainWindow.Network.SetLearningRate(learningRate);
        }
        private BackpropagationNetwork Treinar(int numeroNeuronios, double taxaAprendizado, int ciclos)
        {
            List<Agencia> agencias = Agencia.PegarTodas();

            List<KeyValuePair<double[], double[]>> dadosEntrada = new List<KeyValuePair<double[], double[]>>();
            foreach (Agencia agencia in agencias)
            {
                KeyValuePair<double[], double[]> kvp = new KeyValuePair<double[], double[]>(new double[] { NormalizarLatitude(agencia.Latitude), NormalizarLongitude(agencia.Longitude) }, ConverterZonaParaArrayDouble(agencia.Zona));
                dadosEntrada.Add(kvp);
            }

            BackpropagationNetwork network;
            //int numeroNeuronios = 4;
            //double taxaAprendizado = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(2);
            ActivationLayer hiddenLayer = new SigmoidLayer(numeroNeuronios);
            ActivationLayer outputLayer = new SigmoidLayer(4);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(taxaAprendizado);

            TrainingSet trainingSet = new TrainingSet(2, 4);
            foreach (KeyValuePair<double[], double[]> kvp in dadosEntrada)
            {
                trainingSet.Add(new TrainingSample(kvp.Key, kvp.Value));
            }

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    //trainingProgressBar.Value = (int)(argsNw.TrainingIteration * 100d / cycles);
                    //Application.DoEvents();
                });

            //network.Learn(trainingSet, ciclos);
            int cicloAtual = ciclos / 5;
            int acertos = 0;
            while (cicloAtual <= ciclos && acertos / agencias.Count < 0.99)
            {
                network.Learn(trainingSet, cicloAtual);

                acertos = 0;
                foreach (Agencia agencia in agencias)
                {
                    double[] input = new double[] { NormalizarLatitude(agencia.Latitude), NormalizarLongitude(agencia.Longitude) };
                    double[] resultado = network.Run(input);
                    if (ConverterArrayDoubleParaZona(resultado) != agencia.Zona)
                        trainingSet.Add(new TrainingSample(input, ConverterZonaParaArrayDouble(agencia.Zona)));
                    else
                        acertos++;
                }

                cicloAtual += ciclos / 5;
            }

            //using (Stream stream = File.Open(System.IO.Directory.GetCurrentDirectory() + "\\" + nomeRedeNeural + ".ndn", FileMode.Create))
            using (Stream stream = File.Open("..\\Pasta\\" + nomeRede + ".ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }

            return network;
        }
Ejemplo n.º 6
0
        public Optimiser(string filename)
        {
            Utils.Logger.Log("Loading stopwatch... ");
            stopWatch = new Stopwatch();
            ResultCounter = new Stopwatch();

            this.filename = filename;
            Utils.Logger.Log("Loading preprocessor parameters from " + filename);
            dataAccess = new DataAccess(filename);
            preprocessor = new Preprocessor();

            preprocessor.ImageSize = new Size(Convert.ToInt32(dataAccess.GetParameter("Master_Width")), Convert.ToInt32(dataAccess.GetParameter("Master_Height")));
            preprocessor.KeepAspectRatio = Convert.ToBoolean(dataAccess.GetParameter("Master_Aspect"));
            preprocessor.ScalingMethod = (ScalingMethods)Convert.ToInt32(dataAccess.GetParameter("Master_Resize"));
            preprocessor.ContrastStretch = Convert.ToBoolean(dataAccess.GetParameter("Filter_Stretch"));
            preprocessor.Histogram = Convert.ToBoolean(dataAccess.GetParameter("Filter_Histo"));
            preprocessor.Gaussian = Convert.ToBoolean(dataAccess.GetParameter("Filter_Gaussian"));
            preprocessor.GaussianStrength = Convert.ToInt32(dataAccess.GetParameter("Filter_BlurStr"));
            preprocessor.ContrastAdjustment = Convert.ToBoolean(dataAccess.GetParameter("Filter_Contrast"));
            preprocessor.ContrastStrength = Convert.ToDecimal(dataAccess.GetParameter("Filter_ContrastStr"));
            preprocessor.Greyscale = Convert.ToBoolean(dataAccess.GetParameter("Filter_Greyscale"));
            preprocessor.Bradley = Convert.ToBoolean(dataAccess.GetParameter("Filter_Bradley"));
            preprocessor.Threshold = Convert.ToBoolean(dataAccess.GetParameter("Filter_Threshold"));
            preprocessor.ThresholdStrength = Convert.ToDecimal(dataAccess.GetParameter("Filter_ThresholdStr"));

            /*
            dataAccess.SetParameter("Opt_Bp_LearningType", cmbLearningRateType.SelectedItem.ToString());
            dataAccess.SetParameter("Opt_Bp_InitialLearnRate", txtInitialRate.Text);
            dataAccess.SetParameter("Opt_Bp_FinalLearnRate", txtFinalRate.Text);
            dataAccess.SetParameter("Opt_Bp_JitterEpoch", txtJitterEpoch.Text);
            dataAccess.SetParameter("Opt_Bp_JitterNoiseLimit", txtJitterNoiseLimit.Text);
            dataAccess.SetParameter("Opt_Bp_MaxIterations", txtMaxIterations.Text);
            dataAccess.SetParameter("Opt_Bp_MinError", txtMinimumError.Text);
            */
            bool usePSO = false;
            bool useBP = false;
            try
            {
                useBP = Convert.ToBoolean(dataAccess.GetParameter("Opt_Bp_Enabled"));
            }
            catch (Exception)
            {
                Utils.Logger.Log("Warning unable to read BP params");
            }
            try
            {
                usePSO = Convert.ToBoolean(dataAccess.GetParameter("Opt_Pso_Enabled"));
            }
            catch (Exception)
            {
                Utils.Logger.Log("Warning unable to read PSO params");
            }

            if (usePSO && useBP)
            {
                throw new NotImplementedException("At this current time you cannot use both BP and PSO");

            }

            InputGroup[] inputGroups = dataAccess.GetInputGroups();
            SourceItem[] sourceItems = dataAccess.GetSourceItems();

            /*
            Utils.Logger.Log("Preprocessing images...");
            foreach (SourceItem item in sourceItems)
            {
                Utils.Logger.Log("Preprocessing item {0} ", item.Filename);
                item.InternalImage = preprocessor.Process((Bitmap)item.InternalImage);
            }
             */

            int total = 0;
            foreach (InputGroup inputGroup in inputGroups)
            {
                if (inputGroup.InputGroupType == InputGroupType.Grid)
                {
                    total += (inputGroup.Segments) * (inputGroup.Segments);
                }
                else
                {
                    total += inputGroup.Segments;
                }

            }

            maxIterations = Convert.ToInt32(dataAccess.GetParameter("Opt_Global_MaxIterations"));
            minError = Convert.ToDouble(dataAccess.GetParameter("Opt_Global_MinError"));
            maxTime = Convert.ToInt32(dataAccess.GetParameter("Opt_Global_MaxTime"));
            results = new float[Convert.ToInt32(dataAccess.GetParameter("Opt_Global_BufferSize"))];

            if (useBP)
            {

                int learningRateFunction = Convert.ToInt32(dataAccess.GetParameter("Opt_Bp_LearningType"));
                double initialLR = Convert.ToDouble(dataAccess.GetParameter("Opt_Bp_InitialLearnRate"));
                double finalLR = Convert.ToDouble(dataAccess.GetParameter("Opt_Bp_FinalLearnRate"));
                int jitterEpoch = Convert.ToInt32(dataAccess.GetParameter("Opt_Bp_JitterEpoch"));
                double jitterNoiseLimit = Convert.ToDouble(dataAccess.GetParameter("Opt_Bp_JitterNoiseLimit"));

                NeuronDotNet.Core.Backpropagation.LinearLayer inputLayer = new NeuronDotNet.Core.Backpropagation.LinearLayer(preprocessor.ImageSize.Width * preprocessor.ImageSize.Height);
                NeuronDotNet.Core.Backpropagation.SigmoidLayer hiddenLayer = new NeuronDotNet.Core.Backpropagation.SigmoidLayer(total);
                hiddenLayer.InputGroups = inputGroups.Length;
                NeuronDotNet.Core.Backpropagation.SigmoidLayer outputLayer = new NeuronDotNet.Core.Backpropagation.SigmoidLayer(1);

                hiddenLayer.Initializer = new NguyenWidrowFunction();

                new BackpropagationConnector(
                    inputLayer,
                    hiddenLayer,
                    inputGroups,
                    preprocessor.ImageSize.Width,
                    preprocessor.ImageSize.Height
                    );

                new BackpropagationConnector(hiddenLayer, outputLayer);

                network = new BackpropagationNetwork(inputLayer, outputLayer);

                switch (learningRateFunction)
                {
                    case 0:
                        network.SetLearningRate(initialLR);
                        break;
                    case 1:
                        network.SetLearningRate(new NeuronDotNet.Core.LearningRateFunctions.ExponentialFunction(initialLR, finalLR));//exp
                        break;
                    case 2:
                        network.SetLearningRate(new NeuronDotNet.Core.LearningRateFunctions.HyperbolicFunction(initialLR, finalLR));//hyp
                        break;
                    case 3:
                        network.SetLearningRate(new NeuronDotNet.Core.LearningRateFunctions.LinearFunction(initialLR, finalLR));//lin
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("The learning rate index is out of range.\n");
                }
                network.JitterEpoch = jitterEpoch;
                network.JitterNoiseLimit = jitterNoiseLimit;

            }

            if (usePSO)
            {
                double minP = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_MinP"));
                double maxP = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_MaxP"));
                double minI = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_MinI"));
                double maxI = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_MaxI"));
                double quant = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_Quant"));
                double vMax = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_vMax"));

                int clamping = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_Clamping"));
                int initLinks = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_InitLinks"));
                int randomness = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_Randomness"));
                int randOrder = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_ParticleOrder"));
                int rotation = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_Rotation"));

                int dimensions = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_Dimensions"));
                int swarmSize = Convert.ToInt32(dataAccess.GetParameter("Opt_Pso_Particles"));
                double k = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_k"));
                double p = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_p"));
                double w = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_w"));
                double c = Convert.ToDouble(dataAccess.GetParameter("Opt_Pso_c"));

                Parameters param = new Parameters();
                param.vMax = vMax;

                param.clamping = clamping;
                // 0 => no clamping AND no evaluation. WARNING: the program
                // 				may NEVER stop (in particular with option move 20 (jumps)) 1
                // *1 => classical. Set to bounds, and velocity to zero

                param.initLink = initLinks; // 0 => re-init links after each unsuccessful iteration
                // 1 => re-init links after each successful iteration

                param.rand = randomness; // 0 => Use KISS as random number generator.
                // Any other value => use the system one

                param.randOrder = randOrder; // 0 => at each iteration, particles are modified
                //     always according to the same order 0..S-1
                //*1 => at each iteration, particles numbers are
                //		randomly permutated
                param.rotation = rotation;
                // WARNING. Experimental code, completely valid only for dimension 2
                // 0 =>  sensitive to rotation of the system of coordinates
                // 1 => non sensitive (except side effects),
                // 			by using a rotated hypercube for the probability distribution
                //			WARNING. Quite time consuming!

                param.stop = 0;	// Stop criterion
                // 0 => error < pb.epsilon
                // 1 => eval >= pb.evalMax
                // 2 => ||x-solution|| < pb.epsilon

                // ===========================================================
                // RUNs

                // Initialize some objects
                //pb = new Problem(function);

                // You may "manipulate" S, p, w and c
                // but here are the suggested values
                param.S = swarmSize;
                if (param.S > 910) param.S = 910;

                param.K = (int)k;
                param.p = p;
                // (to simulate the global best PSO, set param.p=1)
                //param.p=1;

                param.w = w;
                param.c = c;

                NeuronDotNet.Core.PSO.LinearLayer inputLayer = new NeuronDotNet.Core.PSO.LinearLayer(preprocessor.ImageSize.Width * preprocessor.ImageSize.Height);
                NeuronDotNet.Core.PSO.SigmoidLayer hiddenLayer = new NeuronDotNet.Core.PSO.SigmoidLayer(total);
                hiddenLayer.InputGroups = inputGroups.Length;
                NeuronDotNet.Core.PSO.SigmoidLayer outputLayer = new NeuronDotNet.Core.PSO.SigmoidLayer(1);

                hiddenLayer.Initializer = new NguyenWidrowFunction();

                new PSOConnector(
                    inputLayer,
                    hiddenLayer,
                    inputGroups,
                    preprocessor.ImageSize.Width,
                    preprocessor.ImageSize.Height
                    );

                new PSOConnector(hiddenLayer, outputLayer);

                PSONetwork n = new PSONetwork(inputLayer, outputLayer);

                n.PsoParameters = param;
                n.PsoProblem.MaxI = maxI;
                n.PsoProblem.MinI = minI;
                n.PsoProblem.MaxP = maxP;
                n.PsoProblem.MinP = minP;
                n.PsoProblem.Quantisation = quant;
                network = n;

            }

            set = new TrainingSet(preprocessor.ImageSize.Width * preprocessor.ImageSize.Height, 1);
            foreach (SourceItem item in sourceItems)
            {

                double[] weights = Utils.getImageWeights(item.InternalImage, inputGroups);
                set.Add(new TrainingSample(weights, new double[] { (double)item.SampleType }));

            }
            network.EndEpochEvent += new TrainingEpochEventHandler(network_EndEpochEvent);
            network.Initialize();
        }
Ejemplo n.º 7
0
 //Get neural network
 public virtual NeuronDotNet.Core.Network getNeural()
 {
     LinearLayer inputLayer = new LinearLayer(23);
     SigmoidLayer outputLayer = new SigmoidLayer(100);
     return new BackpropagationNetwork(inputLayer, outputLayer);
 }
Ejemplo n.º 8
0
        private void Train(object sender, EventArgs e)
        {
            EnableControls(false);
            if (!int.TryParse(txtCycles.Text.Trim(), out cycles)) { cycles = 5000; }
            if (!double.TryParse(txtLearningRate.Text.Trim(), out learningRate)) { learningRate = 0.25d; }
            if (!int.TryParse(txtNeuronCount.Text.Trim(), out neuronCount)) { neuronCount = 3; }

            if (cycles < 1) { cycles = 1; }
            if (learningRate < 0.01) { learningRate = 0.01; }
            if (neuronCount < 1) { neuronCount = 1; }

            txtNeuronCount.Text = neuronCount.ToString();
            txtCycles.Text = cycles.ToString();
            txtLearningRate.Text = learningRate.ToString();

            errorList = new double[cycles];
            InitGraph();

            LinearLayer inputLayer = new LinearLayer(2);
            SigmoidLayer hiddenLayer = new SigmoidLayer(neuronCount);
            SigmoidLayer outputLayer = new SigmoidLayer(1);
            new BackpropagationConnector(inputLayer, hiddenLayer);
            new BackpropagationConnector(hiddenLayer, outputLayer);
            xorNetwork = new BackpropagationNetwork(inputLayer, outputLayer);
            xorNetwork.SetLearningRate(learningRate);

            TrainingSet trainingSet = new TrainingSet(2, 1);
            trainingSet.Add(new TrainingSample(new double[2] { 0d, 0d }, new double[1] { 0d }));
            trainingSet.Add(new TrainingSample(new double[2] { 0d, 1d }, new double[1] { 1d }));
            trainingSet.Add(new TrainingSample(new double[2] { 1d, 0d }, new double[1] { 1d }));
            trainingSet.Add(new TrainingSample(new double[2] { 1d, 1d }, new double[1] { 0d }));

            double max = 0d;

            xorNetwork.EndEpochEvent +=
            delegate(object network, TrainingEpochEventArgs args)
            {
                errorList[args.TrainingIteration] = xorNetwork.MeanSquaredError;
                max = Math.Max(max, xorNetwork.MeanSquaredError);
                progressBar.Value = (int)(args.TrainingIteration * 100d / cycles);
            };

            xorNetwork.Learn(trainingSet, cycles);
            double[] indices = new double[cycles];
            for (int i = 0; i < cycles; i++) { indices[i] = i; }

            lblTrainErrorVal.Text = xorNetwork.MeanSquaredError.ToString("0.000000");

            LineItem errorCurve = new LineItem("Error Dynamics", indices, errorList, Color.Tomato, SymbolType.None, 1.5f);
            errorGraph.GraphPane.YAxis.Scale.Max = max;
            errorGraph.GraphPane.CurveList.Add(errorCurve);
            errorGraph.Invalidate();
            EnableControls(true);
        }
 private void createLayers()
 {
     inputLayer = new LinearLayer(size);
     hiddenLayer = new SigmoidLayer(size);
     outputLayer = new SigmoidLayer(size);
 }
Ejemplo n.º 10
0
 public void Tangensial()
 {
     LinearLayer entrada = new LinearLayer(4);
     TanhLayer oculta = new TanhLayer(3);
     TanhLayer salida = new TanhLayer(3);
     new BackpropagationConnector(entrada, oculta);
     new BackpropagationConnector(oculta, salida);
     red = new BackpropagationNetwork(entrada, salida);
     red.SetLearningRate(0.3);
     datos = new TrainingSet(4, 3);
 }
Ejemplo n.º 11
0
 public void Sine()
 {
     LinearLayer entrada = new LinearLayer(4);
     SineLayer oculta = new SineLayer(3);
     //NeuronDotNet.Core.Backpropagation.
     SineLayer salida = new SineLayer(3);
     new BackpropagationConnector(entrada, oculta);
     new BackpropagationConnector(oculta, salida);
     red = new BackpropagationNetwork(entrada, salida);
     red.SetLearningRate(0.3);
     datos = new TrainingSet(4, 3);
 }
Ejemplo n.º 12
0
        static void Training(List<double> dadosTreinamento, int janela, int ciclos)
        {
            if (dadosTreinamento.Count < janela)
                return;

            /*Cria um mapeamento de entradas para saida com o janelamento informado*/
            //List<KeyValuePair<double[], double>> dadosPorJanelamento = new List<KeyValuePair<double[], double>>();
            //for (int i = 0; i < dadosTreinamento.Count - janela - 1; i += janela + 1)
            //{
            //    dadosPorJanelamento.Add(new KeyValuePair<double[], double>(dadosTreinamento.Skip(i).Take(janela).ToArray(), dadosTreinamento.Skip(i).Take(janela + 1).First()));
            //}
            List<KeyValuePair<double[], double>> dadosPorJanelamento = GetCotacoesPorJanelamento(dadosTreinamento, janela);
            /*Cria um mapeamento de entradas para saida com o janelamento informado*/

            BackpropagationNetwork network;
            int neuronCount = 4;
            double learningRate = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(janela);
            SigmoidLayer hiddenLayer = new SigmoidLayer(neuronCount);
            SigmoidLayer outputLayer = new SigmoidLayer(1);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(learningRate);

            TrainingSet trainingSet = new TrainingSet(janela, 1);
            foreach (KeyValuePair<double[], double> kvp in dadosPorJanelamento)
            {
                trainingSet.Add(new TrainingSample(kvp.Key, new double[] { kvp.Value }));
            }

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    //trainingProgressBar.Value = (int)(argsNw.TrainingIteration * 100d / cycles);
                    //Application.DoEvents();
                });

            bool correct = false;
            int currentCycles = ciclos / 5;
            while (correct == false && currentCycles <= ciclos)
            {
                network.Learn(trainingSet, currentCycles);
                foreach (KeyValuePair<double[], double> kvp in dadosPorJanelamento)
                {
                    double previsao = network.Run(kvp.Key)[0];
                    if (Math.Abs(kvp.Value - previsao) > (kvp.Value / 100 * 0.5))//Verifica se houve mais de 5% de erro
                    {
                        correct = false;
                        trainingSet.Add(new TrainingSample(kvp.Key, new double[] { kvp.Value }));
                    }
                    else
                        correct = true;
                }
                currentCycles += ciclos / 5;
            }

            using (Stream stream = File.Open(System.IO.Directory.GetCurrentDirectory() + @"\network.ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }
        }
Ejemplo n.º 13
0
        public static double Treinar(string nomeRedeNeural, List<Treinamento> treinamentos, int numeroNeuronios, double taxaAprendizado, int ciclos)
        {
            if (treinamentos.Count == 0)
                return -1;

            int inputLayerCount = treinamentos.First().Input.Count();
            int outputLayerCount = treinamentos.First().Output.Count();

            BackpropagationNetwork network;
            //int numeroNeuronios = 4;
            //double taxaAprendizado = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(inputLayerCount);
            //ActivationLayer hiddenLayer = new SigmoidLayer(numeroNeuronios);
            ActivationLayer outputLayer = new LinearLayer(outputLayerCount);
            outputLayer.UseFixedBiasValues = true;

            new BackpropagationConnector(inputLayer, outputLayer).Initializer = new RandomFunction(0, 0.3d);
            //new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(taxaAprendizado);

            TrainingSet trainingSet = new TrainingSet(inputLayerCount, outputLayerCount);
            foreach (Treinamento treinamento in treinamentos)
            {
                trainingSet.Add(new TrainingSample(treinamento.Input.ToArray(), treinamento.Output.ToArray()));
            }

            double lastMeanSquareError = 1;
            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    if (argsNw.TrainingIteration > 0 && argsNw.TrainingIteration % 100 == 0)
                    {
                        double erroAt = Convert.ToDouble(senderNetwork.GetType().GetProperty("MeanSquaredError").GetValue(senderNetwork, null));
                        //Se o erro cresceu ou se o erro melhorou menos do que 0.05%, parar o aprendizado
                        if (erroAt > lastMeanSquareError || Math.Abs(lastMeanSquareError - erroAt) < (lastMeanSquareError / 100))//0.1% de melhora..
                        {
                            network.StopLearning();
                        }
                        else
                            lastMeanSquareError = erroAt;
                    }
                });

            network.Learn(trainingSet, ciclos);

            int numeroAcertos = 0;
            foreach (Treinamento treinamento in treinamentos)
            {
                double[] previsao = network.Run(treinamento.Input.ToArray());
                //double erroRede = 1 - Math.Min(previsao.First(), treinamento.Output.First()) / Math.Max(previsao.First(), treinamento.Output.First());
                if (ValoresMaximosNoMesmoIndice(previsao, treinamento.Output.ToArray()))
                {
                    numeroAcertos++;
                }
            }
            double acertoRede = numeroAcertos / treinamentos.Count * 100;

            using (Stream stream = File.Open(diretorioRedes + nomeRedeNeural + ".ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }

            return acertoRede;
        }
        /// <summary>
        /// Treina a rede que diz qual é a melhor configuração de rede para um papel em um dia
        /// </summary>
        /// <param name="nomeRede"></param>
        /// <param name="trainingSet"></param>
        private static void TreinarRedeDiaria(string nomeRede, TrainingSet trainingSet)
        {
            BackpropagationNetwork network;
            int numeroNeuronios = 4;
            double taxaAprendizado = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(trainingSet.InputVectorLength);
            ActivationLayer hiddenLayer = new SigmoidLayer(numeroNeuronios);
            ActivationLayer outputLayer = new SigmoidLayer(trainingSet.OutputVectorLength);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(taxaAprendizado);

            //TrainingSet trainingSet = new TrainingSet(janelaEntrada, janelaSaida);
            //foreach (KeyValuePair<double[], double[]> kvp in dadosPorJanelamento)
            //{
            //    trainingSet.Add(new TrainingSample(kvp.Key, kvp.Value));
            //}

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    //trainingProgressBar.Value = (int)(argsNw.TrainingIteration * 100d / cycles);
                    //Application.DoEvents();
                });

            bool erroAceito = false;
            int cicloAtual = ciclos / 5;
            while (erroAceito == false && cicloAtual <= ciclos)
            {
                network.Learn(trainingSet, cicloAtual);
                foreach (TrainingSample treinamento in trainingSet.TrainingSamples.Distinct())
                {
                    double[] previsao = network.Run(treinamento.InputVector);
                    double erroAcumulado = 0;
                    for (int indRede = 0; indRede < trainingSet.OutputVectorLength; indRede++)
                    {
                        erroAcumulado += 1 - Math.Min(previsao[indRede], treinamento.OutputVector[indRede]) / Math.Max(previsao[indRede], treinamento.OutputVector[indRede]);
                    }
                    double erroMedio = erroAcumulado / trainingSet.TrainingSampleCount;

                    if (erroMedio > 0.3)//Verifica se houve mais de 3% de erro
                    {
                        erroAceito = false;
                        trainingSet.Add(treinamento);
                    }
                    else
                        erroAceito = erroAceito && true;
                }
                cicloAtual += ciclos / 5;
            }

            using (Stream stream = File.Open(diretorioRedesCaptacao + nomeRede + ".ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }
        }
Ejemplo n.º 15
0
        //public void TreinarRedeNeural(List<DadosBE> dadosBE, string papel = "PETR4")
        //{
        //}
        public static void Treinar(string papel, string nomeRedeNeural, List<DadosBE> dadosBE, int janelaEntrada, int numeroNeuronios, double taxaAprendizado, int ciclos, int numeroDivisoesCrossValidation, int shift)
        {
            if (dadosBE.Count < janelaEntrada)
                return;

            /*//inputLayerCount será a soma de 2+n ao valor da janela de entrada, onde n é o (TOE(tamanho original da entrada) - 5) / 5(arredondado para baixo). Ou seja, para cada 5 dias informados além de 5 iniciais, +1 cotação do dolar será informada
            //EX: passaremos a cotação do dia 1 ao dia 9 (9 dias) para prever o decimo dia. Portanto:
            //-TOE - 5 = 4.
            //4 / 5 = 0.8, arredondando para baixo, 0.
            //portanto apenas 2 cotações do dolar serão informadas: a do dia 1 e a do dia 9
            //de 10 a 14 dias de entrada, 3 cotaçoes do dolar. de 15 a 19, 4 cotações do dolar e assim por diante.
            int inputLayerCount = janelaEntrada + 2;

            //Somamos 2 a janela de entrada pois informaremos também a estação do ano e o valor de bollinger
            inputLayerCount += 2;

            //O primeiro valor será a cotação do ativo para o dia seguinte e o segundo valor a cotação do dolar para o dia seguinte
            int outputLayerCount = 2;
            */

            List<Treinamento> treinamentos = DataBaseUtils.DataBaseUtils.SelecionarTreinamentos_V2(dadosBE, janelaEntrada, 1);
            treinamentos = treinamentos.Where(trein => trein.DivisaoCrossValidation != shift).ToList();

            int inputLayerCount = treinamentos.First().Input.Count();
            int outputLayerCount = treinamentos.First().Output.Count();

            BackpropagationNetwork network;
            //int numeroNeuronios = 4;
            //double taxaAprendizado = 0.25d;

            ActivationLayer inputLayer = new LinearLayer(inputLayerCount);
            ActivationLayer hiddenLayer = new SigmoidLayer(numeroNeuronios);
            ActivationLayer outputLayer = new SigmoidLayer(outputLayerCount);
            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(0d, 0.3d);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(0d, 0.3d);
            network = new BackpropagationNetwork(inputLayer, outputLayer);
            network.SetLearningRate(taxaAprendizado);

            TrainingSet trainingSet = new TrainingSet(inputLayerCount, outputLayerCount);
            //foreach (KeyValuePair<double[], double[]> kvp in dadosPorJanelamento)
            //{
            //    trainingSet.Add(new TrainingSample(kvp.Key, kvp.Value));
            //}
            foreach (Treinamento treinamento in treinamentos)
            {
                trainingSet.Add(new TrainingSample(treinamento.Input.ToArray(), treinamento.Output.ToArray()));
            }

            network.EndEpochEvent += new TrainingEpochEventHandler(
                delegate(object senderNetwork, TrainingEpochEventArgs argsNw)
                {
                    //trainingProgressBar.Value = (int)(argsNw.TrainingIteration * 100d / cycles);
                    //Application.DoEvents();
                });

            bool erroAceito = false;
            int cicloAtual = ciclos / 2;
            while (erroAceito == false && cicloAtual <= ciclos)
            {
                erroAceito = true;
                network.Learn(trainingSet, cicloAtual);
                double erroGeralRede = 0;
                foreach (Treinamento treinamento in treinamentos)
                {
                    double[] previsao = network.Run(treinamento.Input.ToArray());
                    double erroRede = 1 - Math.Min(previsao.First(), treinamento.Output.First()) / Math.Max(previsao.First(), treinamento.Output.First());
                    erroGeralRede += erroRede;
                    if (erroRede > 0.01)//Verifica se houve mais de 1% de erro
                    {
                        trainingSet.Add(new TrainingSample(treinamento.Input.ToArray(), treinamento.Output.ToArray()));
                    }
                }
                erroGeralRede = erroGeralRede / treinamentos.Count;
                if (erroGeralRede > 0.01)
                    erroAceito = false;
                cicloAtual += ciclos / 2;
            }

            using (Stream stream = File.Open(diretorioRedes + "\\RedesPrevisaoFinanceira\\" + nomeRedeNeural + ".ndn", FileMode.Create))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, network);
            }
        }
Ejemplo n.º 16
0
        //Initialize agent's parameters
        public override void agent_init(char type, bool policy, string agentName, int inputCount)
        {
            //Initialize neural net
            LinearLayer inputLayer = new LinearLayer(inputCount + 1);
            SigmoidLayer hiddenLayer = new SigmoidLayer(150);
            LinearLayer outputLayer = new LinearLayer(1);

            new BackpropagationConnector(inputLayer, hiddenLayer).Initializer = new RandomFunction(-0.5, 0.5);
            new BackpropagationConnector(hiddenLayer, outputLayer).Initializer = new RandomFunction(-0.5, 0.5);

            this.network = new BackpropagationNetwork(inputLayer, outputLayer);

            this.network.SetLearningRate(0.2);
            this.network.Initialize();

            #region Initialize_parameters

            this.name = agentName;
            this.id = Int32.Parse(agentName.Last().ToString());

            this.agentType = type;
            this.policyFrozen = policy;

            if (policy)
            {
                this.epsilon = 0;
                this.alpha = 0;
            }
            else
            {
                this.epsilon = 0.5;
                this.alpha = 0.2;
            }

            this.gamma = 0.95;
            this.lamda = 0.8;

            currentEpoch = 1;

            initParams();

            #endregion Initialize_parameters
        }