org.joone.net
Class NeuralNet

java.lang.Object
  extended by org.joone.net.NeuralNet
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, NeuralLayer, NeuralNetListener

public class NeuralNet
extends java.lang.Object
implements NeuralLayer, NeuralNetListener, java.io.Serializable

This object represents a container of a neural network, giving to the developer the possibility to manage a neural network as a whole. Thanks to it, a neural network can be saved and restored using an unique writeObject and readObject command, without be worried about its internal composition. Not only this, because using a NeuralNet object, we can also easily transport a neural network on remote machines and runnit there, writing only few and generalized java code.

See Also:
Serialized Form

Field Summary
static int HIDDEN_LAYER
           
static int INPUT_LAYER
           
protected  java.util.Vector listeners
           
static int OUTPUT_LAYER
           
protected  int patternNumber
          The number of the current pattern being chased through the network
 
Constructor Summary
NeuralNet()
          Creates new NeuralNet
 
Method Summary
 boolean addInputSynapse(InputPatternListener p1)
          Adds a new input synapse to the layer
 void addLayer(Layer layer)
           
 void addLayer(Layer layer, int tier)
           
 void addNeuralNetListener(NeuralNetListener listener)
           
 void addNoise(double p1)
          Adds a noise to the biases of the layer and to all the input synapses connected
 boolean addOutputSynapse(OutputPatternListener p1)
          Adds a new output synapse to the layer
 Layer[] calculateOrderedLayers()
          This method calculates the order of the layers of the network, from the input to the output.
 java.util.TreeSet check()
          Compiles all layers' check messages.
 void cicleTerminated(NeuralNetEvent e)
          To override.
 NeuralNet cloneNet()
          Creates a copy of the contained neural network
 NeuralLayer copyInto(NeuralLayer p1)
          Not implemented.
 void errorChanged(NeuralNetEvent e)
          To override.
protected  void fastContinue()
          This method restore the running of the neural network in single-thread mode, starting from the epoch at which it had been previously stopped.
protected  void fastRun()
          This method runs the neural network in single-thread mode.
protected  void fastRun(int firstEpoch, boolean continuation)
          This method runs the neural network in single-thread mode starting from the epoch passed as parameter.
 Layer findInputLayer()
          Returns the input layer, by searching for it following the rules written in Layer.isInputLayer.
 Layer findOutputLayer()
          Returns the output layer by searching for it following the rules written in Layer.isOutputLayer.
 java.util.Vector getAllInputs()
          Returns the vector of the input listeners
 java.util.Vector getAllOutputs()
          Returns the vector of the input listeners
 Matrix getBias()
          Return the bias matrix
 NeuralNetAttributes getDescriptor()
           
 Layer getInputLayer()
          Returns the input layer of the network.
 java.lang.String[] getKeys()
          Return all the keys of the parameters contained in the net.
 Layer getLayer(java.lang.String layerName)
           
 java.lang.String getLayerName()
          Returns the name of the layer
 java.util.Vector getLayers()
           
 java.util.Vector getListeners()
           
 MacroInterface getMacroPlugin()
          Getter for property macroPlugin.
 Monitor getMonitor()
          Returns the monitor object
static java.lang.Integer getNumericVersion()
          Method to get the numeric version.
protected  int getNumOfstepCounters()
           
 Layer[] getOrderedLayers()
           
 Layer getOutputLayer()
          Returns the output layer of the network.
 java.lang.Object getParam(java.lang.String key)
          Gets a custom parameter from the neural net.
 int getPatternNumber()
          Retrieve the number of the current pattern being pumped through the network
 int getRows()
          Returns the dimension (# of neurons) of the Layer
protected  java.lang.Thread getSingleThread()
           
 ComparingElement getTeacher()
           
static java.lang.String getVersion()
          Method to get the version.
 void go()
          Runs the network in async mode (i.e.
 void go(boolean sync)
          Runs the network.
 void go(boolean singleThreadMode, boolean sync)
          Runs the network.
 boolean isRunning()
          Returns true if the network is running
 boolean isScriptingEnabled()
          Gets if the scripting engine is enabled
 void join()
          Waits for all the termination of all running Threads
 void netStarted(NeuralNetEvent e)
          To override.
 void netStopped(NeuralNetEvent e)
          To override.
 void netStoppedError(NeuralNetEvent e, java.lang.String error)
          Stops the execution threads and resets all the layers in the event of an crtitical network error.
 void randomize(double amplitude)
           
 void removeAllInputs()
          Remove all the input listeners of the net
 void removeAllListeners()
           
 void removeAllOutputs()
          Remove all the output listeners of the net
 void removeInputSynapse(InputPatternListener p1)
          Remove an input Listener
 void removeLayer(Layer layer)
           
 void removeNeuralNetListener(NeuralNetListener listener)
           
 void removeOutputSynapse(OutputPatternListener p1)
          Remove an output listener from the layer
 void reset()
          Resets all the StreamInputLayer of the net.
 void resetInitialState()
          Reset the initial state of any context layers
 void resetInput()
          Resets all the StreamInputLayer of the net
 void restore()
          Continue the execution of the network after a stop() invocation.
 void restore(boolean sync)
          Continue the execution of the network after a stop() invocation.
 void setAllInputs(java.util.Vector p1)
          Sets the vector that contains all the input listeners.
 void setAllOutputs(java.util.Vector p1)
          Sets the vector that contains all the output listeners.
 void setBias(Matrix p1)
          Sets the matrix of biases
 void setDescriptor(NeuralNetAttributes newdescriptor)
           
 void setInputLayer(Layer newLayer)
           
 void setLayerName(java.lang.String p1)
          Sets the name of the layer
 void setLayers(java.util.Vector newlayers)
          Permits to initialize a neural network with a Vector containing layers.
 void setLayersList(java.util.List list)
          Permits to initialize a neural network with an List containing layers.
 void setListeners(java.util.Vector listeners)
          Not implemented.
 void setMacroPlugin(MacroInterface macroPlugin)
          Setter for property macroPlugin.
 void setMonitor(Monitor p1)
          Sets the monitor object
 void setOrderedLayers(Layer[] orderedLayers)
          This method permits to set externally a particular order to traverse the Layers.
 void setOutputLayer(Layer newLayer)
           
 void setParam(java.lang.String key, java.lang.Object obj)
          Sets a custom parameter of the neural net.
 void setRows(int p1)
          Sets the dimension (# of neurons) of the Layer
 void setScriptingEnabled(boolean enabled)
          Enable/disable the scripting engine for the net.
protected  void setSingleThread(java.lang.Thread singleThread)
           
 void setTeacher(ComparingElement ts)
          Sets the Teacher for this NeuralNet object
 void singleStepBackward(Pattern error)
          Use this method to perform a single step backward.
 void singleStepForward(Pattern pattern)
          Use this method to perform a single step forward.
 void start()
          Starts all the Layers' threads, in order to prepare the launch of the neural network in multi-thread mode.
protected  void stepBackward(Pattern error)
           
protected  void stepForward(Pattern pattern)
           
 void stop()
          Terminates the execution of this NeuralNet independently from the threading mode activated.
protected  void stopFastRun()
          This method serves to stop the network when running in single-thread mode.
 void terminate()
          Terminates the execution of all the threads of the neural network.
 void terminate(boolean notify)
          Terminates the execution of all the threads of the neural network.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INPUT_LAYER

public static final int INPUT_LAYER
See Also:
Constant Field Values

HIDDEN_LAYER

public static final int HIDDEN_LAYER
See Also:
Constant Field Values

OUTPUT_LAYER

public static final int OUTPUT_LAYER
See Also:
Constant Field Values

listeners

protected java.util.Vector listeners

patternNumber

protected int patternNumber
The number of the current pattern being chased through the network

Constructor Detail

NeuralNet

public NeuralNet()
Creates new NeuralNet

Method Detail

start

public void start()
Starts all the Layers' threads, in order to prepare the launch of the neural network in multi-thread mode. DO NOT use for single-thread mode.

Specified by:
start in interface NeuralLayer

join

public void join()
Waits for all the termination of all running Threads

See Also:
Thread.join()

stop

public void stop()
Terminates the execution of this NeuralNet independently from the threading mode activated.


terminate

public void terminate(boolean notify)
Terminates the execution of all the threads of the neural network. Used to force a neural network independently from its internal state. Use ONLY in multi-thread mode, and ONLY when the call to the stop() method doesn't give the expected results.

Parameters:
notify - if true, the netStopped event is raised

terminate

public void terminate()
Terminates the execution of all the threads of the neural network.

See Also:
this.terminate(boolean notify)

getNumOfstepCounters

protected int getNumOfstepCounters()

getInputLayer

public Layer getInputLayer()
Returns the input layer of the network. If the method setInputLayer has been never invoked, the input layer is found, set and returned.


findInputLayer

public Layer findInputLayer()
Returns the input layer, by searching for it following the rules written in Layer.isInputLayer. Ignores any previous call made to setInputLayer.


getOutputLayer

public Layer getOutputLayer()
Returns the output layer of the network. If the method setOutputLayer has been never invoked, the output layer is found, set and returned.


findOutputLayer

public Layer findOutputLayer()
Returns the output layer by searching for it following the rules written in Layer.isOutputLayer. Ignores any previous call made to setOutputLayer.


getRows

public int getRows()
Description copied from interface: NeuralLayer
Returns the dimension (# of neurons) of the Layer

Specified by:
getRows in interface NeuralLayer
Returns:
int

setRows

public void setRows(int p1)
Description copied from interface: NeuralLayer
Sets the dimension (# of neurons) of the Layer

Specified by:
setRows in interface NeuralLayer
Parameters:
p1 - The number of the neurons contained in the Layer

addNoise

public void addNoise(double p1)
Description copied from interface: NeuralLayer
Adds a noise to the biases of the layer and to all the input synapses connected

Specified by:
addNoise in interface NeuralLayer
Parameters:
p1 - the noise's amplitude in terms of distance from zero; e.g.: a value equal 0.3 means a noise from -0.3 to 0.3

randomize

public void randomize(double amplitude)

getBias

public Matrix getBias()
Description copied from interface: NeuralLayer
Return the bias matrix

Specified by:
getBias in interface NeuralLayer
Returns:
neural.engine.Matrix

getAllOutputs

public java.util.Vector getAllOutputs()
Description copied from interface: NeuralLayer
Returns the vector of the input listeners

Specified by:
getAllOutputs in interface NeuralLayer
Returns:
java.util.Vector

getLayerName

public java.lang.String getLayerName()
Description copied from interface: NeuralLayer
Returns the name of the layer

Specified by:
getLayerName in interface NeuralLayer
Returns:
java.lang.String

removeOutputSynapse

public void removeOutputSynapse(OutputPatternListener p1)
Description copied from interface: NeuralLayer
Remove an output listener from the layer

Specified by:
removeOutputSynapse in interface NeuralLayer
Parameters:
p1 - the output listener to remove

setAllInputs

public void setAllInputs(java.util.Vector p1)
Description copied from interface: NeuralLayer
Sets the vector that contains all the input listeners. Can be useful to set the input synapses taken from another Layer

Specified by:
setAllInputs in interface NeuralLayer
Parameters:
p1 - The vector containing the list of input synapses

removeAllOutputs

public void removeAllOutputs()
Description copied from interface: NeuralLayer
Remove all the output listeners of the net

Specified by:
removeAllOutputs in interface NeuralLayer

getAllInputs

public java.util.Vector getAllInputs()
Description copied from interface: NeuralLayer
Returns the vector of the input listeners

Specified by:
getAllInputs in interface NeuralLayer
Returns:
java.util.Vector

addOutputSynapse

public boolean addOutputSynapse(OutputPatternListener p1)
Description copied from interface: NeuralLayer
Adds a new output synapse to the layer

Specified by:
addOutputSynapse in interface NeuralLayer
Parameters:
p1 - The new output synapse
Returns:
true if the output synapse has been attached sucessfully

setBias

public void setBias(Matrix p1)
Description copied from interface: NeuralLayer
Sets the matrix of biases

Specified by:
setBias in interface NeuralLayer
Parameters:
p1 - The Matrix object containing the biases

removeInputSynapse

public void removeInputSynapse(InputPatternListener p1)
Description copied from interface: NeuralLayer
Remove an input Listener

Specified by:
removeInputSynapse in interface NeuralLayer
Parameters:
p1 - the input listener to remove

setLayerName

public void setLayerName(java.lang.String p1)
Description copied from interface: NeuralLayer
Sets the name of the layer

Specified by:
setLayerName in interface NeuralLayer
Parameters:
p1 - The name

addInputSynapse

public boolean addInputSynapse(InputPatternListener p1)
Description copied from interface: NeuralLayer
Adds a new input synapse to the layer

Specified by:
addInputSynapse in interface NeuralLayer
Parameters:
p1 - The new input synapse
Returns:
true if the input synapse has been attached sucessfully

setAllOutputs

public void setAllOutputs(java.util.Vector p1)
Description copied from interface: NeuralLayer
Sets the vector that contains all the output listeners. Can be useful to set the output synapses taken from another Layer

Specified by:
setAllOutputs in interface NeuralLayer
Parameters:
p1 - The vector containing the list of output synapses

setMonitor

public void setMonitor(Monitor p1)
Description copied from interface: NeuralLayer
Sets the monitor object

Specified by:
setMonitor in interface NeuralLayer
Parameters:
p1 - The Monitor to be set

getMonitor

public Monitor getMonitor()
Description copied from interface: NeuralLayer
Returns the monitor object

Specified by:
getMonitor in interface NeuralLayer
Returns:
java.engine.Monitor

removeAllInputs

public void removeAllInputs()
Description copied from interface: NeuralLayer
Remove all the input listeners of the net

Specified by:
removeAllInputs in interface NeuralLayer

copyInto

public NeuralLayer copyInto(NeuralLayer p1)
Not implemented.

Specified by:
copyInto in interface NeuralLayer
Parameters:
p1 - neural.engine.Layer
Returns:
The new layer

addLayer

public void addLayer(Layer layer)

addLayer

public void addLayer(Layer layer,
                     int tier)

removeLayer

public void removeLayer(Layer layer)

resetInput

public void resetInput()
Resets all the StreamInputLayer of the net


reset

public void reset()
Resets all the StreamInputLayer of the net. MG Message reset in stead of reset input, less dramatic and not opening a zillion files.


addNeuralNetListener

public void addNeuralNetListener(NeuralNetListener listener)

getListeners

public java.util.Vector getListeners()

removeNeuralNetListener

public void removeNeuralNetListener(NeuralNetListener listener)

getVersion

public static java.lang.String getVersion()
Method to get the version.

Returns:
A string containing the version of joone's engine in the format xx.yy.zz

getNumericVersion

public static java.lang.Integer getNumericVersion()
Method to get the numeric version.

Returns:
an integer containing the joone's engine version

getLayer

public Layer getLayer(java.lang.String layerName)

getLayers

public java.util.Vector getLayers()

setLayers

public void setLayers(java.util.Vector newlayers)
Permits to initialize a neural network with a Vector containing layers.


setLayersList

public void setLayersList(java.util.List list)
Permits to initialize a neural network with an List containing layers. Added for Spring.


setTeacher

public void setTeacher(ComparingElement ts)
Sets the Teacher for this NeuralNet object

Parameters:
TeachingSynapse - - the new teacher. It can be null to make unsupervised this neural network

getTeacher

public ComparingElement getTeacher()

setListeners

public void setListeners(java.util.Vector listeners)
Not implemented.

Parameters:
listeners -

setInputLayer

public void setInputLayer(Layer newLayer)

setOutputLayer

public void setOutputLayer(Layer newLayer)

getDescriptor

public NeuralNetAttributes getDescriptor()

setDescriptor

public void setDescriptor(NeuralNetAttributes newdescriptor)

isRunning

public boolean isRunning()
Returns true if the network is running

Specified by:
isRunning in interface NeuralLayer
Returns:
boolean

cloneNet

public NeuralNet cloneNet()
Creates a copy of the contained neural network

Returns:
the cloned NeuralNet

removeAllListeners

public void removeAllListeners()

setScriptingEnabled

public void setScriptingEnabled(boolean enabled)
Enable/disable the scripting engine for the net. If disabled, all the event-driven macros will be not run

Parameters:
enabled - true to enable the scripting, otherwise false

isScriptingEnabled

public boolean isScriptingEnabled()
Gets if the scripting engine is enabled

Returns:
true if enabled

getMacroPlugin

public MacroInterface getMacroPlugin()
Getter for property macroPlugin.

Returns:
Value of property macroPlugin.

setMacroPlugin

public void setMacroPlugin(MacroInterface macroPlugin)
Setter for property macroPlugin.

Parameters:
macroPlugin - New value of property macroPlugin.

getParam

public java.lang.Object getParam(java.lang.String key)
Gets a custom parameter from the neural net. The user is free to use the custom parameters as s/he wants. They are useful to transport a whatever value along with the net.

Parameters:
key - The searched key
Returns:
The value of the parameter if found, otherwise null

setParam

public void setParam(java.lang.String key,
                     java.lang.Object obj)
Sets a custom parameter of the neural net. The user is free to use the custom parameters as s/he wants. They are useful to transport a whatever value along with the net.

Parameters:
key - The key of the param
obj - The value of the param

getKeys

public java.lang.String[] getKeys()
Return all the keys of the parameters contained in the net.

Returns:
An array of Strings containing all the keys if found, otherwise null

check

public java.util.TreeSet check()
Compiles all layers' check messages.

Specified by:
check in interface NeuralLayer
Returns:
validation errors.
See Also:
NeuralLayer

netStarted

public void netStarted(NeuralNetEvent e)
To override.

Specified by:
netStarted in interface NeuralNetListener
Parameters:
e -

cicleTerminated

public void cicleTerminated(NeuralNetEvent e)
To override.

Specified by:
cicleTerminated in interface NeuralNetListener
Parameters:
e -

netStopped

public void netStopped(NeuralNetEvent e)
To override.

Specified by:
netStopped in interface NeuralNetListener
Parameters:
e -

errorChanged

public void errorChanged(NeuralNetEvent e)
To override.

Specified by:
errorChanged in interface NeuralNetListener
Parameters:
e -

netStoppedError

public void netStoppedError(NeuralNetEvent e,
                            java.lang.String error)
Stops the execution threads and resets all the layers in the event of an crtitical network error.

Specified by:
netStoppedError in interface NeuralNetListener
Parameters:
error - The error message.
e - The event source of this event.

setOrderedLayers

public void setOrderedLayers(Layer[] orderedLayers)
This method permits to set externally a particular order to traverse the Layers. If not used, the order will be calculated automatically. Use this method in cases where the automatic ordering doesn't work (e.g. in case of complex recurrent connections) NOTE: if you set this property, you're responsible to update the array whenever a layer is added/removed.

Parameters:
orderedLayers - an array containing the ordered layers

getOrderedLayers

public Layer[] getOrderedLayers()

calculateOrderedLayers

public Layer[] calculateOrderedLayers()
This method calculates the order of the layers of the network, from the input to the output. If the setOrderedLayers method has been invoked before, that array will be returned, otherwise the order will be calculated automatically.

Returns:
An array containing the ordered Layers, from the input to the output (i.e. layers[0]=input layer, layers[n-1]=output layer.

go

public void go(boolean singleThreadMode,
               boolean sync)
Runs the network.

Parameters:
singleThreadMode - If true, runs the network in single thread mode
sync - If true, runs the network in a separated thread and returns immediately.

go

public void go(boolean sync)
Runs the network. The running mode is determined by the value of the singleThreadMode property.

Parameters:
sync - If false, runs the network in a separated thread and returns immediately.

go

public void go()
Runs the network in async mode (i.e. equivalent to go(false) ). The running mode is determined by the value of the singleThreadMode property.


restore

public void restore(boolean sync)
Continue the execution of the network after a stop() invocation. The network resumes from the last reached epoch.

Parameters:
sync - If false, runs the network in a separated thread and returns immediately.

restore

public void restore()
Continue the execution of the network after a stop() invocation. The network resumes from the last reached epoch in asynch mode. Equivalent to restore(false)


fastRun

protected void fastRun()
This method runs the neural network in single-thread mode.


fastContinue

protected void fastContinue()
This method restore the running of the neural network in single-thread mode, starting from the epoch at which it had been previously stopped.


fastRun

protected void fastRun(int firstEpoch,
                       boolean continuation)
This method runs the neural network in single-thread mode starting from the epoch passed as parameter. NOTE: the network will count-down from firstEpoch to 0.

Parameters:
firstEpoch - the epoch from which the network will start.
continuation - if set layers of NN will not be initialized before run

getPatternNumber

public int getPatternNumber()
Retrieve the number of the current pattern being pumped through the network

Returns:
0 if nothing is done at present, positive for pattern or -1 for stop pattern

singleStepForward

public void singleStepForward(Pattern pattern)
Use this method to perform a single step forward. The network is interrogated using the next available input pattern (only one).

Parameters:
pattern - The input pattern to use. If null, the input pattern is read from the input synapse connected to the input layer.

singleStepBackward

public void singleStepBackward(Pattern error)
Use this method to perform a single step backward. The pattern passed as parameter, that is backpropagated, must contain the error in terms of differences from the desired pattern.

Parameters:
pattern - The error pattern to backpropagate. If null, the pattern is read from the teacher connected to the output layer.

stepForward

protected void stepForward(Pattern pattern)

stepBackward

protected void stepBackward(Pattern error)

stopFastRun

protected void stopFastRun()
This method serves to stop the network when running in single-thread mode. It DOES NOT affect the multi-thread running (i.e. a network launched with Monitor.Go() method.


getSingleThread

protected java.lang.Thread getSingleThread()

setSingleThread

protected void setSingleThread(java.lang.Thread singleThread)

resetInitialState

public void resetInitialState()
Reset the initial state of any context layers



Submit Feedback to pmarrone@users.sourceforge.net