"practical machine learning with ruby" by iqbal farabi (id ruby community)

61
Practical Machine Learning with Ruby Tech in Asia Conference Jakarta, 2016/11/16

Upload: tech-in-asia-id

Post on 07-Jan-2017

54 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Practical Machine Learning with Ruby

Tech in Asia ConferenceJakarta, 2016/11/16

Page 2: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

About Me• Iqbal Farabi• Cofounder of: • Virkea Empresa Sistema• Starqle Indonesia

• Member of ID Ruby community• Contact: [email protected]

Page 3: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

What to Expect

Basic concepts of Machine Learning

Introduction to Classification with Neural Network

Neural Network Implementation with Ruby

Page 4: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Let’s learn together!

Page 5: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Overview

Machine Learning

Mathematical Models• Simple Regression• Handwritten Recognition

Ruby Implementation• Simple Regression• Handwritten Recognition

Page 6: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Machine Learning

Page 7: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Machine LearningMotivations

Page 8: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Data Science Full Stack

Page 9: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Data Science Full Stack

Machine Learning

Page 10: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

SciRuby ManifestoRuby has for some time lacked libraries implementing the basic tools needed for scientific computing and data visualization.

We believe that the time for a Ruby science and visualization package has come. Sometimes when a solution of sugar and water becomes super-saturated, from it precipitates a pure, delicious, and diabetes-causing crystal of sweetness, induced by no more than the tap of a finger. So it is, we believe, with the need for numeric and visualization libraries in Ruby.

Page 11: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Machine LearningBasic Concepts

Page 12: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

DefinitionMachine learning is a type of artificial intelligence (AI) that provides computers with the ability to learn without being explicitly programmed.

Machine learning focuses on the development of computer programs that can teach themselves to grow and change when exposed to new data.

Page 13: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Types of Learning• Supervised Learning• Unsupervised Learning• Reinforcement Learning

Page 14: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Supervised LearningInput data is called training data and has a known label or result such as spam/not-spam or a stock price at a time.

A model is prepared through a training process where it is required to make predictions and is corrected when those predictions are wrong. The training process continues until the model achieves a desired level of accuracy on the training data.

Page 15: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Supervised Learningx y1 22 43 64 ?

Page 16: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Supervised LearningSize (ft) # of Bedrooms # of Floors Age of home Price ($1000)

1 2104 5 1 45 460

1 1416 3 2 40 232

1 1534 3 2 30 315

1 852 2 1 36 178

Page 17: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Mathematical ModelsSimple Regression

Page 18: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (1)x y1 22 43 64 ?

Page 19: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (2)

Page 20: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (3)

Page 21: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (4)

Page 22: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (5)

Page 23: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (6)

Page 24: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (7)

Page 25: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (8)

Page 26: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Regression (10)

Page 27: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Ruby ImplementationSimple Regression

Page 28: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Some Gems You’ll Need• iRuby - https://github.com/SciRuby/iruby• NMatrix - https://github.com/SciRuby/nmatrix• Nyaplot - https://github.com/domitry/nyaplot• Ruby GSL - https://github.com/SciRuby/rb-gsl

Page 29: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

How do We Translate this to Ruby?

Page 30: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
Page 31: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Matrix Representation

Page 32: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Math Matrix

Page 33: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Math Ruby

Page 34: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Gradient Descent

Page 36: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Mathematical ModelsNeural Network

Page 37: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron (1)

x1

x2

x3

output

Page 38: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron (2)• x1, x2, x3 single binary inputs• add “weight” for each input: w1, w2, w3

ouput0 if ≤ threshold

1 if > threshold

Page 39: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron Example (1)Should I go to TIA Conference?• x1 = can I take a leave in November 16th and 17th?• x2 = are my friends going to the conference?• x3 = is there any talk that interests me?

Add “weight” for each input, for instance: • w1 = 6 (means this is the most important factor in my decision) • w2 = 1 (means this is the least important factor in my decision)• w3 = 3 (means this is less important factor in my decision)

Page 40: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron Example (2)Decide threshold• Lower threshold means that I am more likely to go to TIA Conference• Higher threshold means that I am less likely to go to TIA Conference• Example: threshold = 3

Calculate perceptron, example:• output = (0 * 6) + (1 * 1) + (1 * 3) = 4• output > threshold I go to TIA Conference!

Page 41: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron Modified (1)

(vectorized)

Page 42: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron Modified (2)

ouput0 if ≤ threshold

1 if > thresholdouput

0 if w . x + b ≤ 0

1 if w . x + b > 0

b = bias = -threshold

Page 43: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron as Logic Function (1)Example: NAND Perceptron

x1 x2 Calculation Result

0 0 0*(-2) + 0*(-2) + 3 = 3 True

1 0 1*(-2) + 0*(-2) + 3 = 1 True

0 1 0*(-2) + 1*(-2) + 3 = 1 Tue

1 1 1*(-2) + 1*(-2) + 3 = -1 False

3

x1

x2

-2

-2

Page 44: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Perceptron as Logic Function (2)Example: Bitwise Copy

-2

-2-2

-2

-2

-2

-2-2

-4

3

3

3

3

3

source: Neural Networks and Deep Learning

Page 45: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Neural Network’s Goal• To devise learning algorithms which can automatically tune the

weights and biases of a network of artificial neurons. • This tuning happens in response to external stimuli, without direct

intervention by a programmer. • Enable the use of artificial neurons in a way which is radically different

to conventional logic gates• Learn to solve problems, sometimes where it would be extremely

difficult to directly design a conventional circuit

Page 46: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Ruby ImplementationHandwritten Recognition

Page 47: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Handwritten Recognition

Page 48: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Convert Each Image to 28x28 Pixels

Page 49: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Neural Network

Page 50: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Ruby ImplementantionInput Layer• x1 = greyscale value of pixel at (0, 0)• …• x784 = greyscale value of pixel at (27, 27)

Output Layer: • y1 = 0, y2 = 1, y3 = 2, y4 = 3, y5 = 4, y6 = 5, y7 = 6, y8 = 7, y9 = 8, y10 = 9

Neural Network’s goal: based on training data,• Find weight• Find bias

Page 51: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Cost Function• w = weight• b = bias• x = network input• y(x) = label for each input• a = network output

Page 52: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Gradient Descent• Given the cost function, try to minimize cost function

Page 53: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Gems You’ll Need• Chunky PNG (https://github.com/wvanbergen/chunky_png)• Nmatrix (https://github.com/tangledpath/ruby-fann)• Ruby FANN (https://github.com/tangledpath/ruby-fann)• Sinatra (https://github.com/sinatra/sinatra)

Page 54: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Training Neural Network (1)

Page 55: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Training Neural Network (3)

Page 56: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Training Neural Network (2)

Page 57: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Predict

Page 59: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

What’s Next?

Page 60: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

What’s Next?• Explore more SciRuby Libraries• Implement more learning algorithms in Ruby• Learn more Math XD• Join ID Ruby community! (http://tinyurl.com/id-ruby-slack)

Page 61: "Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)

Thank You!