📄 neuralnode.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.MiningHierarchyNode;
import com.prudsys.pdm.Core.PmmlPresentable;
/**
* Base class for all neural nodes (neural input, neuron, neural output). <p>
*
* Mathematically the graph of a feedword neural network is a Directed
* Acyclic Graph (DAG). Therefore this class extends MiningHierarchyNode.
* We define: The inputs are the parents and the outputs are the children.
* Thus the nodes of the neural input layer are the roots and the nodes of the
* neural output layer are the leafs. The levels of the DAG correspond
* to the layers of the neural network.
*/
public abstract class NeuralNode extends MiningHierarchyNode implements PmmlPresentable
{
// -----------------------------------------------------------------------
// Constants of neural node connection type
// -----------------------------------------------------------------------
/** Disconnected type. */
public static final int DISCONNECTED = 0;
/** Connected type. */
public static final int CONNECTED = 1;
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** ID of the neural node. Is a unique identifier within the NN. */
protected String id;
/** Connection type. */
protected int connectType = DISCONNECTED;
/** Reference to NeuralLayer that owns this neural node. */
protected NeuralLayer neuralLayer = null;
/** IDs of input neurons. For PMML purposes. */
protected String[] inputIDs = null;
/** Input value of this node. */
protected double input = Category.MISSING_VALUE;
/** Output value of this node. */
protected double output = Category.MISSING_VALUE;
/** Error value of this node. */
protected double error = Category.MISSING_VALUE;
/** Have the weights of this neural unit already been updated? */
protected boolean weightsUpdated = false;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
public NeuralNode() {
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns ID of neural node.
*
* @return ID of neural node
*/
public String getId() {
return id;
}
/**
* Sets unique ID of neural node.
*
* @param id ID of neural node unique in Neural Network
*/
public void setId(String id) {
this.id = id;
}
/**
* Returns connection type (disconnected, connected).
*
* @return connection type
*/
public int getConnectType() {
return connectType;
}
/**
* Sets connection type (disconnected, connected).
*
* @param connectType new connection type
*/
public void setConnectType(int connectType) {
this.connectType = connectType;
}
/**
* Returns neural layer object that owns this neural node.
*
* @return neural layer owner
*/
public NeuralLayer getNeuralLayer() {
return neuralLayer;
}
/**
* Sets neural layer object that owns this neural node.
*
* @param neuralLayer new neural layer owner
*/
public void setNeuralLayer(NeuralLayer neuralLayer) {
this.neuralLayer = neuralLayer;
}
/**
* Returns IDs of input neurons. Required for PMML.
*
* @return IDs of input neurons
*/
public String[] getInputIDs() {
return inputIDs;
}
// -----------------------------------------------------------------------
// Calculation methods
// -----------------------------------------------------------------------
/**
* Reset all internal calculation values.
*/
public abstract void resetValues();
/**
* Calculates net input of this node.
*
* @param newcalc calculate the input new
* @return input value, missing if not calculated
* @exception MiningException error while calculating input value
*/
public abstract double inputValue(boolean newcalc) throws MiningException;
/**
* Calculates output of this node. This is the activation function
* applied to the input value.
*
* @param newcalc calculate the output new
* @return output value, missing if not calculated
* @exception MiningException error while calculating output value
*/
public abstract double outputValue(boolean newcalc) throws MiningException;
/**
* Calculates error of this node.
*
* @param newcalc calculate the error new
* @return error value, missing if not calculated
* @exception MiningException error while calculating error value
*/
public abstract double errorValue(boolean newcalc) throws MiningException;
/**
* Returns weight at specified index. Should be overriden by Neuron's.
*
* @param index weight index
* @return weight, missing if invalid index
*/
double getWeightAt(int index) {
return 1.0;
}
/**
* This method updates the weights of this neural node and then
* calls the same method of the input nodes recursively. Should
* be overriden by Neuron's.
*
* @param learningRate the learning rate
* @param momentum the momentum
* @exception MiningException cannot update weights
*/
public void updateWeights(double learningRate, double momentum)
throws MiningException {
if (!weightsUpdated) {
for (int i = 0; i < getParentsCount(); i++)
((NeuralNode) getParentAt(i)).updateWeights(learningRate, momentum);
weightsUpdated = true;
}
}
// -----------------------------------------------------------------------
// Further methods
// -----------------------------------------------------------------------
/**
* Delivers string representation of neural node.
*
* @return string representation of neural node
*/
public String toString() {
String s = "ID = " + id;
int npars = getParentsCount();
if (npars > 0) {
s = s + " inputs: ";
for (int i = 0; i < npars; i++)
s = s + ((NeuralNode) getParentAt(i)).getId() + " ";
};
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -