# Classify colors

Edit on Github

Classifying is something a neural network can do quite well. In this article I will demonstrate how you can set up the evolution process of a neural network that learns to classify colors with Neataptic.

Colors: Red Orange Yellow Green Blue Purple Pink Monochrome

Start evolution

    Iteration: 0                                                        Best-fitness: 0

## Set sorted by color

## Set sorted by NN

### How it works

The algorithm to this classification is actually pretty easy. One of my biggest problem was generating the colors, however I stumbled upon this Javascript library that allows you to generate colors randomly by name - exactly what I needed (but it also created a problem, read below). So I used it to create a training set:

    function createSet(){
      var set = [];

      for(index in COLORS){
        var color = COLORS[index];

        var randomColors = randomColor({ hue : color, count: PER_COLOR, format: 'rgb'});

        for(var random in randomColors){
          var rgb = randomColors[random];
          random = rgb.substring(4, rgb.length-1).replace(/ /g, '').split(',');
          for(var y in random) random[y] = random[y]/255;

          var output = Array.apply(null, Array(COLORS.length)).map(Number.prototype.valueOf, 0);
          output[index] = 1;

          set.push({ input: random, output: output, color: color, rgb: rgb});
        }
      }

      return set;
    }

COLORS is an array storing all color names in strings. The possible colors are listed above. Next, we convert this rgb string to an array and normalize the values between 0 and 1. Last of all, we normalize the colors using one-hot encoding. Please note that the `color`and `rgb` object attributes are irrelevant for the algorithm.

    network.evolve(set, {
      iterations: 1,
      mutationRate: 0.6,
      elisitm: 5,
      popSize: 100,
      mutation: methods.mutation.FFW,
      cost: methods.cost.MSE
    });

Now we create the built-in genetic algorithm in neataptic.js. We define that we want to use all possible mutation methods and set the mutation rate higher than normal. Sprinkle in some elitism and double the default population size. Experiment with the parameters yourself, maybe you'll find even better parameters!

The fitness function is the most vital part of the algorithm. It basically calculates the Mean Squared Error of the entire set. Neataptic saves the programming of this fitness calculation. At the same time the default `growth` parameter is used, so the networks will get penalized for being too large.

And putting together all this code will create a color classifier.