Getting Started

The following guide shows you the five steps needed to build a machine learning object and use it. Each step has both instructions for JavaScript and C++.

You can find the full C++ example as HelloRapidMix in the /example folder of the C++ downloadable. You can find the full JavaScript example on CodeCircle here.

What are we doing in this example?

It is possible to think of a machine learning algorithm like any other algorithm or transfer function — the user provides an input, the algorithm is applied, and a specific output is returned.

For this example, we are going to try to make a machine learning algorithm, or model, that takes MIDI note numbers as input, and outputs the frequency in Hertz that corresponds with that note. We don’t really need machine learning to do this conversion as there’s a formula we can use. However, we’re looking at a familiar case and seeing if and how we can reproduce it in a new way.

NodeJS:

C++:

If you’re using JavaScript, just include the tag in your <head> element. If you’re using C++, you need to include the rapidmix header file.

2. Create Object

JavaScript:

JavaScript

1

2

varrapidLib=window.RapidLib();

varmyRegression=newrapidLib.Regression();

C++:

C++

1

rapidmix::staticRegression myRegression;

There are several kinds of machine learning we can use – and you can find our more about them on our general concepts page. For this example, we’re using a regression model as we wish to create new outputs based on our input, rather than the closest match from what we tell our model.

3. Populate training data

JavaScript:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

varmyData=[

{

input:[48],

output:[130.81]

},

{

input:[54],

output:[185.00]

},

{

input:[60],

output:[261.63]

},

{

input:[66],

output:[369.994]

},

{

input:[72],

output:[523.25]

}

];

C++:

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

rapidmix::trainingData myData;

// Setting up the first element of training data

std::vector<double>input{48};

std::vector<double>output{130.81};

myData.recordSingleElement(input,output);

// More elements

input={54};

output={185.00};

myData.recordSingleElement(input,output);

input={60};

output={261.63};

myData.recordSingleElement(input,output);

input={66};

output={369.994};

myData.recordSingleElement(input,output);

input={72};

output={523.25};

myData.recordSingleElement(input,output);

In order to train a model to understand our input, we need to give it some training inputs and the expected outputs. Of course, no machine learning algorithm is going to be able to reproduce an algorithm with one point, so here we add 5. Note in C++ that addElement() expects input and output to be given as a vector out doubles.

4. Train object

JavaScript:

JavaScript

1

myRegression.train(myData);

C++:

C++

1

myRegression.train(myData);

Training is the process of adjusting the internal parameters of a given model to the specific training data provided. Ideally, the trained model will give a reasonable approximation of the desired output, given a specific input. The actual computation that does the training can be very different for different algorithms. Neural Networks can take a relatively large amount of computation to train, but then they run quite quickly. Other algorithms offer different compromises between training and running efficiencies. Now our model is good to use…

5. Use object with new inputs

JavaScript:

JavaScript

1

2

3

varrapidInput=[64];

varfreqHz=myRegression.run(rapidInput);

console.log(freqHz);

C++:

C++

1

2

3

std::vector<double>rapidInput={double(64)};

doublefreqHz=myRegression.run(rapidInput)[0];

cout<<freqHq;

Once an algorithm is trained, you can give it input and get output back.

Finishing up

Congratulations, you’ve now created your first model, trained it, and used it!

You might notice that the frequency values are not very similar to what we would hope for, if you know what the formula. It turns out that the default staticRegression algorithm is not very good at representing this type of function. For better performance, we might try a different algorithm

Next steps

Learn about the xmmStaticRegression algorithm for a more accurate result