📄 neuralnetworkmodel.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
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Models.Regression.NeuralNetwork;
import java.io.Reader;
import java.io.Writer;
import com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary;
import com.prudsys.pdm.Adapters.PmmlVersion20.Header;
import com.prudsys.pdm.Adapters.PmmlVersion20.MiningSchema;
import com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs;
import com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs;
import com.prudsys.pdm.Adapters.PmmlVersion20.PMML;
import com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary;
import com.prudsys.pdm.Core.ApplicationInputSpecification;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Models.Regression.RegressionMiningModel;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningModel;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Utils.PmmlUtils;
/**
* Description of the data produced by a Neural Network (NN)
* mining function. <p>
*
* From PDM CWM extension. <p>
*
* Superclasses:
* <ul>
* <li> RegressionMiningModel
* </ul>
*
* In addition, functionality from extended PMML was added.
* It corresponds to the PMML element NeuralNetwork.
*
* @see MiningModel
* @see SupervisedMiningModel
* @see com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork
*/
public class NeuralNetworkModel extends RegressionMiningModel
{
/**
* Constructor sets function and algorithm parameters.
*/
public NeuralNetworkModel()
{
function = MiningModel.REGRESSION_FUNCTION;
algorithm = MiningModel.NEURAL_NETWORK_ALGORITHM;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Write NN model to PMML document.
*
* @param writer writer for the PMML document
* @exception MiningException
*/
public void writePmml( Writer writer ) throws MiningException
{
PMML pmml = new PMML();
pmml.setVersion( "2.0" );
pmml.setHeader( (Header)PmmlUtils.getHeader() );
// Add data and transformation dictionaries:
MiningDataSpecification metaData = miningSettings.getDataSpecification();
if ( metaData.isTransformed() )
{
pmml.setDataDictionary( (DataDictionary)metaData.getPretransformedMetaData().createPmmlObject() );
pmml.setTransformationDictionary( (com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary)metaData.getMiningTransformationActivity().createPmmlObject() );
}
else
{
pmml.setDataDictionary( (DataDictionary)metaData.createPmmlObject() );
};
// Add NN model:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork[] nnModel =
new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork[1];
nnModel[0] = (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork) createPmmlObject();
pmml.setNeuralNetwork ( nnModel );
// Add encoding and write to document:
PmmlUtils.setEncoding();
PmmlUtils.marshalPmml(writer, pmml);
}
/**
* Write NN model to PMML element.
*
* @return PMML element of NN model
* @exception MiningException
*/
public Object createPmmlObject() throws MiningException
{
// Create NN model:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork nnModel =
new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork();
// Set attributes:
nnModel.setModelName( "neural network model" );
nnModel.setFunctionName("regression");
nnModel.setAlgorithmName(algorithm);
// Add mining schema:
nnModel.setMiningSchema( (MiningSchema) inputSpec.createPmmlObject() );
// Add neural layers:
NeuralNetwork neural = (NeuralNetwork) classifier;
// Set activation function:
ActivationFunction activationFunction = neural.getActivationFunction();
if (activationFunction != null) {
String afname = ActivationFunction.convertTypeToPmml(activationFunction.getFunctionType());
if (afname == null)
afname = activationFunction.getFunctionType(); // use even if no equivalent PMML name exists
nnModel.setActivationFunction(afname);
}
// Set threshold:
double threshold = neural.getThreshold();
if ( !Category.isMissingValue(threshold) )
nnModel.setThreshold( String.valueOf(threshold) );
// ADD LAYERS:
int nlayer = neural.getNumberOfLayers();
if (nlayer <= 2)
throw new MiningException("there must be at least three layers (including input, output)");
NeuralLayer[] layers = neural.getNeuralLayer();
// Set input layer:
nnModel.setNeuralInputs( (NeuralInputs) layers[0].createPmmlObject() );
// Set hidden layer:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer[] hlayer =
new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer[nlayer-2];
for (int i = 1; i < nlayer-1; i++)
hlayer[i-1] = (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer) layers[i].createPmmlObject();
nnModel.setNeuralLayer(hlayer);
// Set output layer:
nnModel.setNeuralOutputs( (NeuralOutputs) layers[nlayer-1].createPmmlObject() );
return nnModel;
}
/**
* Read NN model from PMML document.
*
* @param reader reader for the PMML document
* @exception MiningException always thrown
*/
public void readPmml( Reader reader ) throws MiningException
{
// com.borland.xml.toolkit.XmlUtil.setEncoding( "UTF-8" );
PMML pmml = PMML.unmarshal( reader );
if( pmml.getNeuralNetworkCount() == 0 )
throw new MiningException("no neural network model found");
// Read data dictionary:
DataDictionary dictionary = pmml.getDataDictionary();
MiningDataSpecification newMetaData = new MiningDataSpecification();
newMetaData.parsePmmlObject( dictionary );
// Read transformation dictionary:
TransformationDictionary transDict = pmml.getTransformationDictionary();
if (transDict != null) {
MiningTransformationActivity mta = new MiningTransformationActivity();
mta.parsePmmlObject(transDict);
MiningDataSpecification tmds = mta.transform(newMetaData);
tmds.setPretransformedMetaData(newMetaData);
newMetaData = tmds;
newMetaData.setMiningTransformationActivity( mta );
newMetaData.setTransformed(true);
};
// Init settings:
NeuralNetworkSettings nnSettings = new NeuralNetworkSettings();
nnSettings.setDataSpecification( newMetaData );
setMiningSettings( nnSettings );
// Read NN model:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork[] nnModel = pmml.getNeuralNetwork();
parsePmmlObject( nnModel[0] );
}
/**
* Read NN model from PMML element.
*
* @param pmmlObject PMML element to read in
* @exception MiningException could not parse PMML object
*/
public void parsePmmlObject( Object pmmlObject ) throws MiningException
{
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork nnModel =
(com.prudsys.pdm.Adapters.PmmlVersion20.NeuralNetwork) pmmlObject;
// Get mining schema:
MiningSchema schema = nnModel.getMiningSchema();
// Create input specification, get target attribute and inner trafo from schema:
inputSpec = new ApplicationInputSpecification();
inputSpec.parsePmmlObject( schema );
target = inputSpec.getTargetApplicationAttribute();
miningTransform = inputSpec.createInnerTrafoFromInputSpec( miningSettings.getDataSpecification() );
// Add target attribute to mining settings:
NeuralNetworkSettings nns = (NeuralNetworkSettings) miningSettings;
String classificationAttributeName = target.getName();
nns.setPredictedAttributeName( classificationAttributeName );
MiningAttribute targetAtt = nns.getDataSpecification().getMiningAttribute( classificationAttributeName );
nns.setTarget( targetAtt );
// Create neural network:
NeuralNetwork neural = new NeuralNetwork();
// GET LAYERS:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer[] hl = nnModel.getNeuralLayer();
if (hl == null || hl.length == 0)
throw new MiningException("there must be at least one hidden layer");
int nlayer = hl.length + 2;
NeuralLayer[] hlayer = new NeuralLayer[ nlayer ];
for (int i = 0; i < nlayer; i++)
hlayer[i] = new NeuralLayer();
// Get input layer:
hlayer[0].parsePmmlObject( nnModel.getNeuralInputs() );
// Get hidden layers:
for (int i = 1; i < nlayer-1; i++)
hlayer[i].parsePmmlObject( hl[i-1] );
// Get output layer:
hlayer[nlayer-1].parsePmmlObject( nnModel.getNeuralOutputs() );
// Add layers to model:
neural.setNeuralLayer(hlayer);
// Create connections:
hlayer = neural.getNeuralLayer();
for (int i = 1; i < nlayer; i++) {
NeuralNode[] nodes = hlayer[i].getNeuralNodes();
if (nodes == null || nodes.length == 0) {
System.out.println("Warning! Layer: " + i + " without neural nodes" );
continue;
};
for (int j = 0; j < nodes.length; j++) {
String[] parIDs = nodes[j].getInputIDs();
if (parIDs == null || parIDs.length == 0) {
System.out.println("Warning! Node #ID: " + nodes[j].getId() +
" of Layer: " + i + " without input");
}
else {
for (int k = 0; k < parIDs.length; k++) {
NeuralNode par = neural.getNeuralNodeFromId( parIDs[k] );
if (par == null) {
System.out.println("Warning! Invalid parent node #ID: " + parIDs[k] +
" of node #ID: " + nodes[j].getId() + " of layer: " + i);
}
else {
boolean res = neural.connectNodes(par, nodes[j], false);
};
};
}
}
}
// Get activation function:
String afname = nnModel.getActivationFunction();
if (afname != null) {
String aftype = ActivationFunction.convertPmmlToType(afname);
if (afname == null)
aftype = afname; // try even if name is unknown in PMML standard
ActivationFunction af = ActivationFunction.getInstance(aftype);
if (af != null)
neural.setActivationFunction(af);
}
// Get threshold:
String thresh = nnModel.getThreshold();
if (thresh != null) neural.setThreshold( Double.parseDouble(thresh) );
// Assign meta data:
MiningDataSpecification nnMetaData = nns.getDataSpecification();
if (miningTransform != null) // reduce meta data to mining schema
nnMetaData = miningTransform.transform(nns.getDataSpecification());
neural.setMetaData( nnMetaData );
neural.setClassName( target.getName() );
// Set classifier:
setClassifier(neural);
}
/**
* Returns string representation of neural network model.
*
* @return string representation
*/
public String toString()
{
return "Neural network model";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -