Class Neuron
- java.lang.Object
-
- org.apache.commons.math3.ml.neuralnet.Neuron
-
- All Implemented Interfaces:
java.io.Serializable
public class Neuron extends java.lang.Object implements java.io.SerializableDescribes a neuron element of a neural network. This class aims to be thread-safe.- Since:
- 3.3
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classNeuron.SerializationProxySerialization.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicReference<double[]>featuresNeuron data.private longidentifierIdentifier.private java.util.concurrent.atomic.AtomicLongnumberOfAttemptedUpdatesNumber of attempts to update a neuron.private java.util.concurrent.atomic.AtomicLongnumberOfSuccessfulUpdatesNumber of successful updates of a neuron.private static longserialVersionUIDSerializable.private intsizeLength of the feature set.
-
Constructor Summary
Constructors Constructor Description Neuron(long identifier, double[] features)Creates a neuron.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancompareAndSetFeatures(double[] expect, double[] update)Tries to atomically update the neuron's features.private booleancontainSameValues(double[] current, double[] expect)Checks whether the contents of both arrays is the same.Neuroncopy()Performs a deep copy of this instance.double[]getFeatures()Gets the neuron's features.longgetIdentifier()Gets the neuron's identifier.longgetNumberOfAttemptedUpdates()Retrieves the number of calls to thecompareAndSetFeaturesmethod.longgetNumberOfSuccessfulUpdates()Retrieves the number of successful calls to thecompareAndSetFeaturesmethod.intgetSize()Gets the length of the feature set.private voidreadObject(java.io.ObjectInputStream in)Prevents proxy bypass.private java.lang.ObjectwriteReplace()Custom serialization.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serializable.- See Also:
- Constant Field Values
-
identifier
private final long identifier
Identifier.
-
size
private final int size
Length of the feature set.
-
features
private final java.util.concurrent.atomic.AtomicReference<double[]> features
Neuron data.
-
numberOfAttemptedUpdates
private final java.util.concurrent.atomic.AtomicLong numberOfAttemptedUpdates
Number of attempts to update a neuron.
-
numberOfSuccessfulUpdates
private final java.util.concurrent.atomic.AtomicLong numberOfSuccessfulUpdates
Number of successful updates of a neuron.
-
-
Constructor Detail
-
Neuron
Neuron(long identifier, double[] features)Creates a neuron. The size of the feature set is fixed to the length of the given argument.
Constructor is package-private: Neurons must becreatedby the network instance to which they will belong.- Parameters:
identifier- Identifier (assigned by theNetwork).features- Initial values of the feature set.
-
-
Method Detail
-
copy
public Neuron 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
-
getIdentifier
public long getIdentifier()
Gets the neuron's identifier.- Returns:
- the identifier.
-
getSize
public int getSize()
Gets the length of the feature set.- Returns:
- the number of features.
-
getFeatures
public double[] getFeatures()
Gets the neuron's features.- Returns:
- a copy of the neuron's features.
-
compareAndSetFeatures
public boolean compareAndSetFeatures(double[] expect, double[] update)Tries to atomically update the neuron's features. Update will be performed only if the expected values match the current values.
In effect, when concurrent threads call this method, the state could be modified by one, so that it does not correspond to the the state assumed by another. Typically, a callerretrieves the current state, and uses it to compute the new state. During this computation, another thread might have done the same thing, and updated the state: If the current thread were to proceed with its own update, it would overwrite the new state (which might already have been used by yet other threads). To prevent this, the method does not perform the update when a concurrent modification has been detected, and returnsfalse. When this happens, the caller should fetch the new current state, redo its computation, and call this method again.- Parameters:
expect- Current values of the features, as assumed by the caller. Update will never succeed if the contents of this array does not match the values returned bygetFeatures().update- Features's new values.- Returns:
trueif the update was successful,falseotherwise.- Throws:
DimensionMismatchException- if the length ofupdateis not the same as specified in theconstructor.
-
getNumberOfAttemptedUpdates
public long getNumberOfAttemptedUpdates()
Retrieves the number of calls to thecompareAndSetFeaturesmethod. Note that if the caller wants to use this method in combination withgetNumberOfSuccessfulUpdates(), additional synchronization may be required to ensure consistency.- Returns:
- the number of update attempts.
- Since:
- 3.6
-
getNumberOfSuccessfulUpdates
public long getNumberOfSuccessfulUpdates()
Retrieves the number of successful calls to thecompareAndSetFeaturesmethod. Note that if the caller wants to use this method in combination withgetNumberOfAttemptedUpdates(), additional synchronization may be required to ensure consistency.- Returns:
- the number of successful updates.
- Since:
- 3.6
-
containSameValues
private boolean containSameValues(double[] current, double[] expect)Checks whether the contents of both arrays is the same.- Parameters:
current- Current values.expect- Expected values.- Returns:
trueif the arrays contain the same values.- Throws:
DimensionMismatchException- if the length ofexpectedis not the same as specified in theconstructor.
-
readObject
private void readObject(java.io.ObjectInputStream in)
Prevents proxy bypass.- Parameters:
in- Input stream.
-
writeReplace
private java.lang.Object writeReplace()
Custom serialization.- Returns:
- the proxy instance that will be actually serialized.
-
-