dooooooduo

CI_ASS2_Single Neuron Regression_PHP

Jan 12th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. $inputs = [
  4.     [-0.74347,-0.15948],
  5.     [-0.49418,-0.072128],
  6.     [0.76792,0.37009],
  7.     [-0.60745,-0.11181],
  8.     [-0.75729,-0.16432],
  9.     [0.08739,0.13164],
  10.     [-0.37076,-0.028884],
  11.     [-0.23592,0.018362],
  12.     [0.58308,0.30532],
  13.     [0.67836,0.33871]
  14. ];
  15.  
  16. $sets = [
  17.     [0.36047,0.22733],
  18.     [-0.16616,0.042805],
  19.     [0.28578,0.20116],
  20.     [-0.57184,-0.099338],
  21.     [0.23454,0.1832],
  22. ];
  23.  
  24. $learning_rate = .001;
  25.  
  26. $weights = [
  27.     mt_rand(-.001 * 10000000, .001 * 10000000) / 10000000,
  28.     mt_rand(-.001 * 10000000, .001 * 10000000) / 10000000
  29. ];
  30. $bias = 1;
  31.  
  32. $learning_count = count($inputs);
  33.  
  34. echo '<pre>';
  35. $previous_error = 999;
  36. for ($i = 1; ; ++$i) {
  37.     $total_error = 0;
  38.     foreach ($inputs as $input) {
  39.         $out          = $input[0] * $weights[0] + $weights[1];
  40.         $total_error += pow($error = $input[1] - $out, 2);
  41.         $weights[0]  += $learning_rate * $input[0] * $error;
  42.         $weights[1]  += $learning_rate * $error;
  43.     }
  44.     $total_error = $total_error / $learning_count;
  45.     if ($total_error === $previous_error || $i > 500000) break;
  46.     $previous_error = $total_error;
  47. }
  48.  
  49. echo '<table>';
  50. echo '<tr><td colspan="3"><b>Solving</b></td></tr>';
  51. foreach ($sets as $set) {
  52.     $out = $set[0] * $weights[0] + $bias * $weights[1];
  53.     echo '<tr><td>' . $out . '</td><td>' . $set[1] . '</td><td>' . ($out === $set[1] ? '' : '×') . '</td></tr>';
  54. }
  55. echo '</table>';
Advertisement
Add Comment
Please, Sign In to add comment