All pastes #128464 Raw Edit

Unnamed

public php v1 · immutable
#128464 ·published 2006-08-13 00:34 UTC
rendered paste body
<?php//ignore_user_abort(TRUE);set_time_limit(0);ini_set("precision",8);function get_real_value($inputs,$weights){	$addon = 0;	foreach($inputs as $key=>$input){		$addon += bcmul($inputs[$key],$weights[$key],8);	}	return 1 / (1 + exp(-1*$addon));}function train(&$neruons,&$weights,$input,$wout,$lr){	$errors = array();	$neruons[0] = $input;	$size = sizeof($neruons);	play_nn($neruons,$weights,$input);	foreach($neruons[$size-1] as $key=>$o){		$errors[$size-1][$key] = $o*(1 - $o)*($wout[$key] - $o);	}	for($b = $size-2;$b > 0;$b--){		foreach($neruons[$b] as $key1 => $o){			$add = 0;			foreach($neruons[$b+1] as $key2=>$blah){				$add += bcmul($weights[$b+1][$key2][$key1],$errors[$b+1][$key2],15);			}			$errors[$b][$key1] = $o*(1 - $o)*($add);		}	}	for($b = $size-1;$b > 0;$b--){		foreach($errors[$b] as $key=>$error){			foreach($weights[$b][$key] as $pkey=>$we){				$weights[$b][$key][$pkey] = $we + ($lr * $error * $neruons[$b-1][$pkey]);			}		}	}	play_nn($neruons,$weights,$input);}function play_nn(&$neruons,$weights,$input){	$neruons[0] = $input;	$size = sizeof($neruons);	for($b = 1;$b < $size;$b++){			$akeys = array_keys($neruons[$b]);		$asize = sizeof($akeys);		for($ac=0;$ac<$asize;$ac++){			$neruons[$b][$akeys[$ac]] = get_real_value($neruons[$b-1],$weights[$b][$akeys[$ac]]);		}	}}function make_nn(&$neruons,&$weights,$input,$layers){	$neruons[0] = $input;	$size = sizeof($layers);	for($b = 1;$b < $size;$b++){		for($i = 0; $i < $layers[$b]; $i++){			$neruons[$b][$i] = 1;			for($j = 0; $j < $layers[$b-1]; $j++){				$weights[$b][$i][$j] = rand(-5,5) / 100;			}		}	}	play_nn($neruons,$weights,$input);}?>Example:<?php$input[] = .6;$input[] = .2;$layer[0] = sizeof($input);$layer[1] = 5;$layer[2] = 2;//build the NN$neruons = array();$weights = array();make_nn($neruons,$weights,$input,$layer);//play a example of it, you can change your input, and $neruons returns a good copy of that.play_nn($neruons,$weights,$input);$output[] = .6;$output[] = .2;$input[] = .1;$input[] = .9;$lr = .5; //learning ratefor($i<=0;$i<=500;$i++){      train($neruons,$weights,$input,$output,$lr);  //trains the network to [.1,.9] -> [.6,.2]}print_r($neruons);?>