📄 levenbergmarquardtmodel.java
字号:
/* * $RCSfile: LevenbergMarquardtModel.java,v $ * $Revision: 1.7 $ * $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 neuralnetworktoolkit.*;import neuralnetworktoolkit.debugger.mlp.MLPDebugger;import neuralnetworktoolkit.methods.gradientbased.quasinewton.JacobianTxJacobianAndJacobianTxErrorAndError;import neuralnetworktoolkit.methods.gradientbased.quasinewton.QuasiNewton;import neuralnetworktoolkit.neuralnetwork.*;/** * * * * @version $Revision: 1.7 $ - $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 abstract class LevenbergMarquardtModel extends QuasiNewton { public static final int COMPLETE_LM = 0; public static final int INCOMPLETE_LM = 1; public static final int STANDARD_MULTIPLIER = 10; public static final double STANDARD_LAMBDA = 0.01; public double lambda = STANDARD_LAMBDA; public int multiplier = STANDARD_MULTIPLIER; protected boolean goAhead = true; protected boolean errorDiminished = true; protected StatisticalResults results; protected int inputSize; protected int outputSize = 0; protected int lmKind; protected int numberOfIterations; protected int maximumNumberOfIterations; protected double totalErrorEnergy; protected double instantaneousError; protected int numberOfSynapses; protected double errorGoal; protected double[][] jacobianTxJacobian; protected double[][] jacobianTxError; protected double[][] hessian; protected double[][] deltaW; double inicio, fim; protected JacobianTxJacobianAndJacobianTxErrorAndError jtXjAndJtXerrorAndError; /** * * */ public LevenbergMarquardtModel() { this.results = new StatisticalResults(); this.inputSize = 0; this.lmKind = 0; this.numberOfIterations = 0; this.maximumNumberOfIterations = MAX_ITERATIONS; this.totalErrorEnergy = 0; this.instantaneousError = 1000; } /* (non-Javadoc) * @see neuralnetworktoolkit.methods.gradientbased.quasinewton.QuasiNewton#calculateTotalError(neuralnetworktoolkit.INeuralNetwork, double[][], double[][]) */ public double calculateTotalError(INeuralNetwork neuralNetwork, double[][] instanceSet, double[][] outputs) { double error = 0; double actualOutput = 0; for (int i = 0; i < instanceSet.length; i++) { actualOutput = 0; neuralNetwork.inputLayerSetup(instanceSet[i]); neuralNetwork.propagateInput(); actualOutput = neuralNetwork.retrieveFinalResults()[0]; //for (int j = 0; j < outputs[0].length; j++) { error = error + (outputs[i][0] - actualOutput) * (outputs[i][0] - actualOutput); //} //debugger.printNetworkState(); } return error; } //calculateTotalError() /* (non-Javadoc) * @see neuralnetworktoolkit.methods.gradientbased.quasinewton.QuasiNewton#calculateNeuronDeltas(neuralnetworktoolkit.INeuralNetwork) */ public void calculateNeuronDeltas(INeuralNetwork neuralNetwork) { // TODO Erase comented instrumentation code. double inputValue = 0; double wXdelta = 0; if (neuralNetwork.isDynamic() == false) { if (neuralNetwork.isMultiConexed() == true) { //System.out.println("Calculando delta para saida:"); ILayer l1 = neuralNetwork.getLayer(neuralNetwork.getNetworkSize() - 1); ILayer l2 = neuralNetwork.getLayer(neuralNetwork.getNetworkSize() - 2); for (int k = 0; k < neuralNetwork .getLayer(neuralNetwork.getNetworkSize() - 1) .getLayerSize(); k++) { inputValue = 0; // Calculates the inputValue for the Output Layer. for (int j = 0; j < neuralNetwork .getLayer(neuralNetwork.getNetworkSize() - 2) .getLayerSize(); j++) { inputValue = inputValue + l2.getNeuron(j).getOutputValue() * l1.getWeight(j, k); } inputValue = inputValue + l1.getBias(k); l1.getNeuron(k).setDelta( -l1 .getNeuron(k) .getActivationFunction() .functionDerivative( inputValue)); } for (int i = (neuralNetwork.getNetworkSize() - 2); i >= 0; i--) { //System.out.println("Calculando delta para interna: " + i); for (int j = 0; j < neuralNetwork.getLayer(i).getLayerSize(); j++) { inputValue = 0; wXdelta = 0; // Calculates the respective weight times delta of the forward layer. for (int k = 0; k < neuralNetwork.getLayer(i + 1).getLayerSize(); k++) { wXdelta = wXdelta + neuralNetwork.getLayer(i + 1).getWeight( j, k) * neuralNetwork .getLayer(i + 1) .getNeuron(k) .getDelta(); } //System.out.println("passei no delta"); if (i>0) { //System.out.println("fora da estatica"); for (int a = 0; a < neuralNetwork .getLayer(i - 1) .getLayerSize(); a++) { inputValue = inputValue + neuralNetwork .getLayer(i - 1) .getNeuron(a) .getOutputValue() * neuralNetwork.getLayer( i).getWeight( a, j); } inputValue = inputValue + neuralNetwork.getLayer(i).getBias(j); } else { //System.out.println("na estatica"); for (int a = 0; a < neuralNetwork .getStaticInputValues() .length; a++) { inputValue = inputValue + neuralNetwork .getStaticInputValues()[a] * neuralNetwork.getLayer( i).getWeight( a, j); } inputValue = inputValue + neuralNetwork.getLayer(i).getBias(j); } l1 = neuralNetwork.getLayer(i); l1.getNeuron(j).setDelta( l1 .getNeuron(j) .getActivationFunction() .functionDerivative( inputValue) * wXdelta); } } } else { // TODO implement this case. } } else { // TODO Implement this case. if (neuralNetwork.isMultiConexed() == true) { // TODO Implement this. } else { // TODO Implement this. } } } //calculateNeuronDeltas() /** * Returns <i>lambda</i> value. * * @return <i>Lambda</i> value. */ public double getLambda() { return lambda; } //getLambda() /** * Returns multiplier value. * * @return Multiplier value. */ public int getMultiplier() { return multiplier; } //getMultiplier() /** * Sets a value to <i>lambda</i>. * * @param lambda Value to be seted. */ public void setLambda(double lambda) { this.lambda = lambda; } //setLambda() /** * Sets a value to multiplier. * * @param multiplier Value to be seted. */ public void setMultiplier(int multiplier) { this.multiplier = multiplier; } //setMultiplier() /** * Tests all deltaW to be larger than <code>double</code> * precision. If a deltaW is less than 1E-16, this means that * precision was exceeded, so 1 is seted to corresponing deltaW. * * @param deltaW Neural network training deltaW matrix. */ public void testDeltaW(double[][] deltaW) { for (int i=0; i < deltaW.length; i++){ for (int j=0; j < deltaW[0].length; j++) { if ( Double.compare(Math.abs(deltaW[i][j]),1e-16) < 0) { System.out.println("mudei"); if ( Double.compare((deltaW[i][j]),0) < 0 ) { deltaW[i][j]=-1; } else { deltaW[i][j]=1; } } } } } //testDeltaW() } //LevenbergMarquardtModel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -