Introduction To Machine
Learning In Node.js
Tamar Tena-Stern
Slide 2
Slide 2 text
Tamar Twena-Stern
• Software Engineer - manager and
architect
• Architect @PaloAltoNetworks
• Was a CTO of my own startup
• Passionate about Node.js !
• Twitter: @SternTwena
Slide 3
Slide 3 text
Tamar Twena-Stern
• Just Finished My Maternity Leave
• Have 3 kids
• Loves to play my violin
• Javascript Israel community leader
Slide 4
Slide 4 text
What We Can Do With Machine
Learning ?
Slide 5
Slide 5 text
What is Machine Learning
• Give training data to your model
• Your model implement an algorithm
• The model study
• Patterns on your data
• Inference on your data
• Use those patterns to perform a task with no instructions
Slide 6
Slide 6 text
Some Machine Learning Applications
Slide 7
Slide 7 text
Machine Learning
Fundamentals
Slide 8
Slide 8 text
How To Build Machine Learning Model
• Define - What type of model will it be
• Fit - Capture patterns from provided data
• Predict - Just what it sounds like
• Evaluate - Determine how accurate the model's predictions are.
Slide 9
Slide 9 text
An Example For A DataSet
Slide 10
Slide 10 text
Step 1 - Choosing A Model To Train
• Model #1 - Decision Tree Model
• Easier to understand
• The basic for more complicated Machine Learning Algorithms
• Lets assume we predict house prices
Slide 11
Slide 11 text
Lets Predict House Prices With Simple Decision
Tree
Slide 12
Slide 12 text
More Sophisticated Decision Tree
Slide 13
Slide 13 text
Step 2 - Lets Fit And Predict The Data
• Select several columns from the dataset as variable X
• Select one column of the data set that we want to predict
• Called Prediction Target - Marked as Y
• Predict patterns and according to them give results on new data
Slide 14
Slide 14 text
Node.js Decision Tree Demo
Slide 15
Slide 15 text
Step 3 - Evaluate How Accurate Are The
Predictions Of Your Model
Error = abs(actual−predicted)
Number Of Testing Subjects
TensorFlow.js
• Open-source
• Training and deploying machine learning models.
• Main operations relates to vector and matrices
• Basic unit - Tensor
Slide 19
Slide 19 text
Demo - Image Classification
With TensorFlow
Slide 20
Slide 20 text
Brain.js
Slide 21
Slide 21 text
Brain.js In A Nutshell
• GPU accelerated library of Neural Networks
• Fast and easy to learn
• You do not have to know neural networks in order to work with it
Slide 22
Slide 22 text
Neural Networks In A Nutshell
• Set of Algorithms
• Modelled loosely after the human brain
• Designed to recognise patterns
Slide 23
Slide 23 text
What Is A Neuron
Slide 24
Slide 24 text
Basic Unit Of Neural Networks - Artificial
Neurons
Slide 25
Slide 25 text
How The Network Learns ?
Blue
Blue
Yellow
Slide 26
Slide 26 text
Brain.js Demo
Slide 27
Slide 27 text
Did you notice that the algorithm was CPU intensive ?
Slide 28
Slide 28 text
Is JavaScript The Go-To
Programming Language For
Machine Learning ?
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
Main Problems
• Machine Learning algorithms are CPU intensive
• Matrix manipulations can be more efficient
• Not much richness of libraries
Slide 31
Slide 31 text
Lets Deep Dive Into The
CPU Intensive Problem
Slide 32
Slide 32 text
Node.js Architecture
Slide 33
Slide 33 text
Why CPU Intensive Operations Don’t Shine In
Node
• Non Blocking IO + Event Loop
• Constant amour of threads
• CPU Intensive operation will block the event loop
• CPU intensive operation that will be off loaded to the worker thread
pool will make one of the threads busy for long
Slide 34
Slide 34 text
Demo - CPU Intensive
Slide 35
Slide 35 text
CPU Intensive Tasks In Node.js
• Fork Another Process
• Worker Threads
• Scale Up with Queues + pack our application with
containers
Slide 36
Slide 36 text
Child Process
Slide 37
Slide 37 text
Prefer Not To Use Child Process
• Server running on specific hardware
• Bounded amount of CPU
• Usually , each process will be mapped to a CPU
• Multiple requests to your server - many processes
• Will not scale
Slide 38
Slide 38 text
Worker Threads
• Available since 10.5.0
• Enable the use of threads to execute JavaScript in Parallel
• One process, Multiple threads, one event loop per thread, One JS
engine per thread, one Node.js instance per thread
Slide 39
Slide 39 text
Demo - Brain.js With Worker Threads
Slide 40
Slide 40 text
Scale Up With Queues
Slide 41
Slide 41 text
Queues VS Worker Threads
• Queues Are Better for scale up
• Worker threads limit you to the current machine’s resources
• Can use Multiple CPUs from multiple machines while scaling up