-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathNeuralNetworkPerceptronClassifier.php
192 lines (169 loc) · 4.79 KB
/
NeuralNetworkPerceptronClassifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace NeuralNetworks\PerceptronClassifier;
/**
* This class implements a simple neural network with one hidden layer and one output neuron.
* The network uses the sigmoid activation function and performs binary classification.
* (https://cw.fel.cvut.cz/b211/courses/be5b33rpz/labs/07_perceptron/start)
*
* @author Michał Żarnecki https://github.com/rzarno
*/
class NeuralNetworkPerceptronClassifier
{
/**
* @param array $X
* @param array $Y
* @param int $iterations
* @param float $learningRate
* @return array
*/
public function trainModel(array $X, array $Y, int $iterations, float $learningRate): array
{
[$W, $b] = $this->initParams(count($X));
for ($i = 0; $i < $iterations; $i++) {
// Forward propagation
$A = $this->forwardPropagation($X, $W, $b);
// Compute cost
$cost = $this->computeCost($A, $Y);
// Backward propagation
[$dW, $db] = $this->backwardPropagation($A, $X, $Y);
// Update parameters
[$W, $b] = $this->updateParams($W, $b, $dW, $db, $learningRate);
if ($i % 100 == 0) {
echo "Iteration {$i} - Cost: {$cost}\n";
}
}
return [$W, $b];
}
/**
* @param array $X
* @param array $W
* @param float $b
* @return array
*/
public function predict(array $X, array $W, float $b): array
{
$A = $this->forwardPropagation($X, $W, $b);
return array_map(fn($a) => $a > 0.5 ? 1 : 0, $A);
}
/**
* Stage 1. Prepare dataset
* @return array[]
*/
public function generateTrainingSet(): array
{
$m = 50;
// Generate a 2 x m matrix with binary values (0 or 1)
$X = [];
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < $m; $j++) {
$X[$i][$j] = rand(0, 1);
}
}
// Compute Y: Logical AND condition (X[0] == 1 and X[1] == 0)
$Y = [];
for ($j = 0; $j < $m; $j++) {
$Y[$j] = ($X[0][$j] == 1 && $X[1][$j] == 0) ? 1 : 0;
}
return [$X, $Y];
}
/**
* Stage 2. Initialize model parameters
* @param int $n Number of features
* @return array [$W, $b] Weight and bias arrays
*/
private function initParams(int $n): array
{
$W = [];
for ($i = 0; $i < $n; $i++) {
$W[$i] = mt_rand() / mt_getrandmax(); // Small random values
}
$b = 0.0; // Bias initialized to zero
return [$W, $b];
}
/**
* Sigmoid Activation Function
* @param float $z
* @return float
*/
private function sigmoid(float $z): float
{
return 1 / (1 + exp(-$z));
}
/**
* Stage 3. Forward Propagation
* @param array $X
* @param array $W
* @param float $b
* @return array
*/
private function forwardPropagation(array $X, array $W, float $b): array
{
$Z = [];
for ($j = 0; $j < count($X[0]); $j++) {
$sum = $b;
for ($i = 0; $i < count($W); $i++) {
$sum += $W[$i] * $X[$i][$j];
}
$Z[$j] = $this->sigmoid($sum);
}
return $Z;
}
/**
* Stage 4. Compute Cost Function (Binary Cross-Entropy Loss)
* @param array $A
* @param array $Y
* @return float
*/
private function computeCost(array $A, array $Y): float
{
$m = count($Y);
$cost = 0.0;
for ($i = 0; $i < $m; $i++) {
$cost += -($Y[$i] * log($A[$i]) + (1 - $Y[$i]) * log(1 - $A[$i]));
}
return $cost / $m;
}
/**
* Stage 5. Backward Propagation
* @param array $A
* @param array $X
* @param array $Y
* @return array
*/
private function backwardPropagation(array $A, array $X, array $Y): array
{
$m = count($Y);
$dW = array_fill(0, count($X), 0.0);
$db = 0.0;
for ($j = 0; $j < $m; $j++) {
$dZ = $A[$j] - $Y[$j];
for ($i = 0; $i < count($X); $i++) {
$dW[$i] += $dZ * $X[$i][$j];
}
$db += $dZ;
}
// Average gradients
for ($i = 0; $i < count($dW); $i++) {
$dW[$i] /= $m;
}
$db /= $m;
return [$dW, $db];
}
/**
* STage 6. Update Parameters
* @param array $W
* @param float $b
* @param array $dW
* @param float $db
* @param float $learningRate
* @return array
*/
private function updateParams(array $W, float $b, array $dW, float $db, float $learningRate): array
{
for ($i = 0; $i < count($W); $i++) {
$W[$i] -= $learningRate * $dW[$i];
}
$b -= $learningRate * $db;
return [$W, $b];
}
}