NeuralMesh
  • Class
  • Tree

Classes

  • AbstractNetwork
  • Controller
  • layer
  • ManagedNetwork
  • Model
  • mysql
  • Navigation
  • network
  • NeuralMesh
  • neuron
  • nmesh
  • synapse
  • train
  • UnmanagedNetwork
  • users
  • validation
  1 <?php
  2 set_time_limit(0); //optional, but would be better than a timeout
  3 require("nm-admin/lib/controller.class.php");
  4 
  5 /**
  6  * Neural Mesh Factory pattern. Instance based on existing or new
  7  * @author Louis Stowasser
  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      * Get a nmesh instance from authkey
 23      * @param $authkey Unique key to network
 24      * @return NeuralMesh instance (singleton)
 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      * Create a new network and store it
 38      * @param $input
 39      * @param $output
 40      * @param $hidden
 41      * @param $layers
 42      * @param $bias
 43      * @param $weightrange
 44      * @return unknown_type
 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     /** Nmesh instance */
 59     private $nn;
 60     /** Learning rate */
 61     private $lr;
 62     /** Network ID */
 63     private $id;
 64     
 65     /**
 66      * Run the network or grab from the cache
 67      * @param $inputs String of inputs
 68      * @return array of outputs
 69      */
 70     abstract public function run($inputs);
 71     
 72     /**
 73      * Training function must comply with these
 74      */
 75     abstract public function train($inputs,$outputs,$epochs=30);
 76 } 
 77 
 78 /**
 79  * UnmanagedNetwork is a network that doesn't get viewed in the Manager
 80  * and is usually temporary
 81  * @author Louis Stowasser
 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      * Quickly train network. Don't log the epoch
 95      * @param $inputs String of inputs
 96      * @param $outputs String of desired outputs
 97      * @param $epochs Amount of times to train pattern
 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  * ManagedNetwork is a pre-existing network that is more volatile and logs history, gets cached etc
120  * @author Louis Stowasser
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      * Quickly train network
132      * @param $inputs String of inputs
133      * @param $outputs String of desired outputs
134      * @param $epochs Amount of times to train pattern
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) { //not found in cache
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             //save into cache
156             $hash = $id.$inputs;
157             NeuralMesh::$app->model->saveCache($hash,$id,implode("|",$outputs));
158         } else $outputs = explode("|",$data); //get from cache
159         return $outputs;
160     }
161 }
162 ?>
NeuralMesh API documentation generated by ApiGen