AI Machine Learning Complete Course: For PHP & Python Devs
AI Machine Learning Complete Course: For PHP & Python Devs
https://www.udemy.com/ai-machine-learning-complete-
course/
2
Section 1
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
Rule-Based AI
a.k.a. Symbolic Reasoning Machine Learning
a.k.a. GOFAI
7
Logic & Rule Based AI
The Good Old-Fashioned AI
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
Section 2
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
AI (Artificial Intelligence)
Rule-Based
Machine Learning AI ...
Neural
SVM ...
Networks
15
Section 3
Let’s Code
The Fun Part
17
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.
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
Requirements:
1. PHP7.1+
2. Apache
3. Composer
22
Section 4
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
Word Counter
Section 5
exec("python
mypythonscript.py",$output);
var_dump($output);
Python REST API
34
Python SyntaxCrash 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";
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;
}
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
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)
Section 6
Random
Weight
L2 - Output
Random
Weight
Random
Weight
Output Calculation – Forward
52
1. Forward Propagation
Sigmoid
0 0 1 0
1 1 1 1
1 0 1 1
0 1 1 0
55
Section 7
Activation Functions
Sigmoid
60
New Concepts
There are More!
Input Dimension
61
New Concepts
There are More!
Learning Rate
64
New Concepts
There are More!
Model Loss
65
New Concepts
There are More!
Epoch
Batch
Sampl Sampl Sampl Sampl
Batc Batc Batc Batc ...
... e1 e2 e3 en
h1 h2 h3 hn
66
New ConceptsThere are More!
Dataset
Testing Part (0.2) Training Part (0.8)
Count
Validation Portion
Training Portion (0.9)
(0.1)
Section 8
High Accuracy
Lots of Data Language
Detection
71
Language DetectionFeature Extraction
Letter Counter
Downloaded from
https://dumps.wikimedia.org/
Total Dataset
Dataset Count per Count (we have
Testing Part (0.2) Training Part (0.8)
Language data for 5
languages)
Accuracy
98%
Section 9
TensorFlow Hub
77
Machine Learning is Fun
You can apply it everywhere!
https://alpha.tfhub.dev/
79
Bonus: Section 10
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!
S(1.235) = 0.7746924929149283
90
Building Neural Networks
Manually!
Target = 0
Calculated = 0.77
Output sum margin of error = Target – calculated = -
0.77
𝑑𝑠𝑢𝑚
𝑆 ′ 𝑠𝑢𝑚 =
𝑑𝑟𝑒𝑠𝑢𝑙𝑡
𝑑𝑠𝑢𝑚
× 𝑡𝑎𝑟𝑔𝑒𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 − 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 𝑟𝑒𝑠𝑢𝑙𝑡 = ∆𝑠𝑢𝑚
𝑑𝑟𝑒𝑠𝑢𝑙𝑡
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
linkedin.com/in/amrshawqy
Well Done!
twitter.com/amrshawqy
You have successfully started
your AI journey! Keep going!