org.encog.neural.networks
Class BasicNetwork

java.lang.Object
  extended by org.encog.ml.BasicML
      extended by org.encog.neural.networks.BasicNetwork
All Implemented Interfaces:
Serializable, MLClassification, MLContext, MLEncodable, MLError, MLInput, MLInputOutput, MLMethod, MLOutput, MLProperties, MLRegression, MLResettable, ContainsFlat

public class BasicNetwork
extends BasicML
implements ContainsFlat, MLContext, MLRegression, MLEncodable, MLResettable, MLClassification, MLError

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
static double DEFAULT_CONNECTION_LIMIT
          The default connection limit.
static String TAG_BEGIN_TRAINING
          The property for begin training.
static String TAG_BIAS_ACTIVATION
          The property for bias activation.
static String TAG_CONNECTION_LIMIT
          The property for connection limit.
static String TAG_CONTEXT_TARGET_OFFSET
          The property for context target offset.
static String TAG_CONTEXT_TARGET_SIZE
          The property for context target size.
static String TAG_END_TRAINING
          The property for end training.
static String TAG_HAS_CONTEXT
          The property for has context.
static String TAG_LAYER_CONTEXT_COUNT
          The property for layer context count.
static String TAG_LAYER_COUNTS
          The property for layer counts.
static String TAG_LAYER_FEED_COUNTS
          The property for layer feed counts.
static String TAG_LAYER_INDEX
          The property for layer index.
static String TAG_LIMIT
          Tag used for the connection limit.
static String TAG_WEIGHT_INDEX
          The property for weight index.
 
Constructor Summary
BasicNetwork()
          Construct an empty neural network.
 
Method Summary
 void addLayer(Layer layer)
          Add a layer to the neural network.
 void addWeight(int fromLayer, int fromNeuron, int toNeuron, double value)
          Add to a weight.
 double calculateError(MLDataSet data)
          Calculate the error for this neural network.
 int calculateNeuronCount()
          Calculate the total number of neurons in the network across all layers.
 int classify(MLData input)
          Classify the input into a group.
 void clearContext()
          Clear any data from any context layers.
 Object clone()
          Return a clone of this neural network.
 void compute(double[] input, double[] output)
          Compute the output for this network.
 MLData compute(MLData input)
          Compute the output for a given input to the neural network.
 void decodeFromArray(double[] encoded)
          Decode an array to this object.
 String dumpWeights()
           
 void enableConnection(int fromLayer, int fromNeuron, int toNeuron, boolean enable)
          Enable, or disable, a connection.
 int encodedArrayLength()
          
 void encodeToArray(double[] encoded)
          Encode the object to the specified array.
 boolean equals(BasicNetwork other)
          Compare the two neural networks.
 boolean equals(BasicNetwork other, int precision)
          Determine if this neural network is equal to another.
 ActivationFunction getActivation(int layer)
          Get the activation function for the specified layer.
 FlatNetwork getFlat()
          
 int getInputCount()
          
 double getLayerBiasActivation(int l)
          Get the bias activation for the specified layer.
 int getLayerCount()
           
 int getLayerNeuronCount(int l)
          Get the neuron count.
 double getLayerOutput(int layer, int neuronNumber)
          Get the layer output for the specified neuron.
 int getLayerTotalNeuronCount(int l)
          Get the total (including bias and context) neuron cont for a layer.
 int getOutputCount()
          
 NeuralStructure getStructure()
           
 double getWeight(int fromLayer, int fromNeuron, int toNeuron)
          Get the weight between the two layers.
 int hashCode()
          Generate a hash code.
 boolean isConnected(int layer, int fromNeuron, int toNeuron)
          Determine if the specified connection is enabled.
 boolean isLayerBiased(int l)
          Determine if the specified layer is biased.
 void reset()
          Reset the weight matrix and the bias values.
 void reset(int seed)
          Reset the weight matrix and the bias values.
 void setBiasActivation(double activation)
          Sets the bias activation for every layer that supports bias.
 void setLayerBiasActivation(int l, double value)
          Set the bias activation for the specified layer.
 void setWeight(int fromLayer, int fromNeuron, int toNeuron, double value)
          Set the weight between the two specified neurons.
 String toString()
          
 void updateProperties()
          Update any objeccts when a property changes.
 void validateNeuron(int targetLayer, int neuron)
          Validate the the specified targetLayer and neuron are valid.
 int winner(MLData input)
          Determine the winner for the specified input.
 
Methods inherited from class org.encog.ml.BasicML
getProperties, getPropertyDouble, getPropertyLong, getPropertyString, setProperty, setProperty, setProperty
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

TAG_LIMIT

public static final 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 String TAG_CONNECTION_LIMIT
The property for connection limit.

See Also:
Constant Field Values

TAG_BEGIN_TRAINING

public static final String TAG_BEGIN_TRAINING
The property for begin training.

See Also:
Constant Field Values

TAG_CONTEXT_TARGET_OFFSET

public static final String TAG_CONTEXT_TARGET_OFFSET
The property for context target offset.

See Also:
Constant Field Values

TAG_CONTEXT_TARGET_SIZE

public static final String TAG_CONTEXT_TARGET_SIZE
The property for context target size.

See Also:
Constant Field Values

