The Encog Project

org.encog.neural.networks
Class BasicNetwork

java.lang.Object
  extended by org.encog.neural.networks.BasicNetwork
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Network, EncogPersistedObject

public class BasicNetwork
extends java.lang.Object
implements java.io.Serializable, Network

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

Constructor Summary
BasicNetwork()
          Construct an empty neural network.
 
Method Summary
 void addLayer(Layer layer)
          Add a layer to the neural network.
 void addLayer(Layer layer, SynapseType type)
          Add a layer to the neural network.
 double calculateError(NeuralDataSet data)
          Calculate the error for this neural network.
 int calculateNeuronCount()
          Calculate the total number of neurons in the network across all layers.
 void checkInputSize(NeuralData input)
          Check that the input size is acceptable, if it does not match the input layer, then throw an error.
 java.lang.Object clone()
          Return a clone of this neural network.
 boolean compareLayer(Layer layerThis, Layer layerOther, int precision)
          Used to compare one neural network to another, compare two layers.
 NeuralData compute(NeuralData input)
          Compute the output for a given input to the neural network.
 NeuralData compute(NeuralData input, NeuralOutputHolder useHolder)
          Compute the output for a given input to the neural network.
 Persistor createPersistor()
          Create a persistor for this object.
 boolean equals(BasicNetwork other)
          Compare the two neural networks.
 boolean equals(BasicNetwork other, int precision)
          Determine if this neural network is equal to another.
 java.lang.String getDescription()
           
 int getHiddenLayerCount()
          Get the count for how many hidden layers are present.
 java.util.Collection<Layer> getHiddenLayers()
          Get a collection of the hidden layers in the network.
 Layer getInputLayer()
          Get the input layer.
 java.lang.String getName()
           
 Layer getOutputLayer()
          Get the output layer.
 NeuralStructure getStructure()
           
 int getWeightMatrixSize()
           
 int hashCode()
          Generate a hash code.
 void inferOutputLayer()
          Called to cause the network to attempt to infer which layer should be the output layer.
 boolean isHidden(Layer layer)
          Determine if this layer is hidden.
 boolean isInput(Layer layer)
          Determine if this layer is the input layer.
 boolean isOutput(Layer layer)
          Determine if this layer is the output layer.
 void reset()
          Reset the weight matrix and the thresholds.
 void setDescription(java.lang.String theDescription)
          Set the description for this object.
 void setInputLayer(Layer input)
          Define the input layer for the network.
 void setName(java.lang.String name)
          Set the name of this object.
 void setOutputLayer(Layer outputLayer)
           
 java.lang.String toString()
           
 int winner(NeuralData input)
          Determine the winner for the specified input.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BasicNetwork

public BasicNetwork()
Construct an empty neural network.

Method Detail

addLayer

public void addLayer(Layer layer)
Add a layer to the neural network. The first layer added is the input layer, the last layer added is the output layer. This layer is added with a weighted synapse.

Specified by:
addLayer in interface Network
Parameters:
layer - The layer to be added.

addLayer

public void addLayer(Layer layer,
                     SynapseType type)
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.

Specified by:
addLayer in interface Network
Parameters:
layer - The layer to be added to the network.
type - What sort of synapse should connect this layer to the last.

calculateError

public double calculateError(NeuralDataSet data)
Calculate the error for this neural network. The error is calculated using root-mean-square(RMS).

Specified by:
calculateError in interface Network
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.

Specified by:
calculateNeuronCount in interface Network
Returns:
The neuron count.

checkInputSize

public void checkInputSize(NeuralData input)
Check that the input size is acceptable, if it does not match the input layer, then throw an error.

Specified by:
checkInputSize in interface Network
Parameters:
input - The input data.

clone

public java.lang.Object clone()
Return a clone of this neural network. Including structure, weights and threshold values.

Specified by:
clone in interface Network
Specified by:
clone in interface EncogPersistedObject
Overrides:
clone in class java.lang.Object
Returns:
A cloned copy of the neural network.

compareLayer

public boolean compareLayer(Layer layerThis,
                            Layer layerOther,
                            int precision)
Used to compare one neural network to another, compare two layers.

Specified by:
compareLayer in interface Network
Parameters:
layerThis - The layer being compared.
layerOther - The other layer.
precision - The precision to use, how many decimal places.
Returns:
Returns true if the two layers are the same.

compute

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

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

compute

public NeuralData compute(NeuralData input,
                          NeuralOutputHolder useHolder)
Compute the output for a given input to the neural network. This method provides a parameter to specify an output holder to use. This holder allows propagation training to track the output from each layer. If you do not need this holder pass null, or use the other compare method.

Specified by:
compute in interface Network
Parameters:
input - The input provide to the neural network.
useHolder - Allows a holder to be specified, this allows propagation training to check the output of each layer.
Returns:
The results from the output neurons.

createPersistor

public Persistor createPersistor()
Create a persistor for this object.

Specified by:
createPersistor in interface Network
Specified by:
createPersistor in interface EncogPersistedObject
Returns:
The newly created persistor.

equals

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

Specified by:
equals in interface Network
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 threshold values, within a specified precision.

Specified by:
equals in interface Network
Parameters:
other - The other neural network.
precision - The number of decimal places to compare to.
Returns:
True if the two neural networks are equal.

getDescription

public java.lang.String getDescription()
Specified by:
getDescription in interface Network
Specified by:
getDescription in interface EncogPersistedObject
Returns:
The description for this object.

getHiddenLayerCount

public int getHiddenLayerCount()
Get the count for how many hidden layers are present.

Specified by:
getHiddenLayerCount in interface Network
Returns:
The hidden layer count.

getHiddenLayers

public java.util.Collection<Layer> getHiddenLayers()
Get a collection of the hidden layers in the network.

Specified by:
getHiddenLayers in interface Network
Returns:
The hidden layers.

getInputLayer

public Layer getInputLayer()
Get the input layer.

Specified by:
getInputLayer in interface Network
Returns:
The input layer.

getName

public java.lang.String getName()
Specified by:
getName in interface Network
Specified by:
getName in interface EncogPersistedObject
Returns:
the name

getOutputLayer

public Layer getOutputLayer()
Get the output layer.

Specified by:
getOutputLayer in interface Network
Returns:
The output layer.

getStructure

public NeuralStructure getStructure()
Specified by:
getStructure in interface Network
Returns:
Get the structure of the neural network. The structure allows you to quickly obtain synapses and layers without traversing the network.

getWeightMatrixSize

public int getWeightMatrixSize()
Specified by:
getWeightMatrixSize in interface Network
Returns:
The size of the matrix.

hashCode

public int hashCode()
Generate a hash code.

Specified by:
hashCode in interface Network
Overrides:
hashCode in class java.lang.Object
Returns:
THe hash code.

inferOutputLayer

public void inferOutputLayer()
Called to cause the network to attempt to infer which layer should be the output layer.

Specified by:
inferOutputLayer in interface Network

isHidden

public boolean isHidden(Layer layer)
Determine if this layer is hidden.

Specified by:
isHidden in interface Network
Parameters:
layer - The layer to evaluate.
Returns:
True if this layer is a hidden layer.

isInput

public boolean isInput(Layer layer)
Determine if this layer is the input layer.

Specified by:
isInput in interface Network
Parameters:
layer - The layer to evaluate.
Returns:
True if this layer is the input layer.

isOutput

public boolean isOutput(Layer layer)
Determine if this layer is the output layer.

Specified by:
isOutput in interface Network
Parameters:
layer - The layer to evaluate.
Returns:
True if this layer is the output layer.

reset

public void reset()
Reset the weight matrix and the thresholds.

Specified by:
reset in interface Network

setDescription

public void setDescription(java.lang.String theDescription)
Set the description for this object.

Specified by:
setDescription in interface Network
Specified by:
setDescription in interface EncogPersistedObject
Parameters:
theDescription - The description.

setInputLayer

public void setInputLayer(Layer input)
Define the input layer for the network.

Specified by:
setInputLayer in interface Network
Parameters:
input - The new input layer.

setName

public void setName(java.lang.String name)
Description copied from interface: EncogPersistedObject
Set the name of this object.

Specified by:
setName in interface Network
Specified by:
setName in interface EncogPersistedObject
Parameters:
name - the name to set

setOutputLayer

public void setOutputLayer(Layer outputLayer)
Specified by:
setOutputLayer in interface Network
Parameters:
outputLayer - the outputLayer to set

toString

public java.lang.String toString()
Specified by:
toString in interface Network
Overrides:
toString in class java.lang.Object
Returns:
Convert this object to a string.

winner

public int winner(NeuralData input)
Determine the winner for the specified input. This is the number of the winning neuron.

Specified by:
winner in interface Network
Parameters:
input - The input patter to present to the neural network.
Returns:
The winning neuron.

The Encog Project