Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- // from http://stackoverflow.com/a/686373
- double rand_weight (double min, double max) {
- return min + ((double) rand()) / ((double) (RAND_MAX / (max - min)));
- }
- int main() {
- srand(time(NULL));
- double sets[1000][2];
- double weights[2] = {
- rand_weight(-.0001, .0001),
- rand_weight(-.0001, .0001)
- };
- double learning_rate = .0001;
- double p0, p1;
- int count = 0;
- while (1) {
- scanf("%lf,%lf\n", &sets[count][0], &sets[count][1]);
- if (sets[count][0] == 0 && sets[count][1] == 0) break;
- count++;
- }
- // The training process starts
- int it;
- double previous_error = 999;
- for (it = 0; ; it++) {
- int i;
- double total_errors = 0;
- for (i = 0; i < count; i++) {
- double out = sets[i][0] * weights[0] + weights[1];
- double error = sets[i][1] - out;
- weights[0] += learning_rate * error * sets[i][0];
- weights[1] += learning_rate * error;
- total_errors += pow(error, 2);
- }
- total_errors /= (double) count;
- if (total_errors == previous_error || it > 5000000) break;
- }
- while (scanf("%lf\n", &p0) != EOF) {
- printf("%lf\n", p0 * weights[0] + weights[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment