cc.glsn.v15.neuralnet
Class Brain

java.lang.Object
  extended by cc.glsn.v15.neuralnet.Brain
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
RecurrentBrain

public class Brain
extends Object
implements Serializable

This class acts as a container for an artificial feed-forward neural network.

Supportes any number of inputs, any number of hidden layers of any size and any number of outputs. The inputs and outputs are identified with a string identifier so the user doesn't have to worry about indexing. Also, inputs and outputs may be added at any time, so not all inputs and outputs need to be known initialy.

Each input connects to each neuron on the first hidden layer is using the default input mapping of 'Full'. Each hidden layer is fully connected to the next hidden layer. Each neuron on the last hidden layer connects to the output neuron.

It is serializable, as are all the contained classes. Thus its state can be saved to files, databases, etc.

See Also:
Brain(NetFunction, LinkedList, String), Brain(NetFunction, LinkedList), Serialized Form

Constructor Summary
Brain(NetFunction functg, LinkedList<Integer> layerSizes)
          Uses the default Input mapping style of 'Full'.
Brain(NetFunction functg, LinkedList<Integer> layerSizes, String inputMappingStyle)
           
 
Method Summary
 void addInput(String name)
          Add a new input of the given name.
 void addInputGroup(Collection<String> names)
          Add a group if inputs at once.
 void addOutput(String name)
          Add an output with the given name.
 void backPropogate(double alpha, Map<String,Double> targets)
          Do back propogation on a group of output targets at once.
 void backPropogate(String name, double alpha, double target)
          Do back propogation learning with the given alpha and target.
 Map<String,Double> getAllOutputs()
          Get the output values for all output neurons in a map
 Input getInputNode(String name)
          So that inputs can be set directly to avoid the Map lookup that happens when setInput is used.
 double getOutput(String name)
          Get the output from the single output neuron.
 void incSerial()
          Manually increment the serial number.
 void removeOutput(String name)
          Remove the output with the given name, if it exists
 void setAllInput(double val)
          Set all inputs to the given value.
 void setHiddenLayers(LinkedList<Integer> layerSizes)
          Resets the hidden layer sizes
 void setInput(String name, double val)
          Sets the value of the given input to 'val'
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Brain

public Brain(NetFunction functg,
             LinkedList<Integer> layerSizes)
Uses the default Input mapping style of 'Full'.

Parameters:
functg - The function used inside neurons
layerSizes - a list of hidden layer sizes
See Also:
Brain(NetFunction, LinkedList, String)

Brain

public Brain(NetFunction functg,
             LinkedList<Integer> layerSizes,
             String inputMappingStyle)
Parameters:
functg - The function used inside neurons
layerSizes - a list of hidden layer sizes
inputMappingStyle - Controls how the input layer is connected to the first hidden layer.

Current options are:
  • 'Full' for each input node connected to each layer 1 neuron
  • 'EvenSplit' each input node is connected to one neuron and each neuron has roughly equan number of inputs. This is for handling a very large number of inputs.
  • Method Detail

    setHiddenLayers

    public void setHiddenLayers(LinkedList<Integer> layerSizes)
    Resets the hidden layer sizes

    Parameters:
    layerSizes - - new sizes. The first element of the linked list will be the layer closest to the input. The last element will be the layer closest to the output layer.

    Example: a list of [32,24,16] will make a network with 32 neurons on the first hidden layer, 24 on the next and 16 on the next.

    addInput

    public void addInput(String name)
    Add a new input of the given name. Maps input to first layer. The remapping function is expensive, so if you are doing more than a handful, use addInputGroup() instead.

    Parameters:
    name -
    See Also:
    addInputGroup(Collection)

    addInputGroup

    public void addInputGroup(Collection<String> names)
    Add a group if inputs at once. Advantage over single addInput() above is that for multiple entrys, this method only remaps the input to the first hidden layer once. The remapping function is expensive, so if you are doing more than a handful, use this.

    Parameters:
    names -
    See Also:
    addInput(String)

    addOutput

    public void addOutput(String name)
    Add an output with the given name.

    Parameters:
    name -

    removeOutput

    public void removeOutput(String name)
    Remove the output with the given name, if it exists

    Parameters:
    name -

    setInput

    public void setInput(String name,
                         double val)
    Sets the value of the given input to 'val'

    Parameters:
    name -
    val -

    getInputNode

    public Input getInputNode(String name)
    So that inputs can be set directly to avoid the Map lookup that happens when setInput is used. In order for this to give any speed improvement, the caller has to save the output from this call somewhere. Note: caller is responcible for calling incSerial() on the Brain after inputs are updated. If that is not done, the neurons will not re-read their inputs and wont do anything.

    Parameters:
    name - name of input to get
    Returns:
    The input node for the input
    See Also:
    incSerial(), setInput(String, double)

    incSerial

    public void incSerial()
    Manually increment the serial number. Useful when updating the values of input nodes directly (after getting the input nodes with getInputNode()

    See Also:
    getInputNode(String)

    setAllInput

    public void setAllInput(double val)
    Set all inputs to the given value. Useful in cases where not every input is set on every pass.

    Parameters:
    val - - value to set inputs to

    getOutput

    public double getOutput(String name)
    Get the output from the single output neuron. It will internally cache until the inputs change, so it may be called repeatedly without incurring recomputation expense.

    If called after inputs change, the network will of course have to be traversed to get the answer

    Note: this can also be used to get the output value of hidden neurons if you know their name. Hidden neurons are named "hidden_L_N" where L is the layer number (zero indexed) and N is their position inside the layer (zero indexed).

    If you ask for a non-existant neuron, you will get a null pointer exception

    Returns:
    output value for neuron

    getAllOutputs

    public Map<String,Double> getAllOutputs()
    Get the output values for all output neurons in a map

    Returns:
    a map of output names to values

    backPropogate

    public void backPropogate(String name,
                              double alpha,
                              double target)
    Do back propogation learning with the given alpha and target. Time complexity n where n is number of neuron links

    Parameters:
    name - - Output name to learn
    alpha - - The learning factor. Should be positive and less than one. 0.01 is a good starting point.
    target - - What the output value should be for the set inputs

    backPropogate

    public void backPropogate(double alpha,
                              Map<String,Double> targets)
    Do back propogation on a group of output targets at once. Time complexity n where n is number of neuron links

    Parameters:
    alpha - - learning factor. Should be positive and less than one. 0.01 is a good starting point.
    targets - a map of string(output names) to doubles (target output values) to learn on

    toString

    public String toString()
    Overrides:
    toString in class Object