This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclasses.php
235 lines (172 loc) · 4.36 KB
/
classes.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
class ArrayObj implements ArrayAccess, Iterator {
protected $array = array();
protected $position = 0;
public function __construct(array $array = []) {
$this->array = $array;
$this->position = 0;
}
public function suffle(){
shuffle($this->array);
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->array[$offset]);
}
public function offsetUnset($offset) {
unset($this->array[$offset]);
}
public function offsetGet($offset) {
return isset($this->array[$offset]) ? $this->array[$offset] : null;
}
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->array[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
++$this->position;
}
public function valid() {
return isset($this->array[$this->position]);
}
}
class Neuron {
public $threshold;
private $weights;
private $size;
public function __construct(int $size){
$this->size = $size;
$this->threshold = 0;
$this->weights = array_fill(0, $size, 0);
array_walk($this->weights, function(&$value, $index){
$value=rand(-1, 1)/10;
});
}
public function transfert($input){
$somme = 0;
if(count($input) === $this->size){
for ($i=0; $i < $this->size; $i++) {
$somme += intval($input[$i]) * $this->weights[$i];
}
}
return $somme > $this->threshold ? "1":"0";
}
public function getWeights(){
return $this->weights;
}
public function setWeights(array $newWeights){
$this->weights = $newWeights;
}
}
class Network extends ArrayObj {
public $neuronNumber;
public $neuronSize;
public function __construct($neuronNumber, $neuronSize){
$this->neuronNumber=$neuronNumber;
$this->neuronSize=$neuronSize;
for ($i=0; $i < $neuronNumber; $i++) {
$this->array[] = new Neuron($neuronSize);
}
}
public function answer($input){
$ret = "";
foreach ($this->array as $neuron) {
$ret .= strval($neuron->transfert($input));
}
return $ret;
}
public function print(){
foreach ($this->array as $neuron) {
print_r($neuron);
}
}
public function save($file){
file_put_contents($file, serialize($this));
}
static function load($file){
if (file_exists($file)) {
return unserialize(file_get_contents($file));
}
}
}
class Example {
public $target;
public $input;
public function __construct($target, $input){
$this->target = strval($target);
$this->input = $input;
}
}
class ExampleSet extends ArrayObj {
private $targetSize;
private $inputSize;
public function __construct(int $targetSize, int $inputSize){
$this->targetSize=$targetSize;
$this->inputSize=$inputSize;
}
public function offsetSet($offset, $example) {
if( $example instanceof Example
&& strlen($example->target) == $this->targetSize
&& count($example->input) == $this->inputSize){
return parent::offsetSet($offset, $example);
}
}
}
class Trainnig {
private $examples=[];
private $learningRate = 1;
private $rateStep=0.01;
private $nbSet;
private $deltaWeights=[];
public function __construct(ExampleSet $examples){
$this->examples=$examples;
}
public function run(Network $network, int $max_iterations){
for ($i=0; $i < $max_iterations; $i++) {
$this->examples->suffle();
$this->runWorkout($network);
}
}
private function runWorkout($network){
foreach ($network as $i => $neuron) {
$newWeights = $neuron->getWeights();
foreach ($this->examples as $example) {
$expected = $example->target[$i];
$answer = $neuron->transfert($example->input);
if($expected!==$answer) {
$delta = (intval($answer) - intval($expected))*$this->learningRate;
array_walk($newWeights, function(&$item, $key, $apply) {
$item -= $apply['input'][$key]*$apply['delta'];
},
['input'=>$example->input, 'delta'=>$delta]);
}
}
$neuron->setWeights($newWeights);
}
if($this->learningRate > $this->rateStep){
$this->learningRate-=$this->rateStep;
}
}
public function test($examples, $network){
foreach ($examples as $lettername => $input) {
$expected = strval($lettername);
$answer = $network->answer($input);
if($expected == $answer){
print("$expected : success\n");
} else {
print("$expected : fail\n");
}
}
}
}