Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Understanding machine learning using simple code examples.
â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.
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 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 first team won and if the output is 1 then that means 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 your 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 an 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 you 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
How to Understand Machine Learning with simple Code Examples was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.