First Step in (supervised) Machine Learning using Tensorflow.js

First Step in (supervised) Machine Learning using Tensorflow.js

This article is aimed at people who are,just like me, driven by curiosity to learn machine learning and finds reading tea leaves interesting. This article was originally posted on Medium . I've made a few modifications to it in order to accommodate the acquired knowledge post-writing it, with the hopes of making the content more thicker,richer and easy to understand.My two objectives are:

  1. To reinforce my learning (pun intended)
  2. Help others to learn from me

we will look at what is (supervised) machine learning and understanding some machine learning concepts .We will also look at what TensorFlow.js is and some important components that comes with it .

By the end of this post we should :

  1. be able to explain what machine Learning is .
  2. understand fundamental concept of machine learning.
  3. know what tensorflow.js is.
  4. be familiar with tensorflow.js components


What is machine Learning?

Remember when i said this article is aimed at people who are interested in reading tea leaves? yeah, what i really meant is that people who are interested in Machine learning because

Machine learning it's like reading tea leaves.Because it's about recognizing patterns in data in order to predict what comes next. except that it works.


 Machine Learning is a field of study that gives computers the capability to learn and act without explicitly being programmed .This means computers can learn on their own without having pre-programmed rules and models.The computer learns from experience ,by feeding it labeled data and training it from inputs(Features) to predict better output (label) over time.The machine learning algorithm enables it to identify patterns in the input x (Features) and output y (label) , then builds a model that explains the relationship between the features and label .The model is then used to predict things without having explicit pre-programmed rules and models.

Machine Learning Fundamentals


Labels- A label is the thing we’re predicting ,the output variable .The y variable in simple linear regression model y=wx+b

Features -feature is an input variable — the x variable in simple linear regression model y=wx+b . A simple machine learning project might use a single feature(one-dimension), while a more complex machine learning project could use millions of features.(x-dimension)

Labeled Data - labeled data is the data that contains the information you are trying to predict i.e labels . let's say you want to predicts the sale price of houses. Your training data might consist of a bunch of information about each house (features): the number of bedrooms, the number of bathrooms, the square footage, the area code etc. In order to predict the sale price of houses , you’ll also want your training data to include the sale price of those houses (labels).

Models -A model defines the relationship between features and labels. For example, a spam detection model might associate certain features strongly with “spam”. Let’s highlight two phases of a model’s life:

  • Training means creating or learning the model. That is, you show the model labeled data and enable the model to gradually learn the relationships between features and label.
  • Inference means applying the trained model to unlabeled data. That is, you use the trained model to make useful predictions (y'). For example, during inference, you can predict whether a new unseen email is “spam” or not .

Regression vs Classification

regression model predicts continuous values. For example, regression models make predictions that answer questions like the following:

  • What is the value of a house in the Southern Suburb of Cape Town?
  • What is the probability that oomf retweets my tweet?

classification model predicts discrete values. For example, classification models make predictions that answer questions like the following:

  • Is a given email message spam or not spam?
  • Is this an image of a dog, a cat, or Kenneth?

Tensorflow.js

To add machine learning capabilities to a web application , we use tensorflow.js . Tensorflow.js is an open source webGL accelerated,Javascript Machine learning library that exposes apis that allows us to train and build machine learning models straight from the browser or our nodejs server.It also gives us the capability to run existing models in our javascript enviroment . It is based on it’s parent python machine learningframework ,also called Tensorflow without the js extension though.

Tensorflow.js allows us to use data that is available from the client-side “e.g image from your web camera” to retrain pre-existing machine learningmodels that you’ve trained before or imported from python or keras.If you’re keen on seeing projects that were build with data from the client-side in action ,then visit this link https://js.tensorflow.org/ .

Tensorflow Fundamentals

lets look at some main components of tensorflow .the building blocks of the library

Tensors


Tensors are the central unit of data in tensorflow.Tensors can contain a set of data that consist of one or many arrays(dimensions) and tensors are immutable.Meaning once you create a tensor ,it cannot be changed afterwards.To create a new Tensor you need to define its dimension ,sometimes referred as the shape by passing in the shape as a second argument in the tensor function like below

const t1 = tf.tensor([1,2,3,4,2,4,6,8]), [2,4]);

Notice how above we call a tensor function on a Tensor object (ts) provided by the Tensorflow API library and passing it our shape of two rows and four columns [2,4] as a second argument . The first argument is our input data as an array that we want to create a new tensor from.The resulting tensor will be as follows

[[1,2,3,4],

[2,4,6,8]]

Tensorflow.js can also infer the shape of a tensor . e.g

const t2 = tf.tensor([[1,2,3,4],[2,4,6,8]]);

will result in

[[1,2,3,4],

[2,4,6,8]]

which is the same as the last result from passing in the shape .

The Tensor object (tf) in the Tensorflow.js library comes with other set of functions we can use to enhance our code readability .

  • tf.scalar: Tensor with just one value
  • tf.tensor1d: one dimension Tensor
  • tf.tensor2d: two dimensional Tensor
  • tf.tensor3d: three dimensional Tensor
  • tf.tensor4d: four dimensional Tensor

Another nifty function that we have at our disposal is tf.zeros function .which is useful is we want to create a tensor with all values set to zero.let me illustrate

const t_zeros = tf.zeros([2,3]);

this result in the following tensor of two rows , 3 columns:

[[0,0,0],

[0,0,0]]

Variables

variables are similar to tensors . the difference is that variables are mutable . meaning that we can set a variable and change is later if we wanted to.

Operations

Operations are functions used to manipulate data in tensors or variables .These operations on the tensor will always result in a new tensor with resulting values,this is because of the immutability property of tensors .

Tensorflow.js offers a range of operations , to name a few

  • add
  • sub
  • mul
  • square

to apply an operation on a tensor is a simple as

const t3 = tf.tensor2d([1,2], [3, 4]);

const t3_squared = t3.square();

the result of the operation is a new tensor with new values

[[1, 4 ],

[9, 16]]

Models and Layers

Layers are the primary building blocks of creating a Machine learning model .Layers perform computation on inputs to transform them to outputs.

For now, lets just accept that layers are used to build neural networks (models)which can be trained with data and then used to make prediction for further values based on the trained information.Each model is build up of one or many layers .This two components, models and layers are important when dealing with deep learning .

to get in depth and mathematical understanding of how neural networks and layers work in a practical situation .take a look at Samantha Van Der Merwepost on neural networks Learning the basics:Layers,matrix application



Whats Next?

In this post you’ve learned what machine learning is and some of its basic concepts.We also learned the basics of tensorflow.js and how to create a new tensor from data .By now you should have a basic understanding of the main building blocks of machine learning and tensorflow.

In the next post we will implement a simple machine learning web application ,based on a simple linear regression using tensorflow.js library and dive deeper into JavaScript-based machine learning with TensorFlow.js.


Thanks for reading! If you have any questions, feel free to reach out at [email protected], connect with me on LinkedIn, or follow me on Medium and Twitter.

Useful links

Sam’s Blog Google machine learning CrashCourse Tensorflow tutorials

Abbey Mphahlele

Technical Sales Rep at Chemetall

5 年

My email. [email protected]. Thank you

回复
Abbey Mphahlele

Technical Sales Rep at Chemetall

5 年

Mr Mphe sir. I really enjoyed this article. Do you mind email me the link to this ar

Shaun Moloi

Senior Data Scientist | Published Author | Retail & AI Specialist

6 年
回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了