📄 neurallayer.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.Category;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.PmmlPresentable;
/**
* A NeuralLayer object captures the parameters required to describe a layer
* in a neural network model. <p>
*
* Corresponds to PMML element NeuralLayer.
*
* @see com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer
*/
public class NeuralLayer extends com.prudsys.pdm.Cwm.Core.Class implements PmmlPresentable {
// -----------------------------------------------------------------------
// Constants of layer type
// -----------------------------------------------------------------------
/** Undefined type. */
public static final int UNDEFINED = 0;
/** Input layer type. */
public static final int NEURAL_INPUT = 1;
/** Neuron type. */
public static final int NEURON = 2;
/** Output layer type. */
public static final int NEURAL_OUTPUT = 3;
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Type of this layer (undefined, input, neuron, output). */
protected int layerType = UNDEFINED;
/** Reference to NeuralNetwork that owns this neural layer. */
protected NeuralNetwork neuralNetwork = null;
/** Array of nodes of the layer. */
protected NeuralNode[] neuralNodes;
/** Reference of activation function of this neuron. */
protected ActivationFunction activationFunction = null;
/** Indicates whether a bias term is used in the neuron. */
protected boolean useBias = true;
/** Threshold value when no bias term is used. */
protected double threshold = Category.MISSING_VALUE;
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public NeuralLayer() {
}
/**
* Constructor with given number of neurons of appropriate type.
*
* @param numbNeurons number of neurons
* @param layerType layer type
*/
public NeuralLayer(int numbNeurons, int layerType) {
this.layerType = layerType;
this.neuralNodes = new NeuralNode[numbNeurons];
if (layerType == NEURAL_INPUT) {
for (int i = 0; i < numbNeurons; i++) {
neuralNodes[i] = new NeuralInput();
neuralNodes[i].setNeuralLayer(this);
}
}
else
if (layerType == NEURAL_OUTPUT) {
for (int i = 0; i < numbNeurons; i++) {
neuralNodes[i] = new NeuralOutput();
neuralNodes[i].setNeuralLayer(this);
}
}
else {
for (int i = 0; i < numbNeurons; i++) {
neuralNodes[i] = new Neuron();
neuralNodes[i].setNeuralLayer(this);
}
}
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Get layer type (undefined, input, neuron, output).
*
* @return layer type
*/
public int getLayerType() {
return layerType;
}
/**
* Set layer type (undefined, input, neuron, output).
*
* @param layerType new layer type
*/
public void setLayerType(int layerType) {
this.layerType = layerType;
}
/**
* Get neural network object that owns this neural layer.
*
* @return neural network object, null if not required
*/
public NeuralNetwork getNeuralNetwork() {
return neuralNetwork;
}
/**
* Sets new neural network object that owns this neural layer.
*
* @param neuralNetwork new neural network object
*/
public void setNeuralNetwork(NeuralNetwork neuralNetwork) {
this.neuralNetwork = neuralNetwork;
}
/**
* Reference of activation function of this layer. If activation function
* is defined on level of NeuralNetwork, this must be the same reference.
* If set to null, the activation functions are specified on the neuron level.
*
* @return activation function of layer
*/
public ActivationFunction getActivationFunction() {
return activationFunction;
}
/**
* Set activation function of this layer. If activation function
* is defined on level of NeuralNetwork, this must be the same reference.
* If set to null, the activation functions are specified on the neuron level.
*
* @param activationFunction new activation function of this layer
*/
public void setActivationFunction(ActivationFunction activationFunction) {
this.activationFunction = activationFunction;
if (activationFunction != null && layerType == NEURON) {
for (int i = 0; i < getNumberOfNodes(); i++)
((Neuron) neuralNodes[i]).setActivationFunction(activationFunction);
}
}
/**
* Returns true if a bias term is used in the layer. A bias is equivalent
* to an input connection set at a constant level. If a bias use is
* prescribed by NeuralNetwork, this bias use must also be true. If the value
* is false, the use of bias can also be specified on the neuron level.
*
* @return true if bias term is used, false otherwise
*/
public boolean isUseBias() {
return useBias;
}
/**
* Set bias term is used in the layer. A bias is equivalent
* to an input connection set at a constant level. If a bias use is
* prescribed by NeuralNetwork, this bias use must also be true. If the value
* is false, the use of bias can also be specified on the neuron level.
*
* @param useBias set use bias term
*/
public void setUseBias(boolean useBias) {
this.useBias = useBias;
if (useBias && layerType == NEURON) {
for (int i = 0; i < getNumberOfNodes(); i++)
((Neuron) neuralNodes[i]).setUseBias(useBias);
}
}
/**
* Returns threshold value. Usually required, when no bias term
* is used. If threshold value defined on NeuralNetwork level is
* not missing, this must be the same value. If the value is missing,
* the threshold can also be specified on the neuron level.
*
* @return threshold value
*/
public double getThreshold() {
return threshold;
}
/**
* Sets new threshold value. Usually required, when no bias term
* is used. If threshold value defined on NeuralNetwork level is
* not missing, this must be the same value. If the value is missing,
* the threshold can also be specified on the neuron level.
*
* @param threshold new threshold value
*/
public void setThreshold(double threshold) {
this.threshold = threshold;
if ( !Category.isMissingValue(threshold) && layerType == NEURON ) {
for (int i = 0; i < getNumberOfNodes(); i++)
((Neuron) neuralNodes[i]).setThreshold(threshold);
}
}
// -----------------------------------------------------------------------
// Layer topology methods
// -----------------------------------------------------------------------
/**
* Removes all nodes from layer.
*/
public void removeAllNodes() {
neuralNodes = null;
}
/**
* Returns number of nodes of the layer.
*
* @return number of nodes of the layer
*/
public int getNumberOfNodes() {
return ( neuralNodes != null ) ? neuralNodes.length : 0;
}
/**
* Returns array of nodes of the layer.
*
* @return array of nodes of the layer
*/
public NeuralNode[] getNeuralNodes() {
return neuralNodes;
}
/**
* Sets array of nodes of the layer.
*
* @param neuralNodes new array of nodes of the layer
*/
public void setNeuralNodes(NeuralNode[] neuralNodes) {
this.neuralNodes = neuralNodes;
for (int i = 0; i < getNumberOfNodes(); i++)
neuralNodes[i].setNeuralLayer(this);
}
/**
* Adds a new neural to the layer.
*
* @param node new neural node to add
* @exception MiningException cannot add node
*/
public void addNeuralNode(NeuralNode node) throws MiningException {
int nnode = getNumberOfNodes();
NeuralNode[] node2 = new NeuralNode[nnode];
for (int i = 0; i < nnode; i++)
node2[i] = (NeuralNode) neuralNodes[i];
neuralNodes = new NeuralNode[nnode+1];
for (int i = 0; i < nnode; i++)
neuralNodes[i] = node2[i];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -