Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $inputs = [
- [-0.74347,-0.15948],
- [-0.49418,-0.072128],
- [0.76792,0.37009],
- [-0.60745,-0.11181],
- [-0.75729,-0.16432],
- [0.08739,0.13164],
- [-0.37076,-0.028884],
- [-0.23592,0.018362],
- [0.58308,0.30532],
- [0.67836,0.33871]
- ];
- $sets = [
- [0.36047,0.22733],
- [-0.16616,0.042805],
- [0.28578,0.20116],
- [-0.57184,-0.099338],
- [0.23454,0.1832],
- ];
- $learning_rate = .001;
- $weights = [
- mt_rand(-.001 * 10000000, .001 * 10000000) / 10000000,
- mt_rand(-.001 * 10000000, .001 * 10000000) / 10000000
- ];
- $bias = 1;
- $learning_count = count($inputs);
- echo '<pre>';
- $previous_error = 999;
- for ($i = 1; ; ++$i) {
- $total_error = 0;
- foreach ($inputs as $input) {
- $out = $input[0] * $weights[0] + $weights[1];
- $total_error += pow($error = $input[1] - $out, 2);
- $weights[0] += $learning_rate * $input[0] * $error;
- $weights[1] += $learning_rate * $error;
- }
- $total_error = $total_error / $learning_count;
- if ($total_error === $previous_error || $i > 500000) break;
- $previous_error = $total_error;
- }
- echo '<table>';
- echo '<tr><td colspan="3"><b>Solving</b></td></tr>';
- foreach ($sets as $set) {
- $out = $set[0] * $weights[0] + $bias * $weights[1];
- echo '<tr><td>' . $out . '</td><td>' . $set[1] . '</td><td>' . ($out === $set[1] ? '' : '×') . '</td></tr>';
- }
- echo '</table>';
Advertisement
Add Comment
Please, Sign In to add comment