org.encog.neural.networks
Class BasicNetwork

java.lang.Object
  extended by org.encog.persist.BasicPersistedObject
      extended by org.encog.neural.networks.BasicNetwork
All Implemented Interfaces:
Serializable, EngineMachineLearning, ContextClearable, Network, EncogPersistedObject
Direct Known Subclasses:
SVMNetwork

public class BasicNetwork
extends BasicPersistedObject
implements Serializable, Network, ContextClearable

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 String DEFAULT_CONNECTION_LIMIT
           
static String TAG_INPUT
          Tag used for the input layer.
static String TAG_LIMIT
          Tag used for the connection limit.
static String TAG_OUTPUT
          Tag used for the output layer.
 
Constructor Summary
BasicNetwork()
          Construct an empty neural network.
BasicNetwork(NeuralLogic logic)
          Construct a basic network using the specified logic.
 
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 clearContext()
          Clear any data from any context layers.
 void clearLayerTags()
          Remove all layer tags.
 Object clone()
          Return a clone of this neural network.
 void compute(double[] input, double[] output)
          Compute output for the given input.
 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.
static int determineWinner(NeuralData output)
          Determine which member of the output is the winning neuron.
 String dumpWeights()
           
 void enableConnection(Synapse synapse, int fromNeuron, int toNeuron, boolean enable)
          Enable, or disable, a connection.
 boolean equals(BasicNetwork other)
          Compare the two neural networks.
 boolean equals(BasicNetwork other, int precision)
          Determine if this neural network is equal to another.
 int getInputCount()
          
 Layer getLayer(String tag)
          Get the layer specified by the tag.
 Map<String,Layer> getLayerTags()
           
 NeuralLogic getLogic()
           
 int getOutputCount()
          
 Map<String,String> getProperties()
           
 double getPropertyDouble(String name)
          Get the specified property as a double.
 long getPropertyLong(String name)
          Get the specified property as a long.
 String getPropertyString(String name)
          Get the specified property as a string.
 NeuralStructure getStructure()
           
 Collection<String> getTags(Layer layer)
          Get a list of all of the tags on a specific layer.
 int getWeightMatrixSize()
           
 int hashCode()
          Generate a hash code.
 boolean isConnected(Synapse synapse, int fromNeuron, int toNeuron)
          Determine if the specified connection is enabled.
 void reset()
          Reset the weight matrix and the bias values.
 void setBiasActivation(double activation)
          Sets the bias activation for every layer that supports bias.
 void setLogic(NeuralLogic logic)
          Set the type of logic this network should use.
 void setProperty(String name, double d)
          Set a property as a double.
 void setProperty(String name, long l)
          Set a property as a long.
 void setProperty(String name, String value)
          Set a property as a double.
 void tagLayer(String tag, Layer layer)
          Tag a layer.
 String toString()
          
 int winner(NeuralData input)
          Determine the winner for the specified input.
 
Methods inherited from class org.encog.persist.BasicPersistedObject
getCollection, getDescription, getName, setCollection, setDescription, setName
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.encog.neural.networks.Network
getDescription, getName, setDescription, setName
 
Methods inherited from interface org.encog.persist.EncogPersistedObject
getCollection, setCollection
 

Field Detail

TAG_INPUT

public static final String TAG_INPUT
Tag used for the input layer.

See Also:
Constant Field Values

TAG_OUTPUT

public static final String TAG_OUTPUT
Tag used for the output layer.

See Also:
Constant Field Values

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 String DEFAULT_CONNECTION_LIMIT
See Also:
Constant Field Values
Constructor Detail

BasicNetwork

public BasicNetwork()
Construct an empty neural network.


BasicNetwork

public BasicNetwork(NeuralLogic logic)
Construct a basic network using the specified logic.

Parameters:
logic - The logic to use with the neural network.
Method Detail

determineWinner

public static int determineWinner(NeuralData output)
Determine which member of the output is the winning neuron.

Parameters:
output - The output from the neural network.
Returns:
The winning neuron.

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.

clearContext

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

Specified by:
clearContext in interface ContextClearable

clearLayerTags

public void clearLayerTags()
Remove all layer tags.


clone

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

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

compute

public void compute(double[] input,
                    double[] output)
Compute output for the given input.

Specified by:
compute in interface EngineMachineLearning
Parameters:
input - An array of doubles for the input.
output - An array of doubles for the output.

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
Overrides:
createPersistor in class BasicPersistedObject
Returns:
The newly created persistor.

dumpWeights

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

enableConnection

public void enableConnection(Synapse synapse,
                             int fromNeuron,
                             int toNeuron,
                             boolean enable)
Enable, or disable, a connection.

Parameters:
synapse - The synapse that contains the connection.
fromNeuron - The source neuron.
toNeuron - The target connection.
enable - True to enable, false to disable.

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

getInputCount

public int getInputCount()

Specified by:
getInputCount in interface EngineMachineLearning
Returns:
The input count.

getLayer

public Layer getLayer(String tag)
Get the layer specified by the tag.

Parameters:
tag - The tag.
Returns:
The layer associated with that tag.

getLayerTags

public Map<String,Layer> getLayerTags()
Returns:
The map of all layer tags.

getLogic

public NeuralLogic getLogic()
Returns:
The logic used by this network.

getOutputCount

public int getOutputCount()

Specified by:
getOutputCount in interface EngineMachineLearning
Returns:
The output size.

getProperties

public Map<String,String> getProperties()
Returns:
A map of all properties.

getPropertyDouble

public double getPropertyDouble(String name)
Get the specified property as a double.

Parameters:
name - The name of the property.
Returns:
The property as a double.

getPropertyLong

public long getPropertyLong(String name)
Get the specified property as a long.

Parameters:
name - The name of the specified property.
Returns:
The value of the specified property.

getPropertyString

public String getPropertyString(String name)
Get the specified property as a string.

Parameters:
name - The name of the property.
Returns:
The value of the property.

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.

getTags

public Collection<String> getTags(Layer layer)
Get a list of all of the tags on a specific layer.

Parameters:
layer - The layer to check.
Returns:
A collection of the layer tags.

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 Object
Returns:
THe hash code.

isConnected

public boolean isConnected(Synapse synapse,
                           int fromNeuron,
                           int toNeuron)
Determine if the specified connection is enabled.

Parameters:
synapse - The synapse.
fromNeuron - The source neuron.
toNeuron - THe target neuron.
Returns:
True, if the connection is enabled, false otherwise.

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:
reset in interface Network

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.

setLogic

public void setLogic(NeuralLogic logic)
Set the type of logic this network should use.

Parameters:
logic - The logic used by the network.

setProperty

public void setProperty(String name,
                        double d)
Set a property as a double.

Parameters:
name - The name of the property.
d - The value of the property.

setProperty

public void setProperty(String name,
                        long l)
Set a property as a long.

Parameters:
name - The name of the property.
l - The value of the property.

setProperty

public void setProperty(String name,
                        String value)
Set a property as a double.

Parameters:
name - The name of the property.
value - The value of the property.

tagLayer

public void tagLayer(String tag,
                     Layer layer)
Tag a layer.

Parameters:
tag - The tag name.
layer - THe layer to tag.

toString

public String toString()

Specified by:
toString in interface Network
Overrides:
toString in class Object

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.


Copyright © 2011. All Rights Reserved.