How to Understand Machine Learning with simple Code Examples

Written by pradyumandixit | Published 2019/03/08
Tech Story Tags: artificial-intelligence | machine-learning | deep-learning | neural-networks | javascript

TLDRvia the TL;DR App

“Machine Learning, Artificial Intelligence, Deep Learning, Data Science, Neural Networks”

You must’ve surely read somewhere about how these things are gonna take away future jobs, overthrow us as dominant species on earth and how we’d have to find Arnold Schwarzenegger and John Connor to save humanity.

DDI Editor's Pick: 5 Machine Learning Books That Turn You from Novice to Expert - Data Driven…_The booming growth in the Machine Learning industry has brought renewed interest in people about Artificial…_www.datadriveninvestor.com

With current hype, there is no surprise you might have.

But, what is Machine Learning and what is Artificial Intelligence?

Machine learning is the scientific study of algorithms and statistical models that computer systems use to effectively perform a specific task without using explicit instructions, relying on patterns and inference instead. It is seen as a subset of artificial intelligence.

And what is Neural Network?

Artificial neural networks or connectionist systems are computing systems inspired by the biological neural networks that constitute animal brains. The neural network itself is not an algorithm, but rather a framework for many different machine learning algorithms to work together and process complex data inputs.

Yes, machine learning is a subset of Artificial Intelligence, and no, AI does not mean Terminators who’ve spread using the internet and have a hub called Skynet.

Traditional forms of programming rely on a specific set of instructions for the computer in a specific language, which a compiler turns to assembly and machine code which the CPU understands and executes.

So, we decide what computer would give us for output. We’re the brain in this process and we tell computers exactly what to do and when to do it.

That’s the general premise of traditional programming.

Machine learning here is a bit different. You use data to train your machine learning model to let the machine make decisions based on the outcomes of this training.

Confused? Here’s an example:

How do you understand any new concept as a human? You see the concept, try to figure out what it’s saying, see some examples and understand how to do similar examples using the same concept.

Right?

This is exactly what machine learning is, except here we give the examples to our model which chunks out the output based on previous outputs found in the data.

Yes, this joke somewhat coarsely represents how machine learning works.

Now that you simply understand the concept of machine learning, let’s get into some simple code examples.

Here, I’ll be using the machine learning library ‘brain.js’ and JavaScript and Node.js.

For this example, we’d be using a simple and very small amount of data.

So we have 4 football teams as input, namely 1, 2, 3, 4. Why are these not interesting names and just numbers?

Well, I am not that innovative, blame my un-originality.

So, if the output is 0, that means the first team won and if the output is 1 then that means the second team won.E.g. input: [1, 3], output: [1] → Team “3" won.

So, now let’s code this.

// Here we used the brain.js library to get a ready neural networkconst brain = require('brain.js');const network = new brain.NeuralNetwork();

// Now let's train the datanetwork.train([{ input: [1, 2], output: [1] }, // team 2 wins{ input: [1, 3], output: [1] }, // team 3 wins{ input: [2, 3], output: [0] }, // team 2 wins{ input: [2, 4], output: [1] }, // team 4 wins{ input: [1, 2], output: [0] }, // team 1 wins{ input: [1, 3], output: [0] }, // team 3 wins{ input: [3, 4], output: [0] } // team 3 wins]);

This code trained your neural network on the basis of the data provided. Now you can get probable output for any team’s winning using machine learning.

How? Well, like this:

const output = network.run([1, 4]);console.log(`Prob: ${output}`);

And yes, you’ve built yourself a machine learning model which has been trained using your data, which can predict which team would win based on that data.

But of course, real-world machine learning can’t rely on 7 lines of input data. Lots of data is used to get desirable results with the maximum accuracy possible.

So let’s get into another example with a larger amount of data.

We’d use this data file for our input data. Naming the file as (data.json).

[{ "text": "my unit test failed","category": "software"},{ "text": "tried the program, but it was buggy","category": "software"},{ "text": "i need a new power supply","category": "hardware"},{ "text": "the drive has a 2TB capacity","category": "hardware"},{ "text": "unit-tests","category": "software"},{ "text": "program","category": "software"},{ "text": "power supply","category": "hardware"},{ "text": "drive","category": "hardware"},{ "text": "it needs more memory","category": "hardware"},{ "text": "code","category": "software"},{ "text": "i found some bugs in the code","category": "software"},{ "text": "i swapped the memory","category": "hardware"},{ "text": "i tested the code","category": "software"}]

The above JSON data file has some sentences and a category has been allocated to it.

Our machine learning model will take a line as input and tell the category it belongs to.

So let’s get into some code.

const brain = require('brain.js');const data = require('./data.json');

const network = new brain.recurrent.LSTM();

const trainingData = data.map(item => ({input: item.text,output: item.category}));

network.train(trainingData, { iterations: 2000});

This above code uses the library to create a long short term memory (LSTM) neural network which is trained with about data for 2000 iterations.

For better results, we train our model many times with the same data to get more accuracy in results. Think of it like doing the same example question many times, until you get it perfect without making any mistakes.

You can test your network like this:

const output = network.run('I fixed the power suppy');// Category: hardware

const output = network.run('The code has some bugs');// Category: software

console.log(`Category: ${output}`);

And yes, you’ve built yourself a more complex machine learning model which computes category based on the statement belongs to.

What are you waiting for?

Go and Show Off your neural networks !!

In case we’re meeting for the first time here, I am Pradyuman Dixit and I mostly write about Machine learning, Android Development and sometimes about Web Development.

You can read my other Machine Learning posts here:

How to make a simple Machine Learning Website from Scratch

How to make a Machine Learning Android Game as a beginner


Published by HackerNoon on 2019/03/08