usage - otavanopisto/kohonen GitHub Wiki

Usage

Create a Network

To get a functioning Network object, you can use the included default implementation KohonenNetwork.

In the example below we create a network with four neurons and two inputs each scaled between 0.0 and 1.0.

  int neuronCount = 4;
  double[] maxWeights = new double[] { 1.0, 1.0 };
  Network network = new KohonenNetwork(neuronCount, maxWeights);

Teach it

There are two methods to teach the map, WTA (Winner Takes All) and WTM (Winner Takes Most). As names imply, WTA modifies only one neuron for each input every epoch. WTM instead utilizes topology to figure out which neurons to train and by how much.

WTA

Here is a basic example of WTA training.

  • Training modifier is set to start at 0.2 (at first epoch the winning neurons are moved by this multiplier towards input) and it decreases in linear fashion when the epochs increase.
  • Maximum epochs is set at 20000.
  List<double[]> data;
  WTATrainingAlgorithm learning = new WTATrainingAlgorithm(
      new LinearTrainingModifier(0.2, 20000), 
      20000);
  learning.train(network, data);

WTM

Here's a basic example of WTM training.

  • LinearTrainingModifier sets modifier to 0.2 at start, which decreases in linear fashion over time
  • GaussNeighborhoodModifier works together with topology to train also the neighbouring neurons
  • 30000 is our max epochs here.
  List<double[]> data;
  Topology topology = new MatrixTopology(2, 2);
  WTMTrainingAlgorithm learning = new WTMTrainingAlgorithm(
    topology,
    new LinearTrainingModifier(0.2, 30000), 
    new GaussNeighborhoodModifier(2),
    30000);
  learning.train(network, data);

WTM with more parameters

Below we have a more complex example which utilizes more modifiers:

  • First parameter defines the training modifier which decreases in linear fashion throughout the whole 50000 iterations
  • Second parameter defines Gauss neigborhood modifier with Gauss function width of 5. In addition, neighborhood will get shrunk over time by the second parameter here, where again we use Gauss function.
  • Third we have a training end clause which will halt the training if the defined threshold is met over given amount of epochs.
  • Fourth the usual maximum number of epochs to run
  List<double[]> data;
  Topology topology = new MatrixTopology(2, 2);
  WTMTrainingAlgorithm learning = new WTMTrainingAlgorithm(
    topology,
    new LinearTrainingModifier(0.2, 50000), 
    new GaussNeighborhoodModifier(5, new GaussTrainingModifier(10000)),
    new MinDistanceTrainingEndClause(5, 0.001),
    50000);
  learning.train(network, data);

Save a network

Default implementation of Network (KohonenNetwork) provides easy way to output the map data into a stream. For example, if you want to have it as string, you can use ByteArrayOutputStream:

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  network.networkToStream(baos);
  String networkData = baos.toString(); 

Load a network

Loading a network is just as simple as saving it. Just provide the default implementation a stream that points to the map data.

Example below uses apache-commons-io to make a readable stream from a string for the map to initialize:

  String map_data;
  Network network = new KohonenNetwork(IOUtils.toInputStream(map_data));

UMatrix

You can create an UMatrix easily with UMatrix class.

  Topology topology = new MatrixTopology(10, 10);
  UMatrix uMatrix = UMatrix.createUMatrix(network, topology);

UMatrix.getDistance then returns the sum of distances to neighbouring neurons for each neuron.

⚠️ **GitHub.com Fallback** ⚠️