Package libai.nn.unsupervised
Class Competitive
- java.lang.Object
-
- libai.nn.NeuralNetwork
-
- libai.nn.unsupervised.UnsupervisedLearning
-
- libai.nn.unsupervised.Competitive
-
- All Implemented Interfaces:
java.io.Serializable
public class Competitive extends UnsupervisedLearning
Competitive Learning is an unsupervised network where "the winner takes all". A pattern is presented to each neuron, the closest neuron wins the right to be updated. The update makes this neuron fittest for that pattern in the future. Finally, the network learns a set of descriptive patterns or centroids (if is compared with a clustering algorithm). In general, the output for that network will be a binary codified class, when only one bit on (the winner). One of the problems with the competitive learning is the unable to know beforehand, the order of the output. For example, suppose we have 3 possible outputs (100, 010, 001). For the first random initialization, the winner for the first pattern is the neuron 0, so the output is 100. But, if retrain with a new random initialization, the winner for the same pattern will be the neuron 2, so the output is 001. That is not necessary incorrect because the relative order could be maintained between the inputs. Therefore, before calculate any error metric we need to label the patterns with the respective answers, in a similar way as Kohonen does.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Competitive(int in, int out)
Constructor.Competitive(int in, int out, java.util.Random rand)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description double
error(Column[] patterns, Column[] answers, int offset, int length)
Calculate the error using the average distance between the closest neuron.Column
simulate(Column pattern)
Calculates the output for thepattern
.void
simulate(Column pattern, Column result)
Calculate the output for thepattern
and left the result inresult
.protected int
simulateNoChange(Matrix pattern)
void
train(Column[] patterns, double alpha, int epochs, int offset, int length)
Train the network using "the winner takes all".-
Methods inherited from class libai.nn.NeuralNetwork
error, euclideanDistance2, euclideanDistance2, gaussian, getDefaultRandomGenerator, getPlotter, getProgressBar, initializeProgressBar, open, open, open, save, setPlotter, setProgressBar, train, train
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from class libai.nn.unsupervised.UnsupervisedLearning
train, validatePreconditions
-
-
-
-
Field Detail
-
W
protected Matrix W
-
ins
protected int ins
-
outs
protected int outs
-
-
Constructor Detail
-
Competitive
public Competitive(int in, int out)
Constructor. Creates a network with the specified number of inputs and outputs.- Parameters:
in
- Number of inputs.out
- Number of outputs.
-
Competitive
public Competitive(int in, int out, java.util.Random rand)
Constructor. Creates a network with the specified number of inputs and outputs.- Parameters:
in
- Number of inputs.out
- Number of outputs.rand
- Random generator used for creating matrices
-
-
Method Detail
-
train
public void train(Column[] patterns, double alpha, int epochs, int offset, int length)
Train the network using "the winner takes all". For each neuron the Euclidean distance between the pattern and the neuron is calculated. The position with the lowest distance is updated with the rule:
Ww = Ww + alpha.(pattern - Ww)- Specified by:
train
in classUnsupervisedLearning
- Parameters:
patterns
- The patterns to be learned.alpha
- The learning rate.epochs
- The maximum number of iterationsoffset
- The first pattern positionlength
- How many patterns will be used.
-
simulate
public Column simulate(Column pattern)
Description copied from class:NeuralNetwork
Calculates the output for thepattern
.- Specified by:
simulate
in classNeuralNetwork
- Parameters:
pattern
- Pattern to use as input.- Returns:
- The output for the neural network.
-
simulate
public void simulate(Column pattern, Column result)
Calculate the output for thepattern
and left the result inresult
. The result will be a row matrix fill with 0 except for the winner position.- Specified by:
simulate
in classNeuralNetwork
- Parameters:
pattern
- Pattern to use as input.result
- The output for the input.
-
simulateNoChange
protected int simulateNoChange(Matrix pattern)
-
error
public double error(Column[] patterns, Column[] answers, int offset, int length)
Calculate the error using the average distance between the closest neuron. Less distance means less error and vice versa.- Overrides:
error
in classNeuralNetwork
- Parameters:
patterns
- The array with the patterns to testanswers
- The array with the expected answers for the patterns.offset
- The initial position inside the array.length
- How many patterns must be taken from the offset.- Returns:
- The average distance between the pattern and the winner for that pattern.
-
-