Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  H2_3
  4. //
  5. //  Created by Ulrich Ziegler on 02.12.17.
  6. //  Copyright © 2017 Ulrich Ziegler. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <math.h>
  12.  
  13.  
  14.  
  15. //----------- GLOBAL VARIABLES----------------------
  16.  
  17. double training[1000][2] = {{0}};
  18. double data[1000] = {0};
  19.  
  20.  
  21. void input(int* counter_t, int* counter_d){
  22.    
  23.     int i = 0;
  24.     int j = 0;
  25.    
  26.     while(scanf("%lf,%lf", &training[i][0], &training[i][1]))
  27.     {
  28.         if(training[i][0] == 0.0 && training[i][1] == 0.0){
  29.             break;
  30.         }
  31.        
  32.         else{
  33.             i++;
  34.         }
  35.     }
  36.    
  37.     while(scanf("%lf", &data[j]) != EOF)
  38.     {
  39.         j++;
  40.     }
  41.    
  42.     *counter_t=i;
  43.     *counter_d=j;
  44. }
  45.  
  46.  
  47. void maximum(int counter_t, int counter_d, double* max_x, double* max_y){
  48.    
  49.     int i = 0;
  50.     int j = 0;
  51.    
  52.     while(i < counter_t){
  53.         if(fabs(training[i][0])> *max_x){
  54.             *max_x=fabs(training[i][0]);}
  55.         if(fabs(training[i][1])> *max_y){
  56.             *max_y=fabs(training[i][1]);}
  57.             i++;
  58.     }
  59.    
  60.     while(j < counter_d){
  61.         if(fabs(data[j])>*max_x){
  62.             *max_x=fabs(data[j]);}
  63.         j++;
  64.     }
  65. }
  66.  
  67.  
  68. void scale_matrix(int counter_t, int counter_d, double max_x, double max_y){
  69.    
  70.     for(int i = 0; i < counter_t; i++){
  71.         training[i][0]/=max_x;
  72.         training[i][1]/=max_x;
  73.     }
  74.    
  75.     for(int j = 0; j < counter_d; j++){
  76.         data[j]/=max_x;
  77.     }
  78. }
  79.  
  80.  
  81. void train(double* weight, int counter_t){
  82.    
  83.     double sum[2] = {0.0};
  84.     double error = 0.0;
  85.     double core = 0.0;
  86.     double learning_rate = 0.0001;
  87.     int i = 0;
  88.    
  89.     while(1){
  90.        
  91.         error = 0.0;
  92.        
  93.         // SUMMATION OF DERIVATIVES
  94.        
  95.         for(i = 0; i < counter_t; i++){
  96.            
  97.             core = training[i][0]*weight[0]+weight[1];
  98.            
  99.             sum[0] += (-2)*(training[i][1]-core)*training[i][0];
  100.             sum[1] += (-2)*(training[i][1]-core);
  101.            
  102.             error+= pow((training[i][1]-core),2);
  103.            
  104.             i++;
  105.         }
  106.        
  107.         error/= counter_t;
  108.        
  109.        
  110.         // WEIGHTUPDATE
  111.         weight[0]+= (-1)*sum[0]*learning_rate;
  112.         weight[1]+= (-1)*sum[1]*learning_rate;
  113.        
  114.        
  115.         //INITIALIZING SUM
  116.         sum[0]=0.0;
  117.         sum[1]=0.0;
  118.        
  119.         //printf("error: %lf\n", error);
  120.        
  121.         if(error < 0.0001){
  122.             return;
  123.         }
  124.        
  125.     }
  126. }
  127.  
  128.  
  129. void testing(double* weight, int counter_d, double max_y){
  130.    
  131.     double result = 0;
  132.    
  133.     for(int i = 0; i < counter_d; i++){
  134.        
  135.         result = (data[i]*weight[0]+weight[1]*max_y);
  136.         printf("%lf\n", result);
  137.        
  138.     }
  139. }
  140.  
  141.  
  142. int main(void) {
  143.    
  144.     int counter_t = 0;
  145.     int counter_d = 0;
  146.    
  147.     double max_x = 0;
  148.     double max_y = 0;
  149.    
  150.     double weight[2] = {0.0};
  151.     weight[0] = 0.13;
  152.     weight[1] = -0.18;
  153.    
  154.    
  155.     //--------------------- EINLESEN -----------------------------------------------------
  156.    
  157.     input(&counter_t, &counter_d);
  158.    
  159.     //--------------------- FIND MAXIMUM -------------------------------------------------
  160.    
  161.     maximum(counter_t, counter_d, &max_x, &max_y);
  162.    
  163.     //--------------------- SCALIZATION OF MATRICES --------------------------------------
  164.    
  165.     scale_matrix(counter_t, counter_d, max_x, max_y);
  166.    
  167.     //--------------------- TRAINING OF NEURON -------------------------------------------
  168.    
  169.     train(weight, counter_t);
  170.    
  171.     //--------------------- TESTING OF NEURON --------------------------------------------
  172.    
  173.     testing(weight, counter_d, max_y);
  174.    
  175.    
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement