org.encog.neural.networks
Class BasicNetwork
- java.lang.Object
-
- org.encog.ml.BasicML
-
- org.encog.neural.networks.BasicNetwork
-
- All Implemented Interfaces:
- java.io.Serializable, MLClassification, MLContext, MLEncodable, MLError, MLFactory, MLInput, MLInputOutput, MLMethod, MLOutput, MLProperties, MLRegression, MLResettable, ContainsFlat
public class BasicNetwork extends BasicML implements ContainsFlat, MLContext, MLRegression, MLEncodable, MLResettable, MLClassification, MLError, MLFactory
This class implements a neural network. This class works in conjunction the Layer classes. Layers are added to the BasicNetwork to specify the structure of the neural network. The first layer added is the input layer, the final layer added is the output layer. Any layers added between these two layers are the hidden layers. The network structure is stored in the structure member. It is important to call: network.getStructure().finalizeStructure(); Once the neural network has been completely constructed.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static doubleDEFAULT_CONNECTION_LIMITThe default connection limit.static java.lang.StringTAG_BEGIN_TRAININGThe property for begin training.static java.lang.StringTAG_BIAS_ACTIVATIONThe property for bias activation.static java.lang.StringTAG_CONNECTION_LIMITThe property for connection limit.static java.lang.StringTAG_CONTEXT_TARGET_OFFSETThe property for context target offset.static java.lang.StringTAG_CONTEXT_TARGET_SIZEThe property for context target size.static java.lang.StringTAG_END_TRAININGThe property for end training.static java.lang.StringTAG_HAS_CONTEXTThe property for has context.static java.lang.StringTAG_LAYER_CONTEXT_COUNTThe property for layer context count.static java.lang.StringTAG_LAYER_COUNTSThe property for layer counts.static java.lang.StringTAG_LAYER_FEED_COUNTSThe property for layer feed counts.static java.lang.StringTAG_LAYER_INDEXThe property for layer index.static java.lang.StringTAG_LIMITTag used for the connection limit.static java.lang.StringTAG_WEIGHT_INDEXThe property for weight index.
-
Constructor Summary
Constructors Constructor and Description BasicNetwork()Construct an empty neural network.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddLayer(Layer layer)Add a layer to the neural network.voidaddWeight(int fromLayer, int fromNeuron, int toNeuron, double value)Add to a weight.doublecalculateError(MLDataSet data)Calculate the error for this neural network.intcalculateNeuronCount()Calculate the total number of neurons in the network across all layers.intclassify(MLData input)Classify the input into a group.voidclearContext()Clear any data from any context layers.java.lang.Objectclone()Return a clone of this neural network.voidcompute(double[] input, double[] output)Compute the output for this network.MLDatacompute(MLData input)Compute the output for a given input to the neural network.voiddecodeFromArray(double[] encoded)Decode an array to this object.java.lang.StringdumpWeights()java.lang.StringdumpWeightsVerbose()voidenableConnection(int fromLayer, int fromNeuron, int toNeuron, boolean enable)Enable, or disable, a connection.intencodedArrayLength()voidencodeToArray(double[] encoded)Encode the object to the specified array.booleanequals(BasicNetwork other, int precision)Determine if this neural network is equal to another.booleanequals(java.lang.Object other)Compare the two neural networks.ActivationFunctiongetActivation(int layer)Get the activation function for the specified layer.java.lang.StringgetFactoryArchitecture()java.lang.StringgetFactoryType()FlatNetworkgetFlat()intgetInputCount()doublegetLayerBiasActivation(int l)Get the bias activation for the specified layer.intgetLayerCount()intgetLayerNeuronCount(int l)Get the neuron count.doublegetLayerOutput(int layer, int neuronNumber)Get the layer output for the specified neuron.intgetLayerTotalNeuronCount(int l)Get the total (including bias and context) neuron cont for a layer.intgetOutputCount()NeuralStructuregetStructure()doublegetWeight(int fromLayer, int fromNeuron, int toNeuron)Get the weight between the two layers.inthashCode()Generate a hash code.booleanisConnected(int layer, int fromNeuron, int toNeuron)Determine if the specified connection is enabled.booleanisLayerBiased(int l)Determine if the specified layer is biased.voidreset()Reset the weight matrix and the bias values.voidreset(int seed)Reset the weight matrix and the bias values.voidsetBiasActivation(double activation)Sets the bias activation for every layer that supports bias.voidsetLayerBiasActivation(int l, double value)Set the bias activation for the specified layer.voidsetWeight(int fromLayer, int fromNeuron, int toNeuron, double value)Set the weight between the two specified neurons.java.lang.StringtoString()voidupdateProperties()Update any objeccts when a property changes.voidvalidateNeuron(int targetLayer, int neuron)Validate the the specified targetLayer and neuron are valid.intwinner(MLData input)Determine the winner for the specified input.-
Methods inherited from class org.encog.ml.BasicML
getProperties, getPropertyDouble, getPropertyLong, getPropertyString, setProperty, setProperty, setProperty
-
-
-
-
Field Detail
-
TAG_LIMIT
public static final java.lang.String TAG_LIMIT
Tag used for the connection limit.- See Also:
- Constant Field Values
-
DEFAULT_CONNECTION_LIMIT
public static final double DEFAULT_CONNECTION_LIMIT
The default connection limit.- See Also:
- Constant Field Values
-
TAG_CONNECTION_LIMIT
public static final java.lang.String TAG_CONNECTION_LIMIT
The property for connection limit.- See Also:
- Constant Field Values
-
TAG_BEGIN_TRAINING
public static final java.lang.String TAG_BEGIN_TRAINING
The property for begin training.- See Also:
- Constant Field Values
-
TAG_CONTEXT_TARGET_OFFSET
public static final java.lang.String TAG_CONTEXT_TARGET_OFFSET
The property for context target offset.- See Also:
- Constant Field Values
-
TAG_CONTEXT_TARGET_SIZE
public static final java.lang.String TAG_CONTEXT_TARGET_SIZE
The property for context target size.- See Also:
- Constant Field Values
-
TAG_END_TRAINING
public static final java.lang.String TAG_END_TRAINING
The property for end training.- See Also:
- Constant Field Values
-
TAG_HAS_CONTEXT
public static final java.lang.String TAG_HAS_CONTEXT
The property for has context.- See Also:
- Constant Field Values
-
TAG_LAYER_COUNTS
public static final java.lang.String TAG_LAYER_COUNTS
The property for layer counts.- See Also:
- Constant Field Values
-
TAG_LAYER_FEED_COUNTS
public static final java.lang.String TAG_LAYER_FEED_COUNTS
The property for layer feed counts.- See Also:
- Constant Field Values
-
TAG_LAYER_INDEX
public static final java.lang.String TAG_LAYER_INDEX
The property for layer index.- See Also:
- Constant Field Values
-
TAG_WEIGHT_INDEX
public static final java.lang.String TAG_WEIGHT_INDEX
The property for weight index.- See Also:
- Constant Field Values
-
TAG_BIAS_ACTIVATION
public static final java.lang.String TAG_BIAS_ACTIVATION
The property for bias activation.- See Also:
- Constant Field Values
-
TAG_LAYER_CONTEXT_COUNT
public static final java.lang.String TAG_LAYER_CONTEXT_COUNT
The property for layer context count.- See Also:
- Constant Field Values
-
-
Method Detail
-
addLayer
public void addLayer(Layer layer)
Add a layer to the neural network. If there are no layers added this layer will become the input layer. This function automatically updates both the input and output layer references.- Parameters:
layer- The layer to be added to the network.
-
addWeight
public void addWeight(int fromLayer, int fromNeuron, int toNeuron, double value)Add to a weight.- Parameters:
fromLayer- The from layer.fromNeuron- The from neuron.toNeuron- The to neuron.value- The value to add.
-
calculateError
public double calculateError(MLDataSet data)
Calculate the error for this neural network. We always calculate the error using the "regression" calculator. Neural networks don't directly support classification, rather they use one-of-encoding or similar. So just using the regression calculator gives a good approximation.- Specified by:
calculateErrorin interfaceMLError- Parameters:
data- The training set.- Returns:
- The error percentage.
-
calculateNeuronCount
public int calculateNeuronCount()
Calculate the total number of neurons in the network across all layers.- Returns:
- The neuron count.
-
classify
public int classify(MLData input)
Classify the input into a group.- Specified by:
classifyin interfaceMLClassification- Parameters:
input- The input data to classify.- Returns:
- The group that the data was classified into.
-
clearContext
public void clearContext()
Clear any data from any context layers.- Specified by:
clearContextin interfaceMLContext
-
clone
public java.lang.Object clone()
Return a clone of this neural network. Including structure, weights and bias values. This is a deep copy.- Overrides:
clonein classjava.lang.Object- Returns:
- A cloned copy of the neural network.
-
compute
public void compute(double[] input, double[] output)Compute the output for this network.- Parameters:
input- The input.output- The output.
-
compute
public MLData compute(MLData input)
Compute the output for a given input to the neural network.- Specified by:
computein interfaceMLRegression- Parameters:
input- The input to the neural network.- Returns:
- The output from the neural network.
-
decodeFromArray
public void decodeFromArray(double[] encoded)
Decode an array to this object.- Specified by:
decodeFromArrayin interfaceMLEncodable- Parameters:
encoded- The encoded array.
-
dumpWeights
public java.lang.String dumpWeights()
- Returns:
- The weights as a comma separated list.
-
dumpWeightsVerbose
public java.lang.String dumpWeightsVerbose()
-
enableConnection
public void enableConnection(int fromLayer, int fromNeuron, int toNeuron, boolean enable)Enable, or disable, a connection.- Parameters:
fromLayer- The layer that contains the from neuron.fromNeuron- The source neuron.toNeuron- The target connection.enable- True to enable, false to disable.
-
encodedArrayLength
public int encodedArrayLength()
- Specified by:
encodedArrayLengthin interfaceMLEncodable- Returns:
- The length of an encoded array.
-
encodeToArray
public void encodeToArray(double[] encoded)
Encode the object to the specified array.- Specified by:
encodeToArrayin interfaceMLEncodable- Parameters:
encoded- The array.
-
equals
public boolean equals(java.lang.Object other)
Compare the two neural networks. For them to be equal they must be of the same structure, and have the same matrix values.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- The other neural network.- Returns:
- True if the two networks are equal.
-
equals
public boolean equals(BasicNetwork other, int precision)
Determine if this neural network is equal to another. Equal neural networks have the same weight matrix and bias values, within a specified precision.- Parameters:
other- The other neural network.precision- The number of decimal places to compare to.- Returns:
- True if the two neural networks are equal.
-
getActivation
public ActivationFunction getActivation(int layer)
Get the activation function for the specified layer.- Parameters:
layer- The layer.- Returns:
- The activation function.
-
getFlat
public FlatNetwork getFlat()
- Specified by:
getFlatin interfaceContainsFlat- Returns:
- The flat network associated with this neural network.
-
getInputCount
public int getInputCount()
- Specified by:
getInputCountin interfaceMLInput- Returns:
- The input.
-
getLayerBiasActivation
public double getLayerBiasActivation(int l)
Get the bias activation for the specified layer.- Parameters:
l- The layer.- Returns:
- The bias activation.
-
getLayerCount
public int getLayerCount()
- Returns:
- The layer count.
-
getLayerNeuronCount
public int getLayerNeuronCount(int l)
Get the neuron count.- Parameters:
l- The layer.- Returns:
- The neuron count.
-
getLayerOutput
public double getLayerOutput(int layer, int neuronNumber)Get the layer output for the specified neuron.- Parameters:
layer- The layer.neuronNumber- The neuron number.- Returns:
- The output from the last call to compute.
-
getLayerTotalNeuronCount
public int getLayerTotalNeuronCount(int l)
Get the total (including bias and context) neuron cont for a layer.- Parameters:
l- The layer.- Returns:
- The count.
-
getOutputCount
public int getOutputCount()
- Specified by:
getOutputCountin interfaceMLOutput- Returns:
- The output count.
-
getStructure
public NeuralStructure getStructure()
- Returns:
- Get the structure of the neural network. The structure allows you to quickly obtain synapses and layers without traversing the network.
-
getWeight
public double getWeight(int fromLayer, int fromNeuron, int toNeuron)Get the weight between the two layers.- Parameters:
fromLayer- The from layer.fromNeuron- The from neuron.toNeuron- The to neuron.- Returns:
- The weight value.
-
hashCode
public int hashCode()
Generate a hash code.- Overrides:
hashCodein classjava.lang.Object- Returns:
- THe hash code.
-
isConnected
public boolean isConnected(int layer, int fromNeuron, int toNeuron)Determine if the specified connection is enabled.- Parameters:
layer- The layer to check.fromNeuron- The source neuron.toNeuron- THe target neuron.- Returns:
- True, if the connection is enabled, false otherwise.
-
isLayerBiased
public boolean isLayerBiased(int l)
Determine if the specified layer is biased.- Parameters:
l- The layer number.- Returns:
- True, if the layer is biased.
-
reset
public void reset()
Reset the weight matrix and the bias values. This will use a Nguyen-Widrow randomizer with a range between -1 and 1. If the network does not have an input, output or hidden layers, then Nguyen-Widrow cannot be used and a simple range randomize between -1 and 1 will be used.- Specified by:
resetin interfaceMLResettable
-
reset
public void reset(int seed)
Reset the weight matrix and the bias values. This will use a RangeRandomizer with a range between -1 and 1.- Specified by:
resetin interfaceMLResettable- Parameters:
seed- The seed value.
-
setBiasActivation
public void setBiasActivation(double activation)
Sets the bias activation for every layer that supports bias. Make sure that the network structure has been finalized before calling this method.- Parameters:
activation- THe new activation.
-
setLayerBiasActivation
public void setLayerBiasActivation(int l, double value)Set the bias activation for the specified layer.- Parameters:
l- The layer to use.value- The bias activation.
-
setWeight
public void setWeight(int fromLayer, int fromNeuron, int toNeuron, double value)Set the weight between the two specified neurons. The bias neuron is always the last neuron on a layer.- Parameters:
fromLayer- The from layer.fromNeuron- The from neuron.toNeuron- The to neuron.value- The to value.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
updateProperties
public void updateProperties()
Update any objeccts when a property changes.- Specified by:
updatePropertiesin interfaceMLProperties- Specified by:
updatePropertiesin classBasicML
-
validateNeuron
public void validateNeuron(int targetLayer, int neuron)Validate the the specified targetLayer and neuron are valid.- Parameters:
targetLayer- The target layer.neuron- The target neuron.
-
winner
public int winner(MLData input)
Determine the winner for the specified input. This is the number of the winning neuron.- Parameters:
input- The input patter to present to the neural network.- Returns:
- The winning neuron.
-
getFactoryType
public java.lang.String getFactoryType()
- Specified by:
getFactoryTypein interfaceMLFactory- Returns:
- The Encog factory type code.
-
getFactoryArchitecture
public java.lang.String getFactoryArchitecture()
- Specified by:
getFactoryArchitecturein interfaceMLFactory- Returns:
- The Encog architecture code.
-
-
DMelt 3.0 © DataMelt by jWork.ORG