📄 quasinewton.java
字号:
/* * $RCSfile: QuasiNewton.java,v $ * $Revision: 1.15 $ * $Date: 2005/05/08 02:16:29 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Bras韑ia * * 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;import neuralnetworktoolkit.debugger.mlp.MLPDebugger;import neuralnetworktoolkit.math.NeuralMath;import neuralnetworktoolkit.methods.gradientbased.GradientBasedMethod;import neuralnetworktoolkit.neuralnetwork.INeuralNetwork;/** * An implementation of Quasi-Newton training method. * <br><b>Experimental implementation. Not properly tested!</b> * * @version 1.0 19 Jun 2004 * * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gon锟絘lves</a> * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> */public abstract class QuasiNewton extends GradientBasedMethod { /** Maximum number of iterations. */ public static final int MAX_ITERATIONS = 10000000; protected MLPDebugger debugger; /** * Calculates neuron deltas. * * @param neuralNetwork Neural network to calculate deltas. */ public abstract void calculateNeuronDeltas(INeuralNetwork neuralNetwork); /** * Calculates the product of transposed jacobian matrix by * jacobian matrix, the product of transposed jacobian matrix * by error vector and the sum of the squared error. * * @param neuralNetwork Neural network to calculate the products. * @param instanceSet Training instances set. * @param outputs Network expected outputs. * @param subInstanceSize Sub-block size of <code>instanceSet</code>. * * @return The two products. */ public JacobianTxJacobianAndJacobianTxErrorAndError calculateJacobianTxJacobianAndJacobianTxError( INeuralNetwork neuralNetwork, double[][] instanceSet, double[][] outputs, int subInstanceSize) { double[][] subInstanceSet = new double[subInstanceSize][instanceSet[0].length]; double[][] finalInstances; int numberOfSynapses = neuralNetwork.numberOfSynapses(); int index = 0; int subInstanceIndex = 0; int numberOfBlocks = 0; int blockId = 0; double[][] jtXj = new double[numberOfSynapses][numberOfSynapses]; double[][] jtXerror = new double[numberOfSynapses][1]; double[][] jacobian = new double[subInstanceSize][numberOfSynapses]; double[][] jacobianT; double[][] subError = new double[subInstanceSize][1]; double error = 0; //neuralMath.printMatrix(instanceSet); //neuralMath.printMatrix(outputs); numberOfBlocks = (int)instanceSet.length/subInstanceSize; System.out.println(">>"+numberOfBlocks); for (int i = 0; i < numberOfBlocks * subInstanceSize; i++) { subInstanceSet[subInstanceIndex] = instanceSet[i]; subInstanceIndex++; if ((i % (subInstanceSize) == (subInstanceSize - 1)) && (i > 0)) { for (int m0 = 0; m0 < subInstanceSet.length; m0++) { double[] input = subInstanceSet[m0]; neuralNetwork.inputLayerSetup(input); neuralNetwork.propagateInput(); calculateNeuronDeltas(neuralNetwork); //debugger.printNetworkState(); jacobian[m0] = calculateDerivativesVector(neuralNetwork); subError[m0][0] = outputs[blockId * subInstanceSize + m0][0] - neuralNetwork.retrieveFinalResults()[0]; error += subError[m0][0]*subError[m0][0]; } //System.out.println("J ----------------------"); //NeuralMath.printMatrix(jacobian); //System.out.println("------------------------"); jtXj = NeuralMath.matrixSum( jtXj, NeuralMath.transposeProduct(jacobian)); jacobianT = NeuralMath.calculateTransposed(jacobian); jtXerror = NeuralMath.matrixSum( jtXerror, NeuralMath.matrixProduct(jacobianT, subError)); blockId++; subInstanceIndex = 0; } } if ((instanceSet.length - numberOfBlocks * subInstanceSize) > 0) { finalInstances = new double[instanceSet.length - numberOfBlocks * subInstanceSize][instanceSet[0].length]; for (int i = 0; i < (instanceSet.length - numberOfBlocks * subInstanceSize); i++) { finalInstances[i] = instanceSet[numberOfBlocks * subInstanceSize + i]; } jacobian = new double[finalInstances.length][numberOfSynapses]; subError = new double[finalInstances.length][1]; for (int m0 = 0; m0 < finalInstances.length; m0++) { double[] input = finalInstances[m0]; neuralNetwork.inputLayerSetup(input); neuralNetwork.propagateInput(); calculateNeuronDeltas(neuralNetwork); //debugger.printNetworkState(); jacobian[m0] = calculateDerivativesVector(neuralNetwork); subError[m0][0] = outputs[numberOfBlocks * subInstanceSize + m0][0] - neuralNetwork.retrieveFinalResults()[0]; error += subError[m0][0]*subError[m0][0]; } //System.out.println("J ----------------------"); //NeuralMath.printMatrix(jacobian); //System.out.println("------------------------"); jtXj = NeuralMath.matrixSum( jtXj, NeuralMath.transposeProduct(jacobian)); jacobianT = NeuralMath.calculateTransposed(jacobian); jtXerror = NeuralMath.matrixSum( jtXerror, NeuralMath.matrixProduct(jacobianT, subError)); } //System.out.println("Jt x J -----------------"); //NeuralMath.printMatrix(jtXj); //System.out.println("------------------------"); //System.out.println("Jt x Error -----------------"); //NeuralMath.printMatrix(jtXerror); //System.out.println("------------------------"); return new JacobianTxJacobianAndJacobianTxErrorAndError(jtXj, jtXerror, error); } //calculateJacobianTxJacobian() } //QuasiNewton
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -