org.joone.engine
Class Layer

java.lang.Object
  extended by org.joone.engine.Layer
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable, Learnable, LearnableLayer, NeuralLayer, Inspectable
Direct Known Subclasses:
MemoryLayer, NestedNeuralLayer, NetworkLayer, RbfLayer, SimpleLayer

public abstract class Layer
extends java.lang.Object
implements NeuralLayer, java.lang.Runnable, java.io.Serializable, Inspectable, LearnableLayer

The Layer object is the basic element forming the neural net. Primarily it consists of a number of neurons that apply a transfer function to the sum of a number of input patterns and convey the result to the output pattern. The input patterns are received from connected input listeners and the transformed results are passed to connected output listeners. The component also handles learning by accepting patterns of error gradients from output listeners, applying a reverse (inverse) transfer function and passing the result to the input listeners. Layers execute their own Threads to perform the perform the pattern conveyance, so that a network of Layers can operate in a multi-threaded manner. The execution and termination of the Thread is controlled by a Monitor object.

See Also:
Serialized Form

Field Summary
protected  Matrix bias
          Holds the bias of neurons of the layer
protected  double[] gradientInps
          Set of input error gradient values passed to this layer from connected OutputListenrs during the learning phase.
protected  double[] gradientOuts
          Set of output error gradient values passed from this layer to connected InputListenrs during the learning phase.
protected  double[] inps
          Set of input values passed to this layer from connected InputListeners during the recall phase.
protected  java.util.Vector inputPatternListeners
          Contains the list of input connected listeners (InputPatternListener)
protected  boolean learnable
          Contains true if for the Layer must be used a Learner instead of a built-in learning algorithm.
protected  boolean learning
          The Net's phase: false == recall; true == learning
protected  int m_batch
          Not used but maintained for backward serialization compatability.
protected  Monitor monitor
          The monitor of the layer.
protected  Learner myLearner
          The Learner for this layer.
protected  java.util.Vector outputPatternListeners
          Contains the list of output connected listeners (OutputPatternListener)
protected  double[] outs
          Set of output values passed from this layer to connected OutputListeners durng the recall phase.
protected  boolean running
          Whether the layer is running
protected  int step
          The step number of the network run.
static int STOP_FLAG
          Stop flag.
 
Constructor Summary
Layer()
          The empty constructor
Layer(java.lang.String ElemName)
          Creates a named layer
 
Method Summary
 boolean addInputSynapse(InputPatternListener newListener)
          Adds a new input synapse to the layer
 void addNoise(double amplitude)
          Adds a noise componentto the biases of the layer and to all the input connected synapses.
 boolean addOutputSynapse(OutputPatternListener newListener)
          Adds a new output synapse to the layer
protected  void adjustSizeToFwdPattern(double[] aPattern)
          Adjusts the size of a layer if the size of the forward pattern differs.
protected  void adjustSizeToRevPattern(double[] aPattern)
          Adjusts the size of a layer if the size of the reverse pattern differs.
protected abstract  void backward(double[] pattern)
          Reverse transfer function of the component.
 java.util.TreeSet check()
          Get check messages from listeners.
protected  boolean checkInputEnabled()
          Checks if at least one input synapse is enabled
protected  boolean checkInputs(java.util.Vector inputListeners)
          Determine whether ther are any stream input synapses attached.
protected  boolean checkOutputs(java.util.Vector outputListeners)
          Determine whether ther are any stream output or teach synapses attached.
 NeuralLayer copyInto(NeuralLayer newLayer)
          Copies one layer into another, to obtain a type-transformation from one kind of Layer to another.
 void finalize()
          Method to help remove disused references quickly when the layer goes out of scope.
protected  void fireFwdGet()
          Calls all the fwdGet methods on the input synapses to get the input patterns
protected  void fireFwdPut(Pattern pattern)
          Calls all the fwdPut methods on the output synapses to pass them the calculated patterns
protected  void fireRevGet()
          Calls all the revGet methods on the output synapses to get the error gradients
protected  void fireRevPut(Pattern pattern)
          Calls all the revPut methods on the input synapses to get the input patterns and pass them the resulting calculated gradients
protected abstract  void forward(double[] pattern)
          Transfer function to recall a result on a trained net
 void fwdRun(Pattern pattIn)
          Implementation code for the single-thread version of Joone.
 java.util.Vector getAllInputs()
          Returns the vector of the input listeners
 java.util.Vector getAllOutputs()
          Returns the vector of the output listeners
 Matrix getBias()
          Return the bias matrix
