📄 neuraloutput.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.Adapters.PmmlVersion20.DerivedField;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Transform.OneToOneMapping;
/**
* An output unit of a Neural Network. Just runs a transformation of the
* target attribute. <p>
*
* Corresponds to PMML element NeuralOutput.
*
* @see com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput
*/
public class NeuralOutput extends NeuralNode {
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Transformation object of this class. */
protected OneToOneMapping oneToOneMapping = null;
/** The current target attribute value. Used for training. */
protected double predictedValue = Category.MISSING_VALUE;
/** Use softmax normalization. */
protected boolean softmax;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
public NeuralOutput() {
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns input one-to-one mapping.
*
* @return input mapping
*/
public OneToOneMapping getOneToOneMapping() {
return oneToOneMapping;
}
/**
* Sets input one-to-one mapping.
*
* @param oneToOneMapping new mapping
*/
public void setOneToOneMapping(OneToOneMapping oneToOneMapping) {
this.oneToOneMapping = oneToOneMapping;
}
/**
* Returns value of target attribute for current training instance.
*
* @return value of predicted attribute
*/
public double getPredictedValue() {
return predictedValue;
}
/**
* Sets value of target attribute for current training instance.
*
* @param predictedValue new predicted attribute value
*/
public void setPredictedValue(double predictedValue) {
this.predictedValue = predictedValue;
}
/**
* Is softmax normalization? (Softmax can only be used when the last neuron
* layer is linear.)
*
* @return true if is softmax, false otherwise
*/
public boolean isSoftmax() {
return softmax;
}
/**
* Set softmax normalization. (Softmax can only be used when the last neuron
* layer is linear.)
*
* @param softmax softmax normalization
*/
public void setSoftmax(boolean softmax) {
this.softmax = softmax;
}
// -----------------------------------------------------------------------
// Calculation methods
// -----------------------------------------------------------------------
/**
* Reset all internal calculation values.
*
* Calls recursively the reset methods of all input nodes.
*/
public void resetValues() {
if ( !Category.isMissingValue(input) || !Category.isMissingValue(output) ||
!Category.isMissingValue(error) ) {
input = Category.MISSING_VALUE;
output = Category.MISSING_VALUE;
error = Category.MISSING_VALUE;
weightsUpdated = false;
for (int i = 0; i < getParentsCount(); i++)
( (NeuralNode) getParentAt(i) ).resetValues();
}
}
/**
* Calculates input of the neuron. At this, the sum of all
* input neuron output values (calls the method recursively)
* is calculated.
*
* @param newcalc calculate the input new
* @return output value, missing if not calculated
* @exception MiningException error while calculating input value
*/
public double inputValue(boolean newcalc) throws MiningException {
if ( Category.isMissingValue(input) && newcalc ) {
// Sum:
input = 0;
for (int i = 0; i < getParentsCount(); i++) {
double out = ((NeuralNode) getParentAt(i)).outputValue(true);
input = input + out;
};
}
return input;
}
/**
* Calculates output of the neural output. At this, first the input value
* is calculated and then the transformation is applied.
*
* @param newcalc calculate the output new
* @return output value, missing if not calculated
* @exception MiningException error while calculating output value
*/
public double outputValue(boolean newcalc) throws MiningException {
if ( Category.isMissingValue(output) && newcalc ) {
// Get input value:
output = inputValue(true);
// Softmax:
if (softmax) {
NeuralLayer parLay = ( (NeuralNode) getParentAt(0)).getNeuralLayer();
double sum = 0;
for (int i = 0; i < parLay.getNumberOfNodes(); i++)
sum = sum + Math.exp( parLay.getNeuralNodes()[i].outputValue(true) );
output = Math.exp(output) / sum;
}
// Apply transformation:
output = oneToOneMapping.transformAttributeValue(output);
}
return output;
}
/**
* Calculate error of neuron.
*
* @param newcalc calculate the error new
* @return error value, missing if not calculated
* @exception MiningException error while calculating error value
*/
public double errorValue(boolean newcalc) throws MiningException {
if ( Category.isMissingValue(error) && !Category.isMissingValue(output) && newcalc ) {
error = predictedValue - output;
}
return error;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Write neural output to PMML element.
*
* @return PMML element of neural output
* @exception MiningException
*/
public Object createPmmlObject() throws MiningException
{
// Create neural output:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput noutput =
new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput();
// Add input neuron:
if ( getParentsCount() > 0 ) {
NeuralNode par = (NeuralNode) getParentAt(0);
noutput.setOutputNeuron( par.getId() );
};
// Set derived field if one-to-one mapping:
if (oneToOneMapping != null)
noutput.setDerivedField( (DerivedField) oneToOneMapping.createPmmlObject() );
return noutput;
}
/**
* Read neural output from PMML element.
*
* @param pmmlObject PMML element to read in
* @exception MiningException always thrown
*/
public void parsePmmlObject( Object pmmlObject ) throws MiningException
{
// Get neural output:
com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput noutput =
(com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput) pmmlObject;
// Get input neuron:
inputIDs = new String[1];
inputIDs[0] = noutput.getOutputNeuron();
// Get one-to-one mapping field:
DerivedField derivedField = noutput.getDerivedField();
oneToOneMapping = OneToOneMapping.createOneToOneMappingFromPmml( derivedField );
if ( oneToOneMapping != null )
oneToOneMapping.parsePmmlObject( derivedField );
}
// -----------------------------------------------------------------------
// Further methods
// -----------------------------------------------------------------------
/**
* Delivers string representation of neural output.
*
* @return string representation of neural output
*/
public String toString() {
String s = "Neural Output, " + super.toString() + ", predicted value = " + predictedValue;
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -