⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaussnewton.java

📁 利用Java实现的神经网络工具箱
💻 JAVA
字号:
/* * $RCSfile: GaussNewton.java,v $ * $Revision: 1.11 $ * $Date: 2005/05/05 00:06:37 $ * * 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.gaussnewton;import neuralnetworktoolkit.*;import neuralnetworktoolkit.methods.*;import neuralnetworktoolkit.methods.gradientbased.quasinewton.*;import neuralnetworktoolkit.math.*;import neuralnetworktoolkit.neuralnetwork.*;/** * An implementation of Gauss-Newton training method. * <br><b>Experimental implementation. Not properly tested!</b> *  * @version $Revision: 1.11 $ - $Date: 2005/05/05 00:06:37 $ *  * @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 GaussNewton extends QuasiNewton {	private boolean goAhead = true;	/* (non-Javadoc)	 * @see neuralnetworktoolkit.methods.ITrainingMethod#train(neuralnetworktoolkit.INeuralNetwork, double, double[][], double[][], int[])	 */	public StatisticalResults train(INeuralNetwork neuralNetwork, TrainingParameters parameters) {									 			// TODO Erase comented instrumentation code.		StatisticalResults results = new StatisticalResults();		int inputSize = 0;		int outputSize = 0;		int numberOfIterations = 0;		int maximumNumberOfIterations = MAX_ITERATIONS;		int numberOfSynapses = neuralNetwork.numberOfSynapses();		double totalErrorEnergy = 0;		double instantaneousError = 10;		double[][] jacobianTxJacobian;		double[][] jacobianTxError;		double[][] diagonal;		double[][] inverseHessian;		double[][] deltaW;		double inicio, fim;				GaussNewtonParameters param = (GaussNewtonParameters) parameters;		double errorGoal = param.getError();		JacobianTxJacobianAndJacobianTxErrorAndError jtXjAndJtXerror;		maximumNumberOfIterations = param.getMaxIterations();				inicio = System.currentTimeMillis();				do {			totalErrorEnergy = calculateTotalError(neuralNetwork, param					.getInputs(), param.getOutputs());			jtXjAndJtXerror = calculateJacobianTxJacobianAndJacobianTxError(					neuralNetwork, param.getInputs(), param.getOutputs(), 3);			jacobianTxJacobian = jtXjAndJtXerror.getJtXj();			jacobianTxError = jtXjAndJtXerror.getJtXerror();			inverseHessian = NeuralMath.inverseMatrix(jacobianTxJacobian);			deltaW = NeuralMath.matrixProduct(inverseHessian, jacobianTxError);			deltaW = NeuralMath.constantTimesMatrix(-1, deltaW);			neuralNetwork.updateWeights(deltaW);			instantaneousError = calculateTotalError(neuralNetwork, param					.getInputs(), param.getOutputs());			numberOfIterations++;			if (numberOfIterations % (maximumNumberOfIterations / 100) == 0) {				System.out						.println("Numero de Iteracoes: " + numberOfIterations);				System.out.println("Erro atual: " + totalErrorEnergy);			}		} while (((totalErrorEnergy / param.getInputs().length) > errorGoal)				&& (numberOfIterations < maximumNumberOfIterations)				&& (goAhead = true));				fim = System.currentTimeMillis();						results.setNumberIterations(numberOfIterations);		results.setError(totalErrorEnergy / param.getInputs().length);		results.setTrainingTime((fim-inicio)/1000);				return results;			} //train()	/* (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) {																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 {							if (neuralNetwork.isMultiConexed() == true) {				// TODO Implement this.						} else {				//	TODO Implement this.			}		}	} //calculateNeuronDeltas()	/* (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];						error =				error					+ (outputs[i][0] - actualOutput)						* (outputs[i][0] - actualOutput);		}		return error;			} //calculateTotalError()} //GaussNewton

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -