1 <?php
2 set_time_limit(0);
3 require("nm-admin/lib/controller.class.php");
4
5 6 7 8
9 class NeuralMesh {
10
11 static private $instance = null;
12 static public $app = null;
13
14 static private function init() {
15 if(self::$app === null) {
16 self::$app = new Controller;
17 self::$app->inc("nmesh");
18 }
19 }
20
21 22 23 24 25
26 static public function getNetwork($authkey) {
27 self::init();
28 $data = self::$app->model->network->getAuth($authkey);
29 $nn = self::$app->model->network->nn;
30 nmesh::$momentumrate = $data['momentumrate'];
31 return ($data['networkType'] == "managed") ?
32 new ManagedNetwork($data['networkID'],$data['learningrate'],$nn) :
33 new UnmanagedNetwork($data['networkID'],$data['learningrate'],$nn);
34 }
35
36 37 38 39 40 41 42 43 44 45
46 static public function createNetwork($input,$output,$hidden,$layers) {
47 self::init();
48 $nn = new nmesh($input,$output,$hidden,$layers);
49 $authkey = sha1(uniqid());
50 $id = self::$app->model->network->add("Temp",$authkey,"unmanaged");
51 self::$app->model->network->save($nn,$id);
52
53 return new UnmanagedNetwork($id,$authkey,1,$nn);
54 }
55 }
56
57 abstract class AbstractNetwork {
58
59 private $nn;
60
61 private $lr;
62
63 private $id;
64
65 66 67 68 69
70 abstract public function run($inputs);
71
72 73 74
75 abstract public function train($inputs,$outputs,$epochs=30);
76 }
77
78 79 80 81 82
83 class UnmanagedNetwork extends AbstractNetwork {
84
85 public $authkey;
86
87 public function UnmanagedNetwork($id,$authkey,$lr,$nn) {
88 $this->authkey = $authkey;
89 $this->id = $id;
90 $this->lr = $lr;
91 }
92
93 94 95 96 97 98
99 public function train($inputs,$outputs,$epochs=30) {
100 $id = $this->id;
101 $lr = $this->lr;
102
103 $data = $this->nn->quick_train($id,$epochs,$lr,$inputs,$outputs);
104 NeuralMesh::$app->model->network->save($this->nn,$id);
105 }
106
107 public function run($inputs) {
108 $inputarray = str_split($inputs);
109 if(count($inputarray) != $this->nn->inputs) die("Incorrect number of entries! Expected ".$this->nn->inputs." got ".count($inputarray));
110 return $this->nn->run($inputarray);
111 }
112
113 public function destory() {
114 NeuralMesh::$app->model->network->destory($this->id);
115 }
116 }
117
118 119 120 121
122 class ManagedNetwork extends AbstractNetwork {
123
124 public function ManagedNetwork($id,$lr,$nn) {
125 $this->lr = $lr;
126 $this->id = $id;
127 $this->nn = $nn;
128 }
129
130 131 132 133 134 135
136 public function train($inputs,$outputs,$epochs=30) {
137 $id = $this->id;
138 $lr = $this->lr;
139
140 $data = $this->nn->quick_train($id,$epochs,$lr,$inputs,$outputs);
141 NeuralMesh::$app->model->train->saveEpoch($id,$epochs,$data['startmse'],$data['endmse'],$data['time']);
142 NeuralMesh::$app->model->quick_cache($id,$inputs,nmesh::$cache);
143
144 NeuralMesh::$app->model->network->save($this->nn,$id);
145 }
146
147 public function run($inputs) {
148 $id = $this->id;
149 $data = NeuralMesh::$app->model->getCache($id.$inputs);
150
151 if($data === null) {
152 $inputarray = str_split($inputs);
153 if(count($inputarray) != $this->nn->inputs) die("Incorrect number of entries! Expected ".$this->nn->inputs." got ".count($inputarray));
154 $outputs = $this->nn->run($inputarray);
155
156 $hash = $id.$inputs;
157 NeuralMesh::$app->model->saveCache($hash,$id,implode("|",$outputs));
158 } else $outputs = explode("|",$data);
159 return $outputs;
160 }
161 }
162 ?>