-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron.php
156 lines (142 loc) · 4.13 KB
/
Perceptron.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
<?php
class Perceptron
{
protected $learningRate;
protected $output;
protected $bias;
protected $vectorLength;
protected $weightVector;
protected $epoch = 0;
protected $satisfied = false;
protected $epochLimit = 1000;
/**
* @param int $vectorLength طول آرایه
* @param float $learningRate نرخ یادگیری
* @param int $bias w0
*/
function __construct($vectorLength, $learningRate = 0.5, $bias = 0)
{
//Validation
if ($vectorLength < 1) {
throw new InvalidArgumentException();
} elseif ($learningRate <= 0 || $learningRate > 1) {
throw new InvalidArgumentException();
}
$this->vectorLength = $vectorLength;
$this->bias = $bias;
$this->learningRate = $learningRate;
}
/**
* @param array $data An Big array of data Contains learning samples
*/
public function trainer($data)
{
//if weights is not set , do it random
if (empty($this->weightVector)) {
Self::setWeightRandom();
}
while (!$this->satisfied) {
foreach ($data as $trainSample) {
Self::train($trainSample[0], $trainSample[1]);
}
//test weights
$status = true;
foreach ($data as $trainSample) {
if (Self::test($trainSample[0]) != $trainSample[1]) {
$status = false;
}
}
if ($status == true) {
$this->satisfied = true;
}
$this->epoch++;
if ($this->epoch > 1000000) {
die('It Took too long , more than 1000000 Enoch!' . "\n");
}
}
echo 'Training is Done in: ' . $this->epoch . " Epoch \n";
echo 'Weights are : ' . "\n";
var_dump($this->weightVector);
echo("\n");
echo("and W0 is: ");
echo($this->bias . "\n");
}
/**
* @param array $input array of inputs
* @param int $result 1 = true / 0 = false
*/
public function train($input, $result)
{
//Validation
if (!is_array($input) || ($result != 0 && $result != 1)) {
throw new InvalidArgumentException();
}
// Test if current weights are Valid
// Do test on input
$output = Self::test($input);
if ($output != $result) {
//we need to change weights
for ($i = 0; $i < $this->vectorLength; $i++) {
$this->weightVector[$i] =
$this->weightVector[$i] + $this->learningRate * ((int)$result - (int)$output) * $input[$i];
}
}
$this->bias = $this->bias + ((int)$result - (int)$output);
}
/**
* @param $input Array
* @return bool
*/
public function test($input)
{
//Validation
if (!is_array($input) || count($input) != $this->vectorLength) {
throw new InvalidArgumentException();
}
$testResult = $this->dotVectors($this->weightVector, $input) + $this->bias;
return $testResult > 0 ? 1 : 0;
}
/**
* @param array $vector1
* @param array $vector2
*
* @return number
*/
private function dotVectors($vector1, $vector2)
{
$total = 0;
$dim = count($vector1);
for ($i = 0; $i < $dim; $i++) {
$total += $vector1[$i] * $vector2[$i];
}
return $total;
}
/**
* @param mixed $weightVector
*/
public function setWeight($weightVector)
{
//Validation
if (!is_array($weightVector) || count($weightVector) != $this->vectorLength) {
throw new \InvalidArgumentException();
}
$this->weightVector = $weightVector;
}
/**
*
*/
public function setWeightRandom()
{
//set Default weights to 0
for ($i = 0; $i < $this->vectorLength; $i++) {
$this->weightVector[$i] = rand(0, 10);
}
}
/**
* @param int $epochLimit
*/
public function setEpochLimit($epochLimit)
{
$this->epochLimit = $epochLimit;
}
}