📄 levenbergmarquardt.java
字号:
/* * $RCSfile: LevenbergMarquardt.java,v $ * $Revision: 1.16 $ * $Date: 2005/05/05 02:03:48 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Brasília * * This file is part of NeuralNetworkToolkit. * * NeuralNetworkToolkit is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NeuralNetworkToolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NeuralNetworkToolkit; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 - USA. */package neuralnetworktoolkit.methods.gradientbased.quasinewton.lm;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintStream;import neuralnetworktoolkit.*;import neuralnetworktoolkit.debugger.mlp.MLPDebugger;import neuralnetworktoolkit.math.NeuralMath;import neuralnetworktoolkit.methods.*;import neuralnetworktoolkit.neuralnetwork.*;/** * An implementation of Levenberg-Marquardt training method. This * class implements <i>complete</i> and <i>incomplete</i> * Levenberg-Marquardt methods. See reference documentation for * details about two methods. * * @@version $Revision: 1.16 $ - $Date: 2005/05/05 02:03:48 $ * * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gonçalves</a> * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> */public class LevenbergMarquardt extends LevenbergMarquardtModel { /** * * */ public LevenbergMarquardt() { super(); } /* (non-Javadoc) * @see neuralnetworktoolkit.methods.ITrainingMethod#train(neuralnetworktoolkit.INeuralNetwork, int[]) */ public StatisticalResults train(INeuralNetwork neuralNetwork, TrainingParameters parameters) { double[] diagonal; debugger = new MLPDebugger(neuralNetwork); LevenbergMarquardtParameters param; param = (LevenbergMarquardtParameters) parameters; numberOfSynapses = neuralNetwork.numberOfSynapses(); errorGoal = param.getError(); lmKind = param.getLmKind(); maximumNumberOfIterations = param.getMaxIterations(); inicio = System.currentTimeMillis(); do { /*if (numberOfIterations == 315) { try { System.setOut(new PrintStream(new File("debidenf.txt"))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ if (this.errorDiminished) { jtXjAndJtXerrorAndError = calculateJacobianTxJacobianAndJacobianTxError( neuralNetwork, param.getInputs(), param.getOutputs(), 50); totalErrorEnergy = jtXjAndJtXerrorAndError.getError(); jacobianTxJacobian = jtXjAndJtXerrorAndError.getJtXj(); jacobianTxError = jtXjAndJtXerrorAndError.getJtXerror(); //System.out.println("--->JacobianT X Jacobian"); //neuralMath.printMatrix(jacobianTxJacobian); //System.out.println("--->JacobianT X Error"); //neuralMath.printMatrix(jacobianTxError); } if (lmKind == LevenbergMarquardt.COMPLETE_LM) { diagonal = NeuralMath.diagonalArray(jacobianTxJacobian); diagonal = NeuralMath.constantTimesArray(lambda, diagonal); hessian = NeuralMath.matrixSumDiagonal(jacobianTxJacobian, diagonal); } else { hessian = NeuralMath.matrixSumConstantDiagonal(lambda, jacobianTxJacobian); } /*System.out.println("--- Hessian = Soma JacobianT X Jacobian + diagonal"); NeuralMath.printMatrix(hessian);*/ hessian = NeuralMath.inverseMatrix(hessian); /*System.out.println("--- Hessian Inverse"); NeuralMath.printMatrix(hessian);*/ deltaW = NeuralMath.matrixProduct(hessian, jacobianTxError); deltaW = NeuralMath.constantTimesMatrix(-1, deltaW); // TODO keep this? /*System.out.println("Matriz Antes"); NeuralMath.printMatrix(deltaW);*/ //testDeltaW(deltaW); /*System.out.println("Matriz Depois"); NeuralMath.printMatrix(deltaW);*/ neuralNetwork.updateWeights(deltaW); instantaneousError = calculateTotalError(neuralNetwork, param.getInputs(), param.getOutputs()); if (instantaneousError < totalErrorEnergy) { //System.out.println("---> - O erro diminuiu "+ lambda); lambda = lambda / multiplier; totalErrorEnergy = instantaneousError; this.errorDiminished = true; } else { //System.out.println("---> + O erro aumentou " + lambda); deltaW = NeuralMath.constantTimesMatrix(-1, deltaW); neuralNetwork.updateWeights(deltaW); lambda = lambda * multiplier; this.errorDiminished = false; } numberOfIterations++; if (numberOfIterations % /*(maximumNumberOfIterations/100)*/1 == 0) { System.out.println("Numero de Iteracoes: " + numberOfIterations); System.out.println("Erro atual: " + totalErrorEnergy); } //debugger.printNetworkState(); } while (( (totalErrorEnergy / param.getInputs().length) > errorGoal) && (numberOfIterations < maximumNumberOfIterations)&&(goAhead = true)); fim = System.currentTimeMillis(); neuralNetwork.setError( totalErrorEnergy/(param.getInputs().length) ); results.setNumberIterations(numberOfIterations); numberOfIterations = 0; results.setError(totalErrorEnergy / param.getInputs().length); results.setTrainingTime((fim - inicio)/1000); return results; } //train() } //LevenbergMarquardt
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -