📄 neuralnetworkalgorithm.java
字号:
/*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Michael Thess
* @version 1.2
*/
package com.prudsys.pdm.Models.Regression.NeuralNetwork;
import com.prudsys.pdm.Core.ApplicationAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.MiningSettings;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Models.Regression.RegressionAlgorithm;
import com.prudsys.pdm.Models.Supervised.Classifier;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Transform.Special.ReplaceMissingValueStream;
import com.prudsys.pdm.Transform.Special.TreatOutlierValueStream;
/**
* A class representing a Neural Network algorithm. Each implementation
* should extend this class and override only the methods:
* {@link #runAlgorithm() runAlgorithm()},
* {@link #getClassifier()() getClassifier()}
*/
public abstract class NeuralNetworkAlgorithm extends RegressionAlgorithm
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Type of learning algorithm. */
protected int learningType = NeuralNetworkSettings.BACK_PROPAGATION_WITH_MOMENTUM;
/** Algorithm automatically builds the neural network. */
protected boolean autoBuildNetwork = true;
/** Learning rate of backpropagation method. */
protected double learningRate = 1.0;
/** Momentum of backpropagation method, if used. */
protected double momentum = 0.5;
/** Maximum number of iterations. */
protected int maxNumberOfIterations = 1000;
/** Maximum acceptable error. */
protected double maxError = 1.0E-35;
/** Neural network object, if algorithm doesn't build it automatically. */
protected NeuralNetwork neuralNetwork = null;
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Creates an instance of the neural network settings class that is required
* to run the algorithm. The mining settings are assigned through the
* setMiningSettings method.
*
* @return new instance of the sparse grids settings class of the algorithm
*/
public MiningSettings createMiningSettings() {
return new NeuralNetworkSettings();
}
/**
* Set NN settings.
*
* @param miningSettings instance of NeuralNetworkSettings
* @exception IllegalArgumentException mining settings are not of Neural Network type
*/
public final void setMiningSettings( MiningSettings miningSettings ) throws IllegalArgumentException
{
if ( miningSettings instanceof NeuralNetworkSettings )
{
super.setMiningSettings( miningSettings );
NeuralNetworkSettings nns = (NeuralNetworkSettings) miningSettings;
this.learningType = nns.getLearningType();
this.autoBuildNetwork = nns.isAutoBuildNetwork();
this.learningRate = nns.getLearningRate();
this.momentum = nns.getMomentum();
this.maxNumberOfIterations = nns.getMaxNumberOfIterations();
this.maxError = nns.getMaxError();
this.neuralNetwork = nns.getNeuralNetwork();
}
else
{
throw new IllegalArgumentException( "MiningSettings have to be NeuralNetworkSettings." );
};
}
/**
* Returns NN classifier. By default, this is the neuralNetwork object.
*
* @return NN classifier
*/
public Classifier getClassifier() {
return neuralNetwork;
}
// -----------------------------------------------------------------------
// Run Neural Network algorithm and build model
// -----------------------------------------------------------------------
/**
* Builds mining model by running the NN algorithm internally.
* Before starting the algorithm, missing values are replaced.
*
* @return sparse grids mining model generated by the algorithm
* @exception MiningException could not build model
*/
public MiningModel buildModel() throws MiningException
{
long start = ( new java.util.Date() ).getTime();
// Outlier treatment and missing value replacement:
TreatOutlierValueStream tro = new TreatOutlierValueStream(miningInputStream);
tro.setNumOutliers( ApplicationAttribute.OUTLIER_TREATMENT_METHOD_asExtremeValues );
tro.createMiningTransformationStep();
ReplaceMissingValueStream rep = new ReplaceMissingValueStream(miningInputStream);
miningInputStream = new MiningArrayStream( rep.createTransformedStream() );
// Transform unbounded -> bounded categorical attributes:
metaData.getMetaDataOp().unboundedToBoundedCategories();
// Run NN algorithm:
runAlgorithm();
// Build NN model:
NeuralNetworkModel model = new NeuralNetworkModel();
model.setMiningSettings( miningSettings );
model.setInputSpec( applicationInputSpecification );
model.setTarget( applicationInputSpecification.getTargetApplicationAttribute() );
// Outlier treatment and missing value in application input specification:
// Create inner transformation object:
MiningTransformationActivity mta = new MiningTransformationActivity();
mta.addTransformationStep( tro.getMts() );
mta.addTransformationStep( rep.getMts() );
model.setMiningTransform( mta );
// Outliers and missing values in application input specification:
applicationInputSpecification.setInputSpecFromInnerTrafo(metaData, tro, rep);
// No NN model parameter to set.
// Set classifier:
model.setClassifier( getClassifier() );
this.miningModel = model;
long end = ( new java.util.Date() ).getTime();
timeSpentToBuildModel = ( end - start ) / 1000.0;
return model;
}
/**
* Runs NN algorithm.
*
* @exception MiningException could not run algorithm
*/
protected abstract void runAlgorithm() throws MiningException;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -