Professional Documents
Culture Documents
Białystok / Poland
CTO VP of Engineering
Presentation goals
1. To show how neural network work at the lowest
possible level.
2. To demystify neural networks, show how simple it is.
3. To show that you can train neural networks in pure PHP.
What is a neural
network ? (Artificial) Neural network is a
simplified brain model.
You have one in your head -
Natural Neural Network ;)
It consists of a number of neurons
that process information.
Aka:
● artificial neural network
(ANN)
● neural network (NN)
Input layer Hidden layer Output layer
X1
X2
Output 1
X3
Output 2
X4
X5
Output m
Xn
Who knows how neural
networks work in detail?
What is a
A system that processes the values
neuron ? of input signals into a single output
value:
X1
w1
● x - input signals
w2 ● w - input weights
X2
output
∑ f(∑)
● ∑ - weighted sum (x,w)
● Bias - threshold value
(additional weight)
wn ● f(∑) - activation function
Xn
Bias
the 70’s
In 1969 Minsky and Papert in their research paper proved limitations of single
layer neural networks
0
4
5
9
2
7
1
Input data (1)
Input data - simplified MNIST (2)
00000000000000110000000000000000
00000000000000111100000000000000
00000000000001111110000000000000
00000000000011111111000000000000
Grayscale values transformed to black (1) and white (0)
00000000000111111100000000000000
00000000000111111110000000000000
00000000000111111110000000000000
00000000011111110000000000000000
00000000001111110000000000000000
00000000011111100000000000000000
00000000111111100000000000000000
<?php
00000000111111000000000000000000
00000000111110000000000000000000
00000000111111000000000000000000
00000000111110000000000000000000
$input = array();
00000000111110011111110000000000
00000000111111111111111000000000
00000000111111111111111100000000
00000000111111111111111111000000
//read characters line by line and add to $input
00000000111111111101111111000000
00000000111111000000000011110000
for each pixel
00000000111110000000000011110000
00000000111110000000000011110000
$input[] = $pixel;
00000000011111000000000001111000
00000000011111000000000001111000
00000000001111100000000001111100
00000000001111110000001111111000
00000000001111110000111111111000
00000000000011111111111111110000
print count($input);
00000000000001111111111111100000
00000000000000111111111111100000
00000000000000001111111100000000
//Result is 28x28 = 784
0 0
One hot encoding
1 0
2 0
<?php
3 0
//for the number 6 it’s a 10 elements array
with 1 on the sixth index 4 0
1,0,0,0);
$output = array(0,0,0,0,0,0, 5 0
6 1
7 0
8 0
9 0
0 1 2 3 4 5 6 7 8 9
0 1 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0
2 0 0 1 0 0 0 0 0 0 0
3 0 0 0 1 0 0 0 0 0 0
4 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0
Output data (2)
6 0 0 0 0 0 0 1 0 0 0
7 0 0 0 0 0 0 0 1 0 0
8 0 0 0 0 0 0 0 0 1 0
9 0 0 0 0 0 0 0 0 0 1
One hot encoding
Pixels of digit 6 The hidden layer Output 1
0
Dragons ?
X1 1 0 Output 2
? Output 3
X2 0
0
? Output 4
0
X3
0
0 Output 5
?
X4
0
1 Output 6
?
X5
0 0 Output 7
Output 8
0
?
X784 1 Output 9
0
0 Output 10
LINEAR ALGEBRA
Scalar / Vector / Matrix
Matrix in PHP - 2 dim array
$a = array(array());
Columns Rows
Matrix addition <?php
+
$matrix->array[$i][$j];
}
}
return new Matrix($result);
}
Matrix
subtraction <?php
-
$matrix->array[$i][$j];
}
Same as addition but }
return new Matrix($result);
with the minus sign :) }
Multiplying
matrix by a <?php
*
$val;
}
}
return new Matrix($result);
}
Element-wise
matrix <?php
*
$matrix->array[$i][$j];
}
}
return new Matrix($result);
}
Proper matrix <?php
*
$matrix->array[$h][$j];
}
$result[$i][$j] = $w;
1*7 + 2*9 + 3*11 = 7+18+33 = 58 $w=0;
}
}
return new Matrix($result);
}
Transposed
matrix <?php
Constructor
$m = new Matrix(array(array()));
X1 Activation function
w1
w2
X2
output
∑ f(∑)
wn
Xn
Bias
Sigmoid
function
<?php
$__SIGMOID = function($x)
{
return 1/(1+exp(-$x));
};
Input layer
Hidden layer Output layer
$X = new Matrix(1,784);
X2
Output 1
X3
Output 2
X4
X5
Output 10
X1
w11
SIGMOID
SIGMOID
W2
w12
X2
H Y
X784
1 $X = new Matrix([$input]); B1 B2
2 $H = $X->dot($W1)->add($B1)->applyFunction($__SIGMOID);
3 $Y = $H->dot($W2)->add($B2)->applyFunction($__SIGMOID);
Network training
that input data (vector of pixels of digit 6) after propagating through the network
gives the output vector:
0 0 0 0 0 0 0 0 0
1
Finding values of the hyperparameters
b. NO – go back to point 2
6. The end
Calculating the network error (1)
Warning !
Faint-hearted folks are asked to
close their eyes for the next 5
slides.
DEAMONS FROM THE DEEPEST
BOTTOM OF HELL
DERIVATIVES
Calculating network error (1) - derivatives
Calculating network error (2) - derivatives
1 $dJdB2 =
$Y->
sub($Y2)->
multiply($H->dot($W2)->add($B2)->applyFunction($__SIGMOIDPRIME));
2 $dJdB1 =
$dJdB2->
dot($W2->transpose())->
multiply($X->dot($W1)->add($B1)->applyFunction($__SIGMOIDPRIME));
3 $dJdW2 = $H->transpose()->dot($dJdB2);
4 $dJdW1 = $X->transpose()->dot($dJdB1);
Calculating network error (3) - weights/biases
correction
Calculating network error (4) - weights/biases
correction
1 $W1 = $W1->sub($dJdW1->multiply($learningRate));
2 $W2 = $W2->sub($dJdW2->multiply($learningRate));
3 $B1 = $B1->sub($dJdB1->multiply($learningRate));
4 $B2 = $B2->sub($dJdB2->multiply($learningRate));
Network training - summary
● essential source code contains only 8 lines
● it’s all about correcting weights and biases
● it’s all based on matrix operations !
function learn($expectedOutput)
$Y2 = new Matrix([$expectedOutput]); // expected network output
1. $dJdB2 = $Y->sub($Y2)->multiply($H->dot($W2)->add($B2)->applyFunction($__SIGMOIDPRIME));
2. $dJdB1 = $dJdB2->dot($W2->transpose())->multiply($X->dot($W1)->add($B1)->applyFunction($__SIGMOIDPRIME));
3. $dJdW2 = $H->transpose()->dot($dJdB2);
4. $dJdW1 = $X->transpose()->dot($dJdB1);
5. $W1 = $W1->sub($dJdW1->multiply($learningRate));
6. $W2 = $W2->sub($dJdW2->multiply($learningRate));
7. $B1 = $B1->sub($dJdB1->multiply($learningRate));
8. $B2 = $B2->sub($dJdB2->multiply($learningRate));
Training network in PHP !
Our hyperparameters are already trained: weights $W1, $W2, and biases $B1, $B2
Total: 356,27 s
Total execution time
Number epochs 30
Keras - open source, high level neural networks Python library. Designed for easy
and fast experimentation .
TensorFlow - open source, low level library for numerical computations based on
data flow graphs.
1. ‘pure’ PHP
2. C extension
3. OpenBLAS (http://www.openblas.net/)
4. cuBLAS - (BLAS by NVIDIA CUDA)
Performance tests - dot product of squared mat.
PHPCUDA? / PHPOpenBLAS ?
https://github.com/dnishiyama85/PHPMatrix
No BLAS/CUDA support
Image classification
Object detection
Object detection and attribution
Style transfer
=
+
Image completion
Reminder of the presentation
goals
Who understand now how NN work?