Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple training methods to train a ANN with Encog

I would like to know if training a feed forward neural network with Genetic Algorithms, Particle Swarm Optimization and Simulated Annealing before using resilient propagation training does improve the result.

Here is the code I am using:

                    CalculateScore score = new TrainingSetScore(trainingSet);
                    StopTrainingStrategy stop = new StopTrainingStrategy();
                    StopTrainingStrategy stopGA = new StopTrainingStrategy();
                    StopTrainingStrategy stopSIM = new StopTrainingStrategy();
                    StopTrainingStrategy stopPSO = new StopTrainingStrategy();

                    Randomizer randomizer = new NguyenWidrowRandomizer();
                    //Backpropagation train = new Backpropagation((BasicNetwork) network, trainingSet, 0.2, 0.1);
                    //  LevenbergMarquardtTraining train = new LevenbergMarquardtTraining((BasicNetwork) network, trainingSet);
                    int population = 500;
                    MLTrain trainGA =  new MLMethodGeneticAlgorithm(new MethodFactory(){
                        @Override
                        public MLMethod factor() {
                            final BasicNetwork result = createNetwork();
                            ((MLResettable)result).reset();
                            return result;
                        }}, score,population);


                    Date dStart = new Date();

                    int epochGA = 0;
                    trainGA.addStrategy(stopGA);
                    do{
                        trainGA.iteration();
                        if(writeOnStdOut)
                            System.out.println("Epoch Genetic #" + epochGA + " Error:" + trainGA.getError());
                        epochGA++;//0000001
                        previousError = trainGA.getError();
                        Date dtemp = new Date();
                        totsecs = ((double)(dtemp.getTime()-dStart.getTime())/1000);
                    } while(previousError > maximumAcceptedErrorTreshold && epochGA < (maxIterations/5) && !stopGA.shouldStop()  && totsecs < (secs/3));

                    NeuralPSO trainPSO = new NeuralPSO((BasicNetwork) network, randomizer, score, 100);

                    int epochPSO = 0;
                    trainPSO.addStrategy(stopPSO);
                     dStart = new Date();
                    do{
                        trainPSO.iteration();
                        if(writeOnStdOut)
                            System.out.println("Epoch Particle Swarm #" + epochPSO + " Error:" + trainPSO.getError());
                        epochPSO++;//0000001
                        previousError = trainPSO.getError();
                        Date dtemp = new Date();
                        totsecs = ((double)(dtemp.getTime()-dStart.getTime())/1000);
                    } while(previousError > maximumAcceptedErrorTreshold && epochPSO < (maxIterations/5) && !stopPSO.shouldStop() && totsecs < (secs/3));

                    MLTrain trainSIM = new NeuralSimulatedAnnealing((MLEncodable) network, score, startTemperature, stopTemperature, cycles);

                    int epochSA = 0;
                    trainSIM.addStrategy(stopSIM);
                    dStart = new Date();
                    do{
                        trainSIM.iteration();
                        if(writeOnStdOut)
                            System.out.println("Epoch Simulated Annealing #" + epochSA + " Error:" + trainSIM.getError());
                        epochSA++;//0000001
                        previousError = trainSIM.getError();
                        Date dtemp = new Date();
                        totsecs = ((double)(dtemp.getTime()-dStart.getTime())/1000);
                    } while(previousError > maximumAcceptedErrorTreshold && epochSA < (maxIterations/5) && !stopSIM.shouldStop() && totsecs < (secs/3));




                    previousError = 0;
                    BasicTraining train = getTraining(method,(BasicNetwork) network, trainingSet);


                    //train.addStrategy(new Greedy());
                    //trainAlt.addStrategy(new Greedy());
                    HybridStrategy strAnneal = new HybridStrategy(trainSIM);

                    train.addStrategy(strAnneal);
                    //train.addStrategy(strGenetic);
                    //train.addStrategy(strPSO);

                    train.addStrategy(stop);
                    // 
                    //  Backpropagation train = new Backpropagation((ContainsFlat) network, trainingSet, 0.7, 0.3);
                    dStart = new Date();

                    int epoch = 1;

                    do {
                        train.iteration();
                        if(writeOnStdOut)
                            System.out.println("Epoch #" + epoch + " Error:" + train.getError());
                        epoch++;//0000001
                        if(Math.abs(train.getError()-previousError)<0.0000001) iterationWithoutImprovement++; else iterationWithoutImprovement = 0;
                        previousError = train.getError();

                        Date dtemp = new Date();
                        totsecs = ((double)(dtemp.getTime()-dStart.getTime())/1000);
                    } while(previousError > maximumAcceptedErrorTreshold && epoch < maxIterations && !stop.shouldStop() && totsecs < secs);//&& iterationWithoutImprovement < maxiter);

As you can see is a sequence of training algorithms that should improve the overall training.

Please let me know if it makes sense and if the code is correct. It seems to be working but I want to be sure because sometimes I see that the progress made by GA are reset from PSO.

Thanks

like image 619
vincenzodentamaro Avatar asked Nov 09 '22 20:11

vincenzodentamaro


1 Answers

It seems logical, however it will not work.

With the default parameters of the RPROP, this sequence will not likely work. The reason why is that after your previous training the weights of the neural network will be near a local optimum. Because of the nearness to a local optimum only SMALL changes to the weights will move nearer to the optimum (lower the error rate). By default RPROP uses an initialUpdate value of 0.1 across the weight matrix. This is a huge value for a network so close to an optimum. You are "unleashing a bull in a china shop at this point". The first iteration will move the network far from the optimum and will essentially begin a new global search.

Lowering the initialUpdate value SHOULD help. I am not sure by how much. You might want to look at the average RPROP weight update values for a train with your data to get an idea. Or try setting it really small and working your way back up.

like image 69
JeffHeaton Avatar answered Nov 15 '22 11:11

JeffHeaton