abstract  double getDefaultState()
          Return the default state of a node in this layer, such as 0 for a tanh or 0.5 for a sigmoid layer
abstract  double getDerivative(int i)
          Return the derivative with respect to the function value at a given index.
 int getDimension()
          Returns the number of neurons contained in the layer
 double[] getLastGradientInps()
          Return the last input gradient
 double[] getLastGradientOuts()
          Return the last output gradient
 double[] getLastInputs()
          Return the last inputs
 double[] getLastOutputs()
          Gets the values lastly outputed by the neurons of this layer.
 java.lang.String getLayerName()
          Returns the name of the layer
 Learner getLearner()
          Returns the appropriate Learner object for this class depending on the Monitor.learningMode property value
abstract  double getMaximumState()
          Return maximum value of a node in this layer
abstract  double getMinimumState()
          Return minimum value of a node in this layer
 Monitor getMonitor()
          Returns the monitor object
 int getRows()
          Returns the dimension (# of neurons) of the Layer
protected  java.lang.Object getThreadMonitor()
          Getter for property myThreadMonitor.
 boolean hasStepCounter()
          Determine whether this layer has an input synapse attached that is a step counter.
 void init()
           
 void initLearner()
          Initialize the Learner object of this layer
 java.lang.String InspectableTitle()
          Get the title for the inspectable interface
 java.util.Collection Inspections()
          Method to get a collection of bias inspections for this layer
 boolean isInputLayer()
          Determine whether this is an input layer.
 boolean isOutputLayer()
          Determine whether this is an output layer.
 boolean isRunning()
          Determine whether the execution thread is running
 void join()
          Waits for the current layer's thread to stop
 void randomize(double amplitude)
          Initialize the weights of the biases and of all the connected synapses WARNING: amplitude parameter is ignored when elements are using WeightInitializer other than RandomWeightInitializer
 void randomizeBias(double amplitude)
          Initialize the weights of the biases WARNING: amplitude parameter is ignored when biases are using WeightInitializer other than RandomWeightInitializer
 void randomizeWeights(double amplitude)
          Initialize the weights of all the connected synapses WARNING: amplitude parameter is ignored when elements are using WeightInitializer other than RandomWeightInitializer
 void removeAllInputs()
          Remove all the input listeners of the layer
 void removeAllOutputs()
          Remove all the output listeners of the layer
 void removeInputSynapse(InputPatternListener newListener)
          Remove an input Listener
protected  void removeListener(NeuralNetListener listener)
           
 void removeOutputSynapse(OutputPatternListener newListener)
          Remove an output listener from the layer
protected  void resetInputListeners()
          Reset all the input listeners
 void revRun(Pattern pattIn)
          This method serves to a single backward step when the Layer is called from an external thread
 void run()
          The core running engine of the layer.
 void setAllInputs(java.util.Vector newInputPatternListeners)
          Sets the Vector that contains all the input listeners.
 void setAllOutputs(java.util.Vector newOutputPatternListeners)
          Sets the Vector that contains all the output listeners.
 void setBias(Matrix newBias)
          Sets the matrix of biases
protected  void setConnDimensions()
          Sets the input and output synapses' dimensions
protected abstract  void setDimensions()
          Sets the dimension of the layer.
protected  void setInputDimension(InputPatternListener syn)
          Sets the dimension of the listener passed as parameter.
 void setInputSynapses(java.util.ArrayList newInputPatternListeners)
          Sets the Vector that contains all the input listeners.
 void setLastInputs(double[] inps)
          Set the last inputs
 void setLastOutputs(double[] outs)
          Set the last outputs
 void setLayerName(java.lang.String newLayerName)
          Sets the name of the layer
 void setMonitor(Monitor mon)
          Sets the monitor object
protected  void setOutputDimension(OutputPatternListener syn)
          Sets the dimension of the listener passed as parameter.
 void setOutputSynapses(java.util.ArrayList newOutputPatternListeners)
          Sets the Vector that contains all the output listeners.
 void setRows(int newRows)
          Sets the dimension (# of neurons) of the Layer
 void start()
          Starts the Layer
 void stop()
          Stops the Layer
protected  void sumBackInput(double[] pattern)
          Calculates the net input of the error gradents during the learning phase
protected  void sumInput(double[] pattern)
          Calculates the net input of the values in the recall phase
 java.lang.String toString()
          Produce a String representation of this layer
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STOP_FLAG

public static final int STOP_FLAG
Stop flag. If the step has this value, the execution thread terminates.

See Also:
Constant Field Values

bias

protected Matrix bias
Holds the bias of neurons of the layer


monitor

protected Monitor monitor
The monitor of the layer. Contains all parameters needed to the learning phase


m_batch

protected int m_batch
Not used but maintained for backward serialization compatability.


learning

protected boolean learning
The Net's phase: false == recall; true == learning


learnable

protected boolean learnable
Contains true if for the Layer must be used a Learner instead of a built-in learning algorithm. Set it in the constructor of any inherited class. Used by the getLearner method.

See Also:
getLearner

inputPatternListeners

protected java.util.Vector inputPatternListeners
Contains the list of input connected listeners (InputPatternListener)


outputPatternListeners

protected java.util.Vector outputPatternListeners
Contains the list of output connected listeners (OutputPatternListener)


outs

protected transient double[] outs
Set of output values passed from this layer to connected OutputListeners durng the recall phase.


inps

protected transient double[] inps
Set of input values passed to this layer from connected InputListeners during the recall phase.


gradientInps

protected transient double[] gradientInps
Set of input error gradient values passed to this layer from connected OutputListenrs during the learning phase.


gradientOuts

protected transient double[] gradientOuts
Set of output error gradient values passed from this layer to connected InputListenrs during the learning phase.


step

protected transient int step
The step number of the network run.


running

protected transient volatile boolean running
Whether the layer is running


myLearner

protected transient Learner myLearner
The Learner for this layer.

Constructor Detail

Layer

public Layer()
The empty constructor


Layer

public Layer(java.lang.String ElemName)
Creates a named layer

Parameters:
ElemName - The name of the layer
Method Detail

addNoise

public void addNoise(double amplitude)
Adds a noise componentto the biases of the layer and to all the input connected synapses.

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

getDefaultState

public abstract double getDefaultState()
Return the default state of a node in this layer, such as 0 for a tanh or 0.5 for a sigmoid layer


getMinimumState

public abstract double getMinimumState()
Return minimum value of a node in this layer


getMaximumState

public abstract double getMaximumState()
Return maximum value of a node in this layer


randomize

public void randomize(double amplitude)
Initialize the weights of the biases and of all the connected synapses WARNING: amplitude parameter is ignored when elements are using WeightInitializer other than RandomWeightInitializer

Parameters:
amplitude - the amplitude of uniformly generated random weights

randomizeWeights

public void randomizeWeights(double amplitude)
Initialize the weights of all the connected synapses WARNING: amplitude parameter is ignored when elements are using WeightInitializer other than RandomWeightInitializer

Parameters:
amplitude - the amplitude of uniformly generated random weights

randomizeBias

public void randomizeBias(double amplitude)
Initialize the weights of the biases WARNING: amplitude parameter is ignored when biases are using WeightInitializer other than RandomWeightInitializer

Parameters:
amplitude - the amplitude of uniformly generated random weights

backward

protected abstract void backward(double[] pattern)
                          throws JooneRuntimeException
Reverse transfer function of the component.

Parameters:
pattern - input pattern on which to apply the transfer function
Throws:
JooneRuntimeException

copyInto

public NeuralLayer copyInto(NeuralLayer newLayer)
Copies one layer into another, to obtain a type-transformation from one kind of Layer to another. The old Layer is disconnected from the net, and the new Layer takes its place.

Specified by:
copyInto in interface NeuralLayer
Parameters:
newLayer - the new layer with which to replace this one
Returns:
The new layer

fireFwdGet

protected void fireFwdGet()
Calls all the fwdGet methods on the input synapses to get the input patterns


fireFwdPut

protected void fireFwdPut(Pattern pattern)
Calls all the fwdPut methods on the output synapses to pass them the calculated patterns

Parameters:
pattern - the Pattern to pass to the output synapses

fireRevGet

protected void fireRevGet()
Calls all the revGet methods on the output synapses to get the error gradients


fireRevPut

protected void fireRevPut(Pattern pattern)
Calls all the revPut methods on the input synapses to get the input patterns and pass them the resulting calculated gradients

Parameters:
pattern - the Pattern to pass to the input listeners

adjustSizeToFwdPattern

protected void adjustSizeToFwdPattern(double[] aPattern)
Adjusts the size of a layer if the size of the forward pattern differs.

Parameters:
aPattern - the pattern holding a different size than the layer (dimension of neurons is not in accordance with the dimension of the pattern that is being forwarded).

adjustSizeToRevPattern

protected void adjustSizeToRevPattern(double[] aPattern)
Adjusts the size of a layer if the size of the reverse pattern differs.

Parameters:
aPattern - the pattern holding a different size than the layer (dimension of neurons is not in accordance with the dimension of the pattern that is being reversed).

forward

protected abstract void forward(double[] pattern)
                         throws JooneRuntimeException
Transfer function to recall a result on a trained net

Parameters:
pattern - input pattern to which to apply the rtransfer function
Throws:
JooneRuntimeException

getAllInputs

public java.util.Vector getAllInputs()
Returns the vector of the input listeners

Specified by:
getAllInputs in interface NeuralLayer
Returns:
the connected input pattern listeners

getAllOutputs

public java.util.Vector getAllOutputs()
Returns the vector of the output listeners

Specified by:
getAllOutputs in interface NeuralLayer
Returns:
the connected output pattern listeners

getBias

public Matrix getBias()
Return the bias matrix

Specified by:
getBias in interface NeuralLayer
Returns:
the layer biases

getDimension

public int getDimension()
Returns the number of neurons contained in the layer

Returns:
the number of neurons in the layer.

getLayerName

public java.lang.String getLayerName()
Returns the name of the layer

Specified by:
getLayerName in interface NeuralLayer
Returns:
the name of the layer

getMonitor

public Monitor getMonitor()
Returns the monitor object

Specified by:
getMonitor in interface Learnable
Specified by:
getMonitor in interface NeuralLayer
Returns:
the layer's Monitor object

getRows

public int getRows()
Returns the dimension (# of neurons) of the Layer

Specified by:
getRows in interface NeuralLayer
Returns:
the number of neurons in the layer

removeAllInputs

public void removeAllInputs()
Remove all the input listeners of the layer

Specified by:
removeAllInputs in interface NeuralLayer

removeAllOutputs

public void removeAllOutputs()
Remove all the output listeners of the layer

Specified by:
removeAllOutputs in interface NeuralLayer

removeInputSynapse

public void removeInputSynapse(InputPatternListener newListener)
Remove an input Listener

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

removeOutputSynapse

public void removeOutputSynapse(OutputPatternListener newListener)
Remove an output listener from the layer

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

removeListener

protected void removeListener(NeuralNetListener listener)

getLastOutputs

public double[] getLastOutputs()
Gets the values lastly outputed by the neurons of this layer.

Returns:
the values lastly outputed.

getLastInputs

public double[] getLastInputs()
Return the last inputs


setLastInputs

public void setLastInputs(double[] inps)
Set the last inputs


setLastOutputs

public void setLastOutputs(double[] outs)
Set the last outputs


getLastGradientInps

public double[] getLastGradientInps()
Return the last input gradient


getLastGradientOuts

public double[] getLastGradientOuts()
Return the last output gradient


run

public void run()
         throws JooneRuntimeException
The core running engine of the layer. Called from the method start()

Specified by:
run in interface java.lang.Runnable
Throws:
JooneRuntimeException

setAllInputs

public void setAllInputs(java.util.Vector newInputPatternListeners)
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:
newInputPatternListeners - The vector containing the list of input synapses

setInputSynapses

public void setInputSynapses(java.util.ArrayList newInputPatternListeners)
Sets the Vector that contains all the input listeners. It accepts an ArrayList as parameter. Added for Spring Can be useful to set the input synapses taken from another Layer

Parameters:
newInputPatternListeners - The vector containing the list of input synapses

setAllOutputs

public void setAllOutputs(java.util.Vector newOutputPatternListeners)
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:
newOutputPatternListeners - The vector containing the list of output synapses

setOutputSynapses

public void setOutputSynapses(java.util.ArrayList newOutputPatternListeners)
Sets the Vector that contains all the output listeners. It accepts an ArrayList as parameter. Added for Spring Can be useful to set the output synapses taken from another Layer

Parameters:
newOutputPatternListeners - The vector containing the list of output synapses

setBias

public void setBias(Matrix newBias)
Sets the matrix of biases

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

setDimensions

protected abstract void setDimensions()
Sets the dimension of the layer. Override to define how the internal buffers must be sized.


setInputDimension

protected void setInputDimension(InputPatternListener syn)
Sets the dimension of the listener passed as parameter. Called after a new input listener is added.

Parameters:
syn - the listener to be affected

addInputSynapse

public boolean addInputSynapse(InputPatternListener newListener)
Adds a new input synapse to the layer

Specified by:
addInputSynapse in interface NeuralLayer
Parameters:
newListener - The new input synapse to add
Returns:
whether the listener was added

setLayerName

public void setLayerName(java.lang.String newLayerName)
Sets the name of the layer

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

setMonitor

public void setMonitor(Monitor mon)
Sets the monitor object

Specified by:
setMonitor in interface NeuralLayer
Parameters:
mon - The Monitor

setOutputDimension

protected void setOutputDimension(OutputPatternListener syn)
Sets the dimension of the listener passed as parameter. Called after a new output listener is added.

Parameters:
syn - the OutputPatternListener to affect

addOutputSynapse

public boolean addOutputSynapse(OutputPatternListener newListener)
Adds a new output synapse to the layer

Specified by:
addOutputSynapse in interface NeuralLayer
Parameters:
newListener - The new output synapse
Returns:
whether the listener was added

setRows

public void setRows(int newRows)
Sets the dimension (# of neurons) of the Layer

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

start

public void start()
Starts the Layer

Specified by:
start in interface NeuralLayer

init

public void init()

checkInputEnabled

protected boolean checkInputEnabled()
Checks if at least one input synapse is enabled

Returns:
false if all the input synapses are disabled

stop

public void stop()
Stops the Layer


resetInputListeners

protected void resetInputListeners()
Reset all the input listeners


sumBackInput

protected void sumBackInput(double[] pattern)
Calculates the net input of the error gradents during the learning phase

Parameters:
pattern - array of input values

sumInput

protected void sumInput(double[] pattern)
Calculates the net input of the values in the recall phase

Parameters:
pattern - array of input values

setConnDimensions

protected void setConnDimensions()
Sets the input and output synapses' dimensions


isRunning

public boolean isRunning()
Determine whether the execution thread is running

Specified by:
isRunning in interface NeuralLayer
Returns:
whether it is running

check

public java.util.TreeSet check()
Get check messages from listeners. Subclasses should call this method from thier own check method.

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

toString

public java.lang.String toString()
Produce a String representation of this layer

Overrides:
toString in class java.lang.Object
Returns:
string representation of the layer
See Also:
Object.toString()

finalize

public void finalize()
              throws java.lang.Throwable
Method to help remove disused references quickly when the layer goes out of scope.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable
See Also:
Object.finalize()

Inspections

public java.util.Collection Inspections()
Method to get a collection of bias inspections for this layer

Specified by:
Inspections in interface Inspectable
Returns:
See Also:
org.joone.Inspection

InspectableTitle

public java.lang.String InspectableTitle()
Get the title for the inspectable interface

Specified by:
InspectableTitle in interface Inspectable
Returns:
See Also:
org.joone.InspectionFrame

hasStepCounter

public boolean hasStepCounter()
Determine whether this layer has an input synapse attached that is a step counter.

Returns:
whether it is a step counter.

isInputLayer

public boolean isInputLayer()
Determine whether this is an input layer.

Returns:
whether this is an input layer

checkInputs

protected boolean checkInputs(java.util.Vector inputListeners)
Determine whether ther are any stream input synapses attached. MG: Also returns if nothing is attached or if only a feedback synapse is attached.

Parameters:
inputListeners - Vector to check.
Returns:
whether there are any attached StreamInputSynapses

isOutputLayer

public boolean isOutputLayer()
Determine whether this is an output layer.

Returns:
whether this is an output layer

checkOutputs

protected boolean checkOutputs(java.util.Vector outputListeners)
Determine whether ther are any stream output or teach synapses attached. Also checks the attached listeners of OutputSwitchSynapses. Also checks for loopback condition. All connected synapses must be of this type.

Parameters:
outputListeners - Vector to check.
Returns:
whether there are any attached StreamOutputSynapses

getLearner

public Learner getLearner()
Returns the appropriate Learner object for this class depending on the Monitor.learningMode property value

Specified by:
getLearner in interface Learnable
Returns:
the Learner object if applicable, otherwise null
See Also:
Learnable.getLearner()

initLearner

public void initLearner()
Initialize the Learner object of this layer

Specified by:
initLearner in interface Learnable
See Also:
Learnable.initLearner()

getThreadMonitor

protected java.lang.Object getThreadMonitor()
Getter for property myThreadMonitor.

Returns:
Value of property myThreadMonitor.

join

public void join()
Waits for the current layer's thread to stop


fwdRun

public void fwdRun(Pattern pattIn)
Implementation code for the single-thread version of Joone. This method serves to a single forward step when the Layer is called from an external thread.


revRun

public void revRun(Pattern pattIn)
This method serves to a single backward step when the Layer is called from an external thread


getDerivative

public abstract double getDerivative(int i)
Return the derivative with respect to the function value at a given index. MG: Added for RTRL purposes



Submit Feedback to pmarrone@users.sourceforge.net