📄 activationfunction.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.MiningException;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Elliot;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Exponential;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Gauss;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Linear;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Logistic;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Reciprocal;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Signum;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Sine;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Square;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Step;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.TangensHyperbolicus;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Threshold;
/**
* Abstract class for activation function of a neuron. The activation function
* computes the activation state of the neuron based on its inputs. For a single
* neuron, activation can be computed as a transform of the sum of the neural
* inputs times the weights on those inputs, plus the bias:
* <br> <i>XZ = Sum(w(i) * input(i)) +
* bias</i><br> <i>Y = output(j) = activation(XZ)</i> <p>
*
* Note that the neural inputs may be outputs from the previous layer.
*/
public abstract class ActivationFunction {
// -----------------------------------------------------------------------
// Constants of activiation function types
// -----------------------------------------------------------------------
/** Identity: f(x) = x. */
public static final String LINEAR = "linear";
/** Logistic function: f(x) = 1 / (1 + exp(-x)). */
public static final String LOGISTIC = "logistic";
/** Hyperbolic tangent: f(x) = tanh(x). */
public static final String TANGENS_HYPERBOLICUS = "tanh";
/** Signum function: f(x) = 1 if x >= 0, else -1. */
public static final String SIGNUM = "sgn";
/** Step function: f(x) = 1 if x >= 0, else 0. */
public static final String STEP = "step";
/** Threshold function: f(x) = 1 if x >= threshold, else 0. */
public static final String THRESHOLD = "threshold";
/** Exponential function: f(x) = exp(x). */
public static final String EXPONENTIAL = "exp";
/** Reciprocal function: f(x) = 1/x. */
public static final String RECIPROCAL = "reciprocal";
/** Square function: f(x) = x*x. */
public static final String SQUARE = "sqr";
/** Gaussian function: f(x) = exp(-x*x). */
public static final String GAUSS = "gauss";
/** Sine function: f(x) = sin(x). */
public static final String SINE = "sin";
/** Elliot function: f(x) = x/(1 + |x|). */
public static final String ELLIOT = "elliot";
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public ActivationFunction() {
}
/**
* Returns an instance of ActivationFunction corresponding to the specified
* type.
*
* @param type activation function type
* @return the instance of the specified type
* @exception MiningException unknown activation function type
*/
public static ActivationFunction getInstance(String type) throws MiningException {
ActivationFunction af = null;
if ( type.equals(THRESHOLD) ) af = new Threshold();
else if ( type.equals(LOGISTIC) ) af = new Logistic();
else if ( type.equals(TANGENS_HYPERBOLICUS) ) af = new TangensHyperbolicus();
else if ( type.equals(LINEAR) ) af = new Linear();
else if ( type.equals(ELLIOT) ) af = new Elliot();
else if ( type.equals(EXPONENTIAL) ) af = new Exponential();
else if ( type.equals(GAUSS) ) af = new Gauss();
else if ( type.equals(RECIPROCAL) ) af = new Reciprocal();
else if ( type.equals(SIGNUM) ) af = new Signum();
else if ( type.equals(SINE) ) af = new Sine();
else if ( type.equals(SQUARE) ) af = new Square();
else if ( type.equals(STEP) ) af = new Step();
return af;
}
// -----------------------------------------------------------------------
// Methods of function calculation
// -----------------------------------------------------------------------
/**
* Returns type of activation function as string.
*
* @return type of activation function
*/
public abstract String getFunctionType();
/**
* Calculates the function value.
*
* @param value argument
* @return function value, NaN if not defined
*/
public abstract double function(double value);
/**
* Calculates the reverse function value.
*
* @param value argument
* @return reverse function value, NaN if not defined
*/
public abstract double reverseFunction(double value);
/**
* First derivation of function.
*
* @param value argument
* @return first deruvation, NaN if not defined
*/
public abstract double derivation(double value);
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Returns name of PMML entity ACTIVATION-FUNCTION, null if no equivalent
* exists.
*
* @param type activation function type
* @return PMML name
*/
public static String convertTypeToPmml(String type) {
String pmmlName = null;
if ( type.equals(THRESHOLD) ) pmmlName = "threshold";
else if ( type.equals(LOGISTIC) ) pmmlName = "logistic";
else if ( type.equals(TANGENS_HYPERBOLICUS) ) pmmlName = "tanh";
else if ( type.equals(LINEAR) ) pmmlName = "identity";
return pmmlName;
}
/**
* Returns name of activation function from PMML entity ACTIVATION-FUNCTION,
* null if no equivalent exists.
*
* @param pmmlName PMML name
* @return activation function type
*/
public static String convertPmmlToType(String pmmlName) {
String type = null;
if ( pmmlName.equals("threshold") ) type = THRESHOLD;
else if ( pmmlName.equals("logistic") ) type = LOGISTIC;
else if ( pmmlName.equals("tanh") ) type = TANGENS_HYPERBOLICUS;
else if ( pmmlName.equals("identity") ) type = LINEAR;
return type;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -