Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // H2_3
- //
- // Created by Ulrich Ziegler on 02.12.17.
- // Copyright © 2017 Ulrich Ziegler. All rights reserved.
- //
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- //----------- GLOBAL VARIABLES----------------------
- double training[1000][2] = {{0}};
- double data[1000] = {0};
- void input(int* counter_t, int* counter_d){
- int i = 0;
- int j = 0;
- while(scanf("%lf,%lf", &training[i][0], &training[i][1]))
- {
- if(training[i][0] == 0.0 && training[i][1] == 0.0){
- break;
- }
- else{
- i++;
- }
- }
- while(scanf("%lf", &data[j]) != EOF)
- {
- j++;
- }
- *counter_t=i;
- *counter_d=j;
- }
- void maximum(int counter_t, int counter_d, double* max_x, double* max_y){
- int i = 0;
- int j = 0;
- while(i < counter_t){
- if(fabs(training[i][0])> *max_x){
- *max_x=fabs(training[i][0]);}
- if(fabs(training[i][1])> *max_y){
- *max_y=fabs(training[i][1]);}
- i++;
- }
- while(j < counter_d){
- if(fabs(data[j])>*max_x){
- *max_x=fabs(data[j]);}
- j++;
- }
- }
- void scale_matrix(int counter_t, int counter_d, double max_x, double max_y){
- for(int i = 0; i < counter_t; i++){
- training[i][0]/=max_x;
- training[i][1]/=max_x;
- }
- for(int j = 0; j < counter_d; j++){
- data[j]/=max_x;
- }
- }
- void train(double* weight, int counter_t){
- double sum[2] = {0.0};
- double error = 0.0;
- double core = 0.0;
- double learning_rate = 0.0001;
- int i = 0;
- while(1){
- error = 0.0;
- // SUMMATION OF DERIVATIVES
- for(i = 0; i < counter_t; i++){
- core = training[i][0]*weight[0]+weight[1];
- sum[0] += (-2)*(training[i][1]-core)*training[i][0];
- sum[1] += (-2)*(training[i][1]-core);
- error+= pow((training[i][1]-core),2);
- i++;
- }
- error/= counter_t;
- // WEIGHTUPDATE
- weight[0]+= (-1)*sum[0]*learning_rate;
- weight[1]+= (-1)*sum[1]*learning_rate;
- //INITIALIZING SUM
- sum[0]=0.0;
- sum[1]=0.0;
- //printf("error: %lf\n", error);
- if(error < 0.0001){
- return;
- }
- }
- }
- void testing(double* weight, int counter_d, double max_y){
- double result = 0;
- for(int i = 0; i < counter_d; i++){
- result = (data[i]*weight[0]+weight[1]*max_y);
- printf("%lf\n", result);
- }
- }
- int main(void) {
- int counter_t = 0;
- int counter_d = 0;
- double max_x = 0;
- double max_y = 0;
- double weight[2] = {0.0};
- weight[0] = 0.13;
- weight[1] = -0.18;
- //--------------------- EINLESEN -----------------------------------------------------
- input(&counter_t, &counter_d);
- //--------------------- FIND MAXIMUM -------------------------------------------------
- maximum(counter_t, counter_d, &max_x, &max_y);
- //--------------------- SCALIZATION OF MATRICES --------------------------------------
- scale_matrix(counter_t, counter_d, max_x, max_y);
- //--------------------- TRAINING OF NEURON -------------------------------------------
- train(weight, counter_t);
- //--------------------- TESTING OF NEURON --------------------------------------------
- testing(weight, counter_d, max_y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement