Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Tutorial ‐ Supervised training

Jeremy Stucki edited this page Nov 27, 2016 · 1 revision

Classification

If you have a collection of classified (or labeled) data, you can let Hippocrates learn how to classify new data.
Your data has to be a std::vector of a type that is convertible to float.

Spoiler alert, the whole thing looks like this

// Classifications
enum class XORResult {
	Zero,
	One,
	
	ClassificationCount
};

// Datasets
Training::Data<XORResult> data;
data.AddSet( {0, 0}, XORResult::Zero );
data.AddSet( {0, 1}, XORResult::One );
data.AddSet( {1, 0}, XORResult::One );
data.AddSet( {1, 1}, XORResult::Zero );

//Training
Training::NeuralNetworkTrainer trainer;
auto champ = TrainSupervised(data)

We're now gonna go through the individual steps.

Declaring the classifications

enum class YourClassifications {
	SomeLabel,
	AnotherOne,
	LastClassification,
	
	ClassificationCount
};

All the possible classifications have to be located in an enum class and can be called however you want.
Requirements of the enum class:

  1. The underlying type is convertible to std::size_t
  2. You can not alter its members' underlying number
  3. The last element has to be called exactly ClassificationCount

Setting the datasets up

Training::Data<YourClassifications> data;
data.AddSet( {3, 42, 123}, YourClassifications::SomeLabel );
data.AddSet( {-123, 1337, 10}, YourClassifications::AnotherOne );
data.AddSet( {80085, 455, 1337}, YourClassifications::LastClassification );

A single set of values that have one classification is called a Training::Data::Set.
All of your sets togheter are the Training::Data. When you instantiate it, you'll need to provide your classification type from the last section as a template parameter.

Then you can add as many sets of data that you want.
Please note that this line

data.AddSet( {3, 42, 123}, YourClassifications::SomeLabel );

is the same as these

Training::Data::Set<YourClassifications> set;
set.input = {3, 42, 123};
set.classification = YourClassifications::SomeLabel;

data.AddSet( std::move(set) );

Now you may be asking where you can get the Training::Data::Set::input values from. This is your job. Convert it any real life values that you need somehow into a type that is convertible to a float.

The actual learning

Training::NeuralNetworkTrainer trainer;
auto champ = trainer.TrainSupervised(data)

This should be self explanatory. Training::NeuralNetworkTrainer::TrainSupervised learns until a neural network is able to correctly classify all training datasets and then returns it. You can now use this champ to classify new data:

auto previouslyUnknownClassification = champ.Classify({1, 3, 5});

Loading and saving

When you're done, you may whish to save the champ to a file so you don't have to train him again. You can simply stream him into file for that:

std::ofstream file {"champ.nn"};
file << champ;
file.close();

The file ending doesn't matter, we stylisticly recommend .nn, which stands for neural network.
You can load your neural network like this

std::ifstream savedNN{"champ.nn"};
Training::Trained::Classifier<YourClassifications> loadedChamp {savedNN};