3 //NMNN - Nervous Man's Neural Network
6 * My first neural network
10 $nmnn_version = '0.1-DEV';
13 ///FUNCTIONS///////////////////////////////////////////////////////////////////////////////////////
15 function nervous_banner($text) { //Print banner with custom text
16 echo("\n[*]-== Nervous Man's Neural Network ==-[*]\n");
17 $text = explode("\n", $text);
18 foreach($text as $line) {
21 echo("[+]-= $line\n");
23 echo("[*]-==================================-[*]\n\n");
26 function neural_network_create($net) { //Create new network in $GLOBALS['neural_networks'][$net]
27 if(isset($GLOBALS['neural_networks'][$net])) {
28 echo("Net already exists\n");
31 $GLOBALS['neural_networks'][$net] = new neural_network($net);
35 function neural_network($net) {
36 return($GLOBALS['neural_networks'][$net]);
39 function global_neuron($net, $id) {
40 return neural_network($net)->neurons
[$id];
43 ///CLASSES/////////////////////////////////////////////////////////////////////////////////////////
45 class neuron
{ //Single Neurone class
47 var $net = 'N/A'; //ID of network containing this neuron
48 var $id = 'N/A'; //ID of this neuron
49 var $treshold = 0; //Treshold is sensitivity of this neuron
50 var $synapses = array(); //List of neurons connected to dendrites (inputs)
51 var $decisions = array(); //List of values received from dendrites
52 var $axon = 'N/A'; //Final decision of this neuron
53 var $sensor = false; //Is this a sensor?
55 function neuron($net, $id, $treshold=0) { //Constructor - Set neuron identification
58 $this->treshold
= $treshold;
61 function synapse_add($id, $strength=0) { //Connect neuron to dendrite
62 $this->synapses
[$id] = $strength;
65 function synapse_request($id) { //Request synapse add by another neuron
67 global_neuron($this->net
, $id)->synapse_add($this->id
);
68 } else foreach($id as $id_single) {
69 global_neuron($this->net
, $id_single)->synapse_add($this->id
);
73 function synapse_break($id) { //Disconnect neuron from dendrite
74 unset($this->synapses
[$id]);
77 function debug() { //Print info about this neuron
78 echo("[i] Info about neuron no.$this->id in $this->net network:\n");
79 if($this->sensor
) { //if this is sensor print different stuff
80 echo("\tThis cell is a SENSOR!!!\n\tSensor axon value:$this->axon\n\n");
83 echo("\tTreshold: $this->treshold\n\tAxon: $this->axon\n");
84 echo("\tSynapses:\n");
85 print_r($this->synapses
);
86 echo("\tDecisions:\n");
87 print_r($this->decisions
);
91 function dendrites_read() { //Receive axon values from all neurons connected to dendrites
92 if($this->sensor
) return('s');
93 $this->decisions
= array(); //Remove old decisions
94 foreach($this->synapses
as $synapse => $strength) {
95 //print_r (global_neuron($this->net, $synapse)); //debug
96 global_neuron($this->net
, $synapse)->dendrites_read();
97 global_neuron($this->net
, $synapse)->decide();
98 $this->decisions
[$synapse]=global_neuron($this->net
, $synapse)->axon_get();
102 function decide() { //Decide final axon (output) value - read dendrites first!
103 if($this->sensor
) return('s');
104 $this->axon
= 0; //Reset old
105 foreach($this->decisions
as $synapse => $decision) {
106 $this->axon +
= $decision * $this->synapses
[$synapse];
108 if($this->axon
> $this->treshold
) {
115 function axon_get() {
119 function learn($truth) { //Determine, what is truth
120 if($this->sensor
) return('s');
121 foreach($this->decisions
as $synapse => $decision) {
122 if($decision == $truth) {
123 $this->synapses
[$synapse]++
;
125 $this->synapses
[$synapse]--;
130 function sensor_set($sensor_value, $is_sensor=true) { //Make sensor from this neuron and set axon value
131 $this->sensor
= $is_sensor;
132 $this->axon
= $sensor_value;
139 class neural_network
{ //Single neural network class
141 var $net = 'N/A'; //Name of this network
142 var $neurons = array(); //Array of neuron objects
144 function neural_network($net) { //Constructor - Set network name
148 function add_neuron($id, $treshold=0) { //Creates new neurone with specified id
149 if(!isset($this->neurons
[$id])) {
150 $this->neurons
[$id] = new neuron($this->net
, $id, $treshold);
154 function debug() { //Print debug for all neurons in network
155 echo("###########\nDebug information about $this->net network:\n###########\n\n");
156 foreach($this->neurons
as $neuron) $neuron->debug();
159 function learn($truth) { //Learn all neurons
160 foreach($this->neurons
as $neuron) $neuron->learn($truth);
166 nervous_banner("\nNeural network simulator\nCoded by Harvie 2oo7/8\nInfo:\n- Version: $nmnn_version\n- NeuroNet library loaded!\n");
169 8x ---------------------------------- CUT HERE ----------------------------------
172 ///SAMPLE_NETWORK///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
174 neural_network_create('neuronet'); //Create new network called neuronet
175 for($i=1;$i<=6;$i++
) neural_network('neuronet')->add_neuron($i,-1); //Create few neurons in neuronet
178 Now wee have six neurons floating somewhere in the network space:
180 (1) (2) (3) (4) (5) (6)
183 //Connect neurons into synapse infrastructure:
184 global_neuron('neuronet', 1)->synapse_request(array(3,4));
185 global_neuron('neuronet', 2)->synapse_request(array(4,5));
186 global_neuron('neuronet', 3)->synapse_request(6);
187 global_neuron('neuronet', 4)->synapse_request(6);
188 global_neuron('neuronet', 5)->synapse_request(6);
190 //Convert neurons 1 and 2 to sensors:
191 for($i=1;$i<=2;$i++
) global_neuron('neuronet', $i)->sensor_set(0, true);
195 After making this synapse infrastructure, we'll get somethinq like this:
199 Input \_(4)__\_(6> -> Output
203 ]1) and ]2) are sensors/receptors/...
206 Network structure inspired by: http://en.wikipedia.org/wiki/Image:Neural_network_example.png
208 If you can't understand this, i can try to say it easier: "It's something like your brain (but much more complicated)" ;D
209 Or try to read one of these:
210 - http://en.wikipedia.org/wiki/Neural_networks
211 - http://en.wikipedia.org/wiki/Artificial_neural_network
215 //Now, i have neural network and i will try to teach it something
219 I will try to teach this network to compare two booleans
220 Epected output values:
227 So. Let's try to write some function for this:
231 function same($first, $second) { //This function uses neural network to compare two bools
233 global_neuron('neuronet', 1)->sensor_set($first);
234 global_neuron('neuronet', 2)->sensor_set($second);
236 global_neuron('neuronet', 6)->dendrites_read();
237 global_neuron('neuronet', 6)->decide();
238 $decision = global_neuron('neuronet', 6)->axon_get();
240 echo("$first == $second ?\nnetwork says: $decision (0=NO, 1=YES)\n");
247 We got nice function (in fact it hardly sux, like whole this network...)
255 neural_network('neuronet')->learn('true'); //Correct answer (teach)
256 neural_network('neuronet')->learn('true'); //Correct answer (teach)
258 neural_network('neuronet')->learn('false');
259 neural_network('neuronet')->learn('false');
261 neural_network('neuronet')->learn('false');
262 neural_network('neuronet')->learn('false');
264 ///Using superinteligent lifeform ehm... 3 neurones brain ;P
266 echo("\nI hope it will work now: 8')\n\n");
267 same(0,0); //Ask (without teaching)
269 //Now, we like to print some stats
270 neural_network('neuronet')->debug(); //Print some info about neuronet and it's neurons
This page took 0.915424 seconds and 4 git commands to generate.