Class Network
- java.lang.Object
-
- org.apache.commons.math3.ml.neuralnet.Network
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<Neuron>
public class Network extends java.lang.Object implements java.lang.Iterable<Neuron>, java.io.Serializable
Neural network, composed ofNeuron
instances and the links between them. Although updating a neuron's state is thread-safe, modifying the network's topology (adding or removing links) is not.- Since:
- 3.3
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Network.NeuronIdentifierComparator
Comparator that prescribes an order of the neurons according to the increasing order of their identifier.
-
Constructor Summary
Constructors Constructor Description Network(long initialIdentifier, int featureSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addLink(Neuron a, Neuron b)
Adds a link from neurona
to neuronb
.Network
copy()
Performs a deep copy of this instance.long
createNeuron(double[] features)
Creates a neuron and assigns it a unique identifier.void
deleteLink(Neuron a, Neuron b)
Deletes the link between neuronsa
andb
.void
deleteNeuron(Neuron neuron)
Deletes a neuron.int
getFeaturesSize()
Gets the size of the neurons' features set.java.util.Collection<Neuron>
getNeighbours(java.lang.Iterable<Neuron> neurons)
Retrieves the neurons in the neighbourhood of any neuron in theneurons
list.java.util.Collection<Neuron>
getNeighbours(java.lang.Iterable<Neuron> neurons, java.lang.Iterable<Neuron> exclude)
Retrieves the neurons in the neighbourhood of any neuron in theneurons
list.java.util.Collection<Neuron>
getNeighbours(Neuron neuron)
Retrieves the neighbours of the given neuron.java.util.Collection<Neuron>
getNeighbours(Neuron neuron, java.lang.Iterable<Neuron> exclude)
Retrieves the neighbours of the given neuron.Neuron
getNeuron(long id)
Retrieves the neuron with the given (unique)id
.java.util.Collection<Neuron>
getNeurons(java.util.Comparator<Neuron> comparator)
Creates a list of the neurons, sorted in a custom order.java.util.Iterator<Neuron>
iterator()
-
-
-
Method Detail
-
copy
public Network copy()
Performs a deep copy of this instance. Upon return, the copied and original instances will be independent: Updating one will not affect the other.- Returns:
- a new instance with the same state as this instance.
- Since:
- 3.6
-
iterator
public java.util.Iterator<Neuron> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<Neuron>
-
getNeurons
public java.util.Collection<Neuron> getNeurons(java.util.Comparator<Neuron> comparator)
Creates a list of the neurons, sorted in a custom order.- Parameters:
comparator
-Comparator
used for sorting the neurons.- Returns:
- a list of neurons, sorted in the order prescribed by the
given
comparator
. - See Also:
Network.NeuronIdentifierComparator
-
createNeuron
public long createNeuron(double[] features)
Creates a neuron and assigns it a unique identifier.- Parameters:
features
- Initial values for the neuron's features.- Returns:
- the neuron's identifier.
- Throws:
DimensionMismatchException
- if the length offeatures
is different from the expected size (as set by theconstructor
).
-
deleteNeuron
public void deleteNeuron(Neuron neuron)
Deletes a neuron. Links from all neighbours to the removed neuron will also bedeleted
.- Parameters:
neuron
- Neuron to be removed from this network.- Throws:
java.util.NoSuchElementException
- ifn
does not belong to this network.
-
getFeaturesSize
public int getFeaturesSize()
Gets the size of the neurons' features set.- Returns:
- the size of the features set.
-
addLink
public void addLink(Neuron a, Neuron b)
Adds a link from neurona
to neuronb
. Note: the link is not bi-directional; if a bi-directional link is required, an additional call must be made witha
andb
exchanged in the argument list.- Parameters:
a
- Neuron.b
- Neuron.- Throws:
java.util.NoSuchElementException
- if the neurons do not exist in the network.
-
deleteLink
public void deleteLink(Neuron a, Neuron b)
Deletes the link between neuronsa
andb
.- Parameters:
a
- Neuron.b
- Neuron.- Throws:
java.util.NoSuchElementException
- if the neurons do not exist in the network.
-
getNeuron
public Neuron getNeuron(long id)
Retrieves the neuron with the given (unique)id
.- Parameters:
id
- Identifier.- Returns:
- the neuron associated with the given
id
. - Throws:
java.util.NoSuchElementException
- if the neuron does not exist in the network.
-
getNeighbours
public java.util.Collection<Neuron> getNeighbours(java.lang.Iterable<Neuron> neurons)
Retrieves the neurons in the neighbourhood of any neuron in theneurons
list.- Parameters:
neurons
- Neurons for which to retrieve the neighbours.- Returns:
- the list of neighbours.
- See Also:
getNeighbours(Iterable,Iterable)
-
getNeighbours
public java.util.Collection<Neuron> getNeighbours(java.lang.Iterable<Neuron> neurons, java.lang.Iterable<Neuron> exclude)
Retrieves the neurons in the neighbourhood of any neuron in theneurons
list. Theexclude
list allows to retrieve the "concentric" neighbourhoods by removing the neurons that belong to the inner "circles".- Parameters:
neurons
- Neurons for which to retrieve the neighbours.exclude
- Neurons to exclude from the returned list. Can benull
.- Returns:
- the list of neighbours.
-
getNeighbours
public java.util.Collection<Neuron> getNeighbours(Neuron neuron)
Retrieves the neighbours of the given neuron.- Parameters:
neuron
- Neuron for which to retrieve the neighbours.- Returns:
- the list of neighbours.
- See Also:
getNeighbours(Neuron,Iterable)
-
getNeighbours
public java.util.Collection<Neuron> getNeighbours(Neuron neuron, java.lang.Iterable<Neuron> exclude)
Retrieves the neighbours of the given neuron.- Parameters:
neuron
- Neuron for which to retrieve the neighbours.exclude
- Neurons to exclude from the returned list. Can benull
.- Returns:
- the list of neighbours.
-
-