TAG_END_TRAINING

public static final String TAG_END_TRAINING
The property for end training.

See Also:
Constant Field Values

TAG_HAS_CONTEXT

public static final String TAG_HAS_CONTEXT
The property for has context.

See Also:
Constant Field Values

TAG_LAYER_COUNTS

public static final String TAG_LAYER_COUNTS
The property for layer counts.

See Also:
Constant Field Values

TAG_LAYER_FEED_COUNTS

public static final String TAG_LAYER_FEED_COUNTS
The property for layer feed counts.

See Also:
Constant Field Values

TAG_LAYER_INDEX

public static final String TAG_LAYER_INDEX
The property for layer index.

See Also:
Constant Field Values

TAG_WEIGHT_INDEX

public static final String TAG_WEIGHT_INDEX
The property for weight index.

See Also:
Constant Field Values

TAG_BIAS_ACTIVATION

public static final String TAG_BIAS_ACTIVATION
The property for bias activation.

See Also:
Constant Field Values

TAG_LAYER_CONTEXT_COUNT

public static final String TAG_LAYER_CONTEXT_COUNT
The property for layer context count.

See Also:
Constant Field Values
Constructor Detail

BasicNetwork

public BasicNetwork()
Construct an empty neural network.

Method Detail

addLayer

public final 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 final 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 final double calculateError(MLDataSet data)
Calculate the error for this neural network.

Specified by:
calculateError in interface MLError
Parameters:
data - The training set.
Returns:
The error percentage.

calculateNeuronCount

public final int calculateNeuronCount()
Calculate the total number of neurons in the network across all layers.

Returns:
The neuron count.

classify

public final int classify(MLData input)
Classify the input into a group.

Specified by:
classify in interface MLClassification
Parameters:
input - The input data to classify.
Returns:
The group that the data was classified into.

clearContext

public final void clearContext()
Clear any data from any context layers.

Specified by:
clearContext in interface MLContext

clone

public final Object clone()
Return a clone of this neural network. Including structure, weights and bias values. This is a deep copy.

Overrides:
clone in class Object
Returns:
A cloned copy of the neural network.

compute

public final void compute(double[] input,
                          double[] output)
Compute the output for this network.

Parameters:
input - The input.
output - The output.

compute

public final MLData compute(MLData input)
Compute the output for a given input to the neural network.

Specified by:
compute in interface MLRegression
Parameters:
input - The input to the neural network.
Returns:
The output from the neural network.

decodeFromArray

public final void decodeFromArray(double[] encoded)
Decode an array to this object.

Specified by:
decodeFromArray in interface MLEncodable
Parameters:
encoded - The encoded array.

dumpWeights

public final String dumpWeights()
Returns:
The weights as a comma separated list.

enableConnection

public final 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 final int encodedArrayLength()

Specified by:
encodedArrayLength in interface MLEncodable
Returns:
The length of an encoded array.

encodeToArray

public final void encodeToArray(double[] encoded)
Encode the object to the specified array.

Specified by:
encodeToArray in interface MLEncodable
Parameters:
encoded - The array.

equals

public final boolean equals(BasicNetwork other)
Compare the two neural networks. For them to be equal they must be of the same structure, and have the same matrix values.

Parameters:
other - The other neural network.
Returns:
True if the two networks are equal.

equals

public final 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 final ActivationFunction getActivation(int layer)
Get the activation function for the specified layer.

Parameters:
layer - The layer.
Returns:
The activation function.

getFlat

public final FlatNetwork getFlat()

Specified by:
getFlat in interface ContainsFlat
Returns:
The flat network associated with this neural network.

getInputCount

public final int getInputCount()

Specified by:
getInputCount in interface MLInput
Returns:
The input.

getLayerBiasActivation

public final double getLayerBiasActivation(int l)
Get the bias activation for the specified layer.

Parameters:
l - The layer.
Returns:
The bias activation.

getLayerCount

public final int getLayerCount()
Returns:
The layer count.

getLayerNeuronCount

public final int getLayerNeuronCount(int l)
Get the neuron count.

Parameters:
l - The layer.
Returns:
The neuron count.

getLayerOutput

public final 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 final 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 final int getOutputCount()

Specified by:
getOutputCount in interface MLOutput
Returns:
The output count.

getStructure

public final 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 final 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 final int hashCode()
Generate a hash code.

Overrides:
hashCode in class Object
Returns:
THe hash code.

isConnected

public final 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 final 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 final 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:
reset in interface MLResettable

reset

public final void reset(int seed)
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. Use the specified seed.

Specified by:
reset in interface MLResettable
Parameters:
seed - The seed value.

setBiasActivation

public final 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 final 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 final void setWeight(int fromLayer,
                            int fromNeuron,
                            int toNeuron,
                            double value)
Set the weight between the two specified neurons.

Parameters:
fromLayer - The from layer.
fromNeuron - The from neuron.
toNeuron - The to neuron.
value - The to value.

toString

public final String toString()

Overrides:
toString in class Object

updateProperties

public final void updateProperties()
Update any objeccts when a property changes.

Specified by:
updateProperties in interface MLProperties
Specified by:
updateProperties in class BasicML

validateNeuron

public final 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 final 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.


Copyright © 2011. All Rights Reserved.