Sei sulla pagina 1di 96

1

AI Machine Learning – The Complete


Course: for PHP & Python Developers

https://www.udemy.com/ai-machine-learning-complete-
course/
2

Section 1

Understanding AI & Machine Learning


3
Artificial Intelligence
It is Everywhere

Top Trending Technology

Used Everywhere

• Websites
• Mobile Apps
• Chat-bots
• Search Engines
• & Much More!
4
Artificial Intelligence
History

Founded as an
Academic
Discipline
1956

1943
Studied By
Researchers
5
What is Artificial Intelligence?
Definition

Any technology that makes


machines process or respond to
data in human like ways, so the
machine can simulate human
behavior such as learning, and
problem solving.
6
Artificial Intelligence Approaches
There are many approaches

There are many approaches to mimic human


intelligence, but the most common are

Rule-Based AI
a.k.a. Symbolic Reasoning Machine Learning
a.k.a. GOFAI
7
Logic & Rule Based AI
The Good Old-Fashioned AI

Set of rules, logic, like a nested “if .. Else .. “


statements

Example – Tic-tac-toe

1. If someone has a "threat" (that is, two in a row), take the remaining square. Otherwise,
2. if a move "forks" to create two threats at once, play that move. Otherwise,
3. take the center square if it is free. Otherwise,
4. if your opponent has played in a corner, take the opposite corner. Otherwise,
5. take an empty corner if one exists. Otherwise,
6. take any empty square.
8
Machine Learning
The dominant approach for AI today

A computer program finds patterns in data and


create its own rules. Basically Learns from
historical data

There are four types of machine learning algorithms:


supervised, semi-supervised, unsupervised and
reinforcement

Machine Learning can be done using many


algorithms
9

Section 2

Machine Learning Types & Algorithms


10
Machine Learning Types
Just as a reference

Semi-
Supervised Unsupervised Reinforcement
supervised
Learning Learning Learning
Learning

Classificatio
Clustering
n

Dimension
Regression
Reduction

Forecasting
11
Machine Learning Algorithms
Just as a reference

1. Naïve Bayes
2. K Means
3. Support Vector Machine
(SVM)
4. Linear Regression
5. Logistic Regression
6. Decision Trees
7. Random Forests Simple Example of a Neural Network
8. Nearest Neighbors
9. Neural Networks
12

What Algorithm Should I Use?


The Cheat Sheet
13
14
What to Conclude
Mind Map

AI (Artificial Intelligence)

Rule-Based
Machine Learning AI ...

Neural
SVM ...
Networks
15

Section 3

Coding Example: Dominant Color


Detector (Neural Networks Algorithm)
16

Let’s Code
The Fun Part 
17
Dominant Color Detector
Real world example for an AI Application in PHP

Each Machine Learning Program


Generally Consists of:
• Training
• Prediction

Our Neural Network


18
Dominant Color Detector
Real world example for an AI Application in PHP

For this example, we want to give the AI program the color code for any color, and it
gives us what is the dominant primary color in that color. Pretty cool.

For Example, the dominant color for this is blue


19
Dominant Color Detector - Planning Let’s plan our code

train predict
1. Build the network 1. Restore the trained network from
2. Read test data the file
3. Train the network 2. Pass a completely new input
4. Save the trained network to 3. Observe the network output
a file
20
Dominant Color Detector - Planning
The Network

R Output is
either “red”,
Three Inputs G “blue”, or
B “green”

Activatio Activatio
n n
function function
PReLU Sigmoid

2 Hidden Layers – 4 Neurons Each


21
PHP-ML
A wonderful Machine Learning Library

For this example, we will use a library called PHP-ML (https://github.com/php-ai/php-


ml), this library is a fresh approach to Machine Learning in PHP. Algorithms, Cross
Validation, Neural Network, Preprocessing, Feature Extraction and much more in that
library.

Requirements:
1. PHP7.1+
2. Apache
3. Composer
22

Code Lab – Dominant Color Detector


Fingers Crossed
23
Artificial Intelligence
Think of the Possibilities

YES! You have now done your


first complete AI Machine
Learning application, and using
Neural Networks!

You Can Do More!


24

Section 4

Coding Example: Language Detection


(Support Vector Machine Algorithm)
25
Language Detection
Real world example for an AI Application in PHP

Sample Data:

"sentence","language"
"Hello, do you know what time the movie is tonight?","english"
"Vérifiez la batterie, s'il vous plaît.","french"
CSV
"A che ora sara' la prossima raccolta?","italian"
26
Language Detection
Feature Extraction

Text, Image, Video, or Numerical


Voice Representatio
n
27
Language Detection
Feature Extraction

Brown Black Long Black White


Eyes Eyes Ears Fur Fur
Image 1 0 1 1 0
1
Image 1 0 0 0 1
2
Image 0 1 1 1 0
3
Image 0 1 0 0 1
4
28
Language Detection
Feature Extraction

Word Counter

Text Data Count Vectorizer TFIDF Transformer Numerical


Vectors

We will use Kernel SVM Algorithm


29

Code Lab – Language Detector


Fingers Crossed
30
Applications!
Tweeter Analysis
31

Section 5

Python & PHP


32
Python & PHP
What we can do!
33
Python & PHP
What we can do!

Python was build in a light core, and lots of packages

PHP is a powerful web development language

exec("python
mypythonscript.py",$output);
var_dump($output);
Python REST API
34
Python SyntaxCrash Course

Python files doesn’t start with anything special, just


write code directly “unlike php which requires <?php
at the beginning or something similar”. And the
Python files have the extension .py

Python files cannot run from the browser directly, it


needs a web framework, we are not going to
explain this for now, but the way you can use
Python easily “and the most common” is from
command line like this “python mypythonscript.py”
35
Python SyntaxCrash Course

You can download and install Python from the official


Python website (https://www.python.org/downloads/)
and the install is very easy, after that you can just go
to command line navigate to your Python file and run
it with the “python” command. More info on
installation from here
(https://realpython.com/installing-python/)
36
Python Syntax
Crash Course

No semi column at the end of the statement, it is


optional, but strongly recommended not to use it

New line indicates a new statement

For comments Python uses “#” at the beginning of


each line, it doesn’t have multi-line comments
37
Python SyntaxCrash Course

Some basic Python data types are Numbers, String, Tuple


“like array but immutable”, List, Dictionary. The first three
are immutable. List and Dictionary are mutable. List is
ordered, but Dictionary is not

In PHP you include files with “include, require, include_once,


require_once”, in Python you can include using “import,
__import__(), and importlib.import_module()” (for example
“import sqlite3”, no paths here, once you install the module,
you can import it like that)

Whereas PHP uses NULL, Python uses None which can be


checked by “if foo is None”
38
Python Syntax
Crash Course

Print Statement
a. In PHP
print("Hello World");

b. In Python
print("Hello World")
39
Python SyntaxCrash Course

Echo Statement
a. In PHP
echo "Hello", " World";

b. In Python (no echo in Python)


print("Hello", "World")
40
Python Syntax Crash Course

Switch Statement
a. In PHP
$animal = "duck";
switch ( $animal ) {
case "duck": echo "two legs"; break;
case "cow": echo "four legs"; break;
default:
echo "don't know";
}

b. In Python (there is no switch statement in Python, but you can do it with if .. elif .. else, elif here = elseif in PHP) (Adding a column at
the end of condition statements or for loops, examine below)
animal = "duck"
if animal == "duck":
print("two legs")
elif animal == "cow":
print("four legs")
else:
print("don't know")

Instead of “switch” in Python you can also use dictionaries as follows, much easier:
animal = "duck"
legs = { "duck": "two legs", "cow": "four legs" }
if animal in legs:
print(legs[animal])
else:
print("don't know")
41
Python Syntax Crash Course

Multi-line IF
a. In PHP
$x = 1;
if ($x == 1) {
$a = 1;
$b = 2;
$c = 3;
}

b. In Python (no { or }, just an indentation would indicate the if scoop, indentation


and spacing is very important in Python)
if x == 1:
a = 1
b = 2
c = 3
42
Python Syntax
Crash Course

Conditional Expressions
a. In PHP
$a = 7;
$b = 10;
echo ($a < $b) ? "a is less": "not";

b. In Python
a = 7
b = 10
print ("a is less" if a < b else "not")
43
Python Syntax Crash Course

For Loops
a. In PHP
for ( $i = 0; $i < 10; $i++ ) {
echo $i;
}

b. In Python (range provides the condition out of the box) (the second parameter of
the print here means it will not print end of line after the printable because it
does by default)
for i in range(0,10):
print(i, end="")
44
Python Syntax Crash Course

While Loops
a. In PHP
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}

b. In Python (There is no ++ operator in Python, instead it has +=)


i = 0
while ( i < 10 ):
print(i, end="")
i += 1
45
Python Syntax
Crash Course

Loop Through List


a. In PHP
$myList = array("duck", "cow", "sheep");
foreach ($myList as $animal) {
echo($animal);
}

b. In Python
myList = ["duck", "cow", "sheep"]
for animal in myList:
print(animal)
46
Python Syntax Crash Course

Functions
a. In PHP
function main() {
sayHello("John");
}

function sayHello($person) {
echo ("Hello " . $person);
}

main();

b. In Python (it is easy to define functions) (In Python, you have an extra check, the __name__ will be = to “__main__” only if the file
itself was executed and not imported from another file, so you can have more control on when to run the main function or not)
def main():
sayHello("John")

def sayHello(person):
print("Hello", person)

if __name__ == "__main__":
main()
47
Python Syntax Crash Course

Classes
a. In PHP
class Person {
var $name;

function __construct($name) {
$this->name = $name;
}

function sayHi() {
echo('Hello, my name is ' . $this->name);
}
}

$p = new Person('John');
$p->sayHi();

b. In Python (self is like $this, but you need to pass it in every function)
class Person:
def __init__(self, name):
self.name = name

def sayHi(self):
print('Hello, my name is', self.name)

# this is outside the class, notice indentation changed.


p = Person('John')
p.sayHi()
48
Python & PHP
Well Done!
49

Section 6

Coding Example: Simple Neural


Network (Built from scratch without
libraries!)
50
Simple Neural Network - Python
Training Machine Learning Example
51
Simple Neural Network - Training
Training Algorithm

L1 - Input 1. Forward Propagation


2. Back Propagation

Random
Weight

L2 - Output

Random
Weight

Random
Weight
Output Calculation – Forward
52

Let’s do the Math

1. Forward Propagation

2. Normalize Output (Between 0 and 1)

Sigmoid

3. Together, the output becomes


Weight Adjustment – Backward
53

Let’s do the Math

1. Calculate Error (output is predicated output by the


network)
𝑒𝑟𝑟𝑜𝑟 = 𝑜𝑢𝑡𝑝𝑢𝑡 𝑓𝑟𝑜𝑚 𝑡𝑟𝑎𝑖𝑛𝑖𝑛𝑔 − 𝑜𝑢𝑡𝑝𝑢𝑡
2. Backward
Propagation
54
numpy
Python’s Math

• exp— the natural exponential


• array — creates a matrix
• dot — multiplies matrices
• random — gives us random numbers

training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])


training_set_outputs = array([[0, 1, 1, 0]]).T

0 0 1 0
1 1 1 1
1 0 1 1
0 1 1 0
55

Code Lab – Simple Python Neural Network


Fingers Crossed
56
Well Done!
Think of the Possibilities

And we have a neural network now done with Python! A simple


one!

What about this one?


57
XOR Assignment
For you to workout!
58

Section 7

Important Neural Networks Concepts


59
New Concepts
There are More!

Activation Functions

Sigmoid
60
New Concepts
There are More!

Input Dimension
61
New Concepts
There are More!

Dense Layer & Dropout Layer


62
New Concepts
There are More!
63
New Concepts
There are More!

Learning Rate
64
New Concepts
There are More!

Model Loss
65
New Concepts
There are More!

Epoch & Batch Size

Epoch
Batch
Sampl Sampl Sampl Sampl
Batc Batc Batc Batc ...
... e1 e2 e3 en
h1 h2 h3 hn
66
New ConceptsThere are More!

Training Data vs Validation Data vs Test Data

for each epoch


for each training data instance
- propagate error through the network
- adjust the weights
- calculate the accuracy over training data

for each validation data instance


calculate the accuracy over the validation
data

If the accuracy over the training data set


increases, but the accuracy over the validation
data set stays the same or decreases
exit training
else
continue training
67
New Concepts There are More!

Training Data vs Validation Data vs Test


Data

For example, if we have 1000 data samples

Dataset
Testing Part (0.2) Training Part (0.8)
Count

1,000 = 1,000 * 0.2 = 200 = 1,000 * 0.8 = 800

Validation Portion
Training Portion (0.9)
(0.1)

= 800 * 0.1 = 80 = 800 * 0.9 = 720


68

Section 8

Coding Example: Language Detection


with Google's TensorFlow (Real-world
example)
69
TensorFlow
Let’s go further!
70
Language Detection - TensorFlow
A real world Deep Neural Networks Example

Deep Neural Network

High Accuracy
Lots of Data Language
Detection
71
Language DetectionFeature Extraction

Letter Counter

Standard Scaler Numerical


Text Data Count Vectorizer
Transformation Vectors

We will identify: English, French, Spanish, Italian, German, Slovak, Czech


72
The Data
Thanks Wikipedia!

Downloaded from
https://dumps.wikimedia.org/

Then Extracted to txt using


http://medialab.di.unipi.it/wiki/Wikipedia_Extractor

I generated 7 files 1 for each language, 204 MB each!!

I gave you 5 files only


73

Code Lab – TensorFlow Language Detection


Fingers Crossed
74
Data Counts To Clarify

Training Data vs Validation Data vs Test


Data

Total Dataset
Dataset Count per Count (we have
Testing Part (0.2) Training Part (0.8)
Language data for 5
languages)

= 5 * 250,000 = = 1,250,000 * 0.2 =


250,000 = 1,250,000 * 0.8 = 1,000,000
1,250,000 250,000
Validation Portion
Training Portion (0.9)
(0.1)
= 1,000,000 * 0.1 = = 1,000,000 * 0.9 =
100,000 900,000
75
AWESOME!
We did it, again!

Accuracy
98%

Machine learning train part is from lines 315-330


(just 11 lines of code!) and the predict code is from
396-406 (10 lines of code!) the remaining code is
for:

Data cleaning (custom function)


Text vectorization (custom function)
Text Transformation (from library)
Evaluation, reporting, and plotting
76

Section 9

TensorFlow Hub
77
Machine Learning is Fun
You can apply it everywhere!

1. Get training data for your problem.


2. Convert those data into numerical vectors.
3. Train the network.
4. Predict!
78
TensorFlow Hub
Don’t Reinvent the Wheel

https://alpha.tfhub.dev/
79

Code Lab – TensorFlow Hub Examples


Fingers Crossed
80

Bonus: Section 10

Bonus Section: Building a


Neural Network using Plain
Math
81
Building Neural Networks
Manually!
82
Building Neural NetworksManually!

We will walk through a simple example of training a


neural network to function as an “Exclusive OR”
(“XOR”) operation to illustrate each step in the
training process.

input | output
-------------------
0, 0 | 0
0, 1 | 1
1, 0 | 1
1, 1 | 0
83
Building Neural Networks
Manually!
84
Building Neural Networks
Manually!
85
Building Neural Networks
Manually!

1 * 0.8 + 1 * 0.2 = 1
1 * 0.4 + 1 * 0.9 = 1.3
1 * 0.3 + 1 * 0.5 = 0.8
86
Building Neural Networks
Manually!
87
Building Neural Networks
Manually!
88
Building Neural Networks
Manually!

S(1.0) = 0.73105857863
S(1.3) = 0.78583498304
S(0.8) = 0.68997448112
89
Building Neural Networks
Manually!

0.73 * 0.3 + 0.79 * 0.5 + 0.69 * 0.9 = 1.235

S(1.235) = 0.7746924929149283
90
Building Neural Networks
Manually!

Output sum margin of error = target – calculated

Target = 0
Calculated = 0.77
Output sum margin of error = Target – calculated = -
0.77
𝑑𝑠𝑢𝑚
𝑆 ′ 𝑠𝑢𝑚 =
𝑑𝑟𝑒𝑠𝑢𝑙𝑡

𝑑𝑠𝑢𝑚
× 𝑡𝑎𝑟𝑔𝑒𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 − 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 𝑟𝑒𝑠𝑢𝑙𝑡 = ∆𝑠𝑢𝑚
𝑑𝑟𝑒𝑠𝑢𝑙𝑡

Delta output sum = S'(sum) * (output sum margin of error)


Delta output sum = S'(1.235) * (-0.77)
Delta output sum = -0.13439890643886018
91
Building Neural Networks
Manually!
92
Building Neural Networks
Manually!

hidden result 1 = 0.73105857863


hidden result 2 = 0.78583498304
𝐻𝑟𝑒𝑠𝑢𝑙𝑡 × 𝑊ℎ→𝑜 = 𝑂𝑠𝑢𝑚 hidden result 3 = 0.68997448112

Delta weights = delta output sum / hidden layer results


𝑑𝑂𝑠𝑢𝑚 Delta weights = -0.1344 / [0.73105, 0.78583, 0.69997]
= 𝐻𝑟𝑒𝑠𝑢𝑙𝑡𝑠
𝑑𝑤ℎ→𝑜 Delta weights = [-0.1838, -0.1710, -0.1920]

𝑑𝑂𝑠𝑢𝑚 old w7 = 0.3


𝑑𝑤ℎ→𝑜 = old w8 = 0.5
𝐻𝑟𝑒𝑠𝑢𝑙𝑡𝑠
old w9 = 0.9

new w7 = 0.1162
new w8 = 0.329
new w9 = 0.708
93
Building Neural Networks Manually!

𝐻𝑟𝑒𝑠𝑢𝑙𝑡 × 𝑊ℎ→𝑜 = 𝑂𝑠𝑢𝑚

𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡 1
=
𝑑𝑂𝑠𝑢𝑚 𝑤ℎ→𝑜 Delta hidden sum
= delta output sum / hidden-to-outer weights * S'(hidden sum
𝑑𝑂𝑠𝑢𝑚 Delta hidden sum
𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡 =
𝑤ℎ→𝑜 = -0.1344 / [0.3, 0.5, 0.9] * S'([1, 1.3, 0.8])
Delta hidden sum
𝑑𝐻𝑠𝑢𝑚 = [-0.448, -0.2688, -0.1493] * [0.1966, 0.1683, 0.2139]
𝑆 ′ 𝐻𝑠𝑢𝑚 = Delta hidden sum
𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡
= [-0.088, -0.0452, -0.0319]

𝑑𝐻𝑠𝑢𝑚 𝑑𝑂𝑠𝑢𝑚 𝑑𝐻𝑠𝑢𝑚


𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡 × = ×
𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡 𝑤ℎ→𝑜 𝑑𝐻𝑟𝑒𝑠𝑢𝑙𝑡

𝑑𝑂𝑠𝑢𝑚
𝑑𝐻𝑠𝑢𝑚 = × 𝑆′(𝐻𝑠𝑢𝑚 )
𝑤ℎ→𝑜
94
Building Neural Networks
Manually!

input 1 = 1
input 2 = 1
𝐼 × 𝑤𝑖→ℎ = 𝐻𝑠𝑢𝑚 Delta weights = delta hidden sum / input data
Delta weights = [-0.088, -0.0452, -0.0319] / [1, 1]
𝑑𝐻𝑠𝑢𝑚 Delta weights = [-0.088, -0.0452, -0.0319, -0.088, -0.0452, -0.0319]
=𝐼
𝑑𝑤𝑖→ℎ
old w1 = 0.8 new w1 = 0.712
old w2 = 0.4 new w2 = 0.3548
𝑑𝐻𝑠𝑢𝑚
𝑑𝑤𝑖→ℎ = old w3 = 0.3 new w3 = 0.2681
𝐼 old w4 = 0.2 new w4 = 0.112
old w5 = 0.9 new w5 = 0.8548
old w6 = 0.5 new w6 = 0.4681
95
Building Neural Networks
Manually!

old new
--------------------------
w1: 0.8 w1: 0.712
w2: 0.4 w2: 0.3548
w3: 0.3 w3: 0.2681
w4: 0.2 w4: 0.112
w5: 0.9 w5: 0.8548
w6: 0.5 w6: 0.4681
w7: 0.3 w7: 0.1162
w8: 0.5 w8: 0.329
w9: 0.9 w9: 0.708
96

I would love to help


Keep in Touch

linkedin.com/in/amrshawqy
Well Done!
twitter.com/amrshawqy
You have successfully started
your AI journey! Keep going!

Potrebbero piacerti anche