Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // H2_2
- //
- // Created by Ulrich Ziegler on 02.12.17.
- // Copyright © 2017 Ulrich Ziegler. All rights reserved.
- //
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- //----------- GLOBAL VARIABLES----------------------
- double training[1000][3] = {{0}};
- double data[1000][2] = {{0}};
- double weight1[4][3] = {{0}};
- double weight2[4][5] = {{0}};
- double weight3[4][5] = {{0}};
- double weight_o[5] = {0};
- double output1[5] = {0};
- double output2[5] = {0};
- double output3[5] = {0};
- double output_o = 0;
- double error_i[2] = {0};
- double error1[4] = {0};
- double error2[4] = {0};
- double error3[4] = {0};
- double error_o = 0;
- double d_weight1[4][3] = {{0}};
- double d_weight2[4][5] = {{0}};
- double d_weight3[4][5] = {{0}};
- double d_weight_o[5] = {0};
- //*******************************************************************************************
- void random_weights(){
- double value = 0.4;
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 3; j++){
- weight1[i][j] = ((double)rand()/(double)(RAND_MAX)) * value;
- if(i == 0 && j == 0){
- weight1[i][j] += 0.001;
- }
- if((i + j) % 2 == 0){
- weight1[i][j] = -1 * weight1[i][j];
- }
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- weight2[i][j] = ((double)rand()/(double)(RAND_MAX)) * value;
- if(i == 0 && j == 0){
- weight2[i][j] += 0.001;
- }
- if((i + j) % 2 == 0){
- weight2[i][j] = -1 * weight2[i][j];
- }
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- weight3[i][j] = ((double)rand()/(double)(RAND_MAX)) * value;
- if(i == 0 && j == 0){
- weight3[i][j] += 0.001;
- }
- if((i + j) % 2 == 0){
- weight3[i][j] = -1 * weight3[i][j];
- }
- }
- }
- for(int i = 0; i < 5; i++){
- weight_o[i] = ((double)rand()/(double)(RAND_MAX)) * value;
- if(i == 0){
- weight_o[i] += 0.001;
- }
- if(i % 2 == 0){
- weight_o[i] = -1 * weight_o[i];
- }
- }
- }
- //*******************************************************************************************
- void input(int* counter_t, int* counter_d){
- int i = 0;
- int j = 0;
- while(scanf("%lf,%lf,%lf", &training[i][0], &training[i][1], &training[i][2]))
- {
- if(training[i][0] == 0.0 && training[i][1] == 0.0 && training[i][2] == 0.0){
- break;
- }
- else{
- i++;
- }
- }
- while(scanf("%lf,%lf", &data[j][0], &data[j][1]) != 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][0])> *max_x){
- *max_x=fabs(data[j][0]);}
- if(fabs(data[j][1])> *max_y){
- *max_y=fabs(data[j][1]);}
- 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_y;
- }
- for(int j = 0; j < *counter_d; j++){
- data[j][0]/=*max_x;
- data[j][1]/=*max_y;
- }
- }
- //*******************************************************************************************
- void output(int iterator){
- double buffer[3] = {training[iterator][0], training[iterator][1], 1};
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for (int j = 0; j < 3; j++)
- {
- sum += buffer[j] * weight1[i][j];
- }
- output1[i] = tanh(sum);
- }
- output1[4]=1; //bias
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for (int j = 0; j < 5; j++)
- {
- sum += output1[j] * weight2[i][j];
- }
- output2[i] = tanh(sum);
- }
- output2[4]=1; //bias;
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for (int j = 0; j < 5; j++)
- {
- sum += output2[j] * weight3[i][j];
- }
- output3[i] = tanh(sum);
- }
- output3[4]=1; //bias;
- double sum = 0;
- for (int i = 0; i < 5; i++)
- {
- sum += output3[i] * weight_o[i];
- }
- output_o = tanh(sum);
- }
- //*******************************************************************************************
- void error(int iterator){
- error_o = 2*(training[iterator][2]-output_o)*(1 - output_o * output_o);
- for(int i = 0; i < 4; i++){
- error3[i]=error_o*weight_o[i]*(1 - output3[i] * output3[i]);
- }
- for(int j = 0; j < 4; j++){
- error2[j]=0;
- for(int i = 0; i < 4; i++){
- error2[j]+=error3[i]*weight3[i][j]*(1-output2[j]*output2[j]);
- }
- }
- for(int j = 0; j < 4; j++){
- error1[j]=0;
- for(int i = 0; i < 4; i++){
- error1[j]+=error2[i]*weight2[i][j]*(1-output1[j]*output1[j]);
- }
- }
- for(int j = 0; j < 2; j++){
- error_i[j]=0;
- for(int i = 0; i < 4; i++){
- error_i[j]+=error1[i]*weight1[i][j]*(1- training[iterator][j] * training[iterator][j]);
- }
- }
- }
- //*******************************************************************************************
- void weight_change(int iterator, double learning_rate){
- for(int i = 0; i < 4; i++){
- d_weight_o[i] += learning_rate*error_o*output3[i];
- }
- d_weight_o[4] += learning_rate*error_o;
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4; j++){
- d_weight3[i][j] += learning_rate*error3[j]*output2[i];
- }
- }
- for(int i = 0; i < 4; i++){
- d_weight3[i][4] += learning_rate*error3[i];
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 4; j++){
- d_weight2[i][j] += learning_rate*error2[j]*output1[i];
- }
- }
- for(int i = 0; i < 4; i++){
- d_weight2[i][4] += learning_rate*error2[i];
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 2; j++){
- d_weight1[i][j] += learning_rate*error1[i]*training[iterator][j];
- }
- }
- for(int i = 0; i < 4; i++){
- d_weight1[i][2] += learning_rate*error1[i];
- }
- }
- //*******************************************************************************************
- void weight_update(){
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 3; j++){
- weight1[i][j]+=d_weight1[i][j];
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- weight2[i][j]+=d_weight2[i][j];
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- weight3[i][j]+=d_weight3[i][j];
- }
- }
- for(int i = 0; i < 5; i++){
- weight_o[i]+=d_weight_o[i];
- }
- }
- //*******************************************************************************************
- void initialize_d_weights(){
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 3; j++){
- d_weight1[i][j]=0;
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- d_weight2[i][j]=0;
- }
- }
- for(int i = 0; i < 4; i++){
- for(int j = 0; j < 5; j++){
- d_weight3[i][j]=0;
- }
- }
- for(int i = 0; i < 5; i++){
- d_weight_o[i]=0;
- }
- }
- //*******************************************************************************************
- void test(int i){
- double buffer[3] = {data[i][0], data[i][1], 1};
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for(int j = 0; j < 3; j++)
- {
- sum += buffer[j] * weight1[i][j];
- }
- output1[i] = tanh(sum);
- }
- output1[4]=1;
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for (int j = 0; j < 5; j++)
- {
- sum += output1[j] * weight2[i][j];
- }
- output2[i] = tanh(sum);
- }
- output2[4]=1;
- for (int i = 0; i < 4; i++)
- {
- double sum = 0;
- for (int j = 0; j < 5; j++)
- {
- sum += output2[j] * weight3[i][j];
- }
- output3[i] = tanh(sum);
- }
- output3[4]=1;
- double sum = 0;
- for (int i = 0; i < 5; i++)
- {
- sum += output3[i] * weight_o[i];
- }
- output_o = tanh(sum);
- }
- int main(void) {
- int counter_t = 0;
- int counter_d = 0;
- double error_sum = 0;
- double max_x = 0;
- double max_y = 0;
- /*
- training[0][0] = 1.3;
- training[0][1] = 0.4;
- training[0][2] = 1;
- */
- //--------------------- 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);
- //--------------------- RANDOM VALUES TO WEIGHTS -------------------------------------
- random_weights();
- //--------------------- TRAINING OF NEURONAL NETWORK --------------------------------
- /*
- output(0);
- for(int i = 0; i < 5; i++){
- printf("output1: %lf\n", output1[i]);
- }
- for(int i = 0; i < 5; i++){
- printf("output2: %lf\n", output2[i]);
- }
- for(int i = 0; i < 5; i++){
- printf("output3: %lf\n", output3[i]);
- }
- printf("output4: %lf\n", output4);
- error(0);
- for(int i = 0; i < 2; i++){
- printf("error0; %lf\n", error0[i]);
- }
- for(int i = 0; i < 4; i++){
- printf("error1: %lf\n", error1[i]);
- }
- for(int i = 0; i < 4; i++){
- printf("error2: %lf\n", error2[i]);
- }
- for(int i = 0; i < 4; i++){
- printf("error3: %lf\n", error3[i]);
- }
- printf("error4: %lf\n", error4);
- printf("\n");
- weight_change(0, 0.001);
- printf("d_weight4:\n");
- for(int i = 0; i < 5; i++){
- printf("%lf, ",d_weight4[i]);
- }
- printf("\n");
- printf("d_weight3:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 5; i++){
- printf("%lf, ",d_weight3[j][i]);
- }
- printf("\n");
- }
- printf("\n");
- printf("d_weight2:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 5; i++){
- printf("%lf, ",d_weight2[j][i]);
- }
- printf("\n");
- }
- printf("\n");
- printf("d_weight1:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 3; i++){
- printf("%lf, ",d_weight1[j][i]);
- }
- printf("\n");
- }
- printf("\n");
- printf("\n");
- printf("\n");
- weight_update();
- printf("weight4:\n");
- for(int i = 0; i < 5; i++){
- printf("%lf, ",weight4[i]);
- }
- printf("\n");
- printf("\n");
- printf("weight3:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 5; i++){
- printf("%lf, ",weight3[j][i]);
- }
- printf("\n");
- }
- printf("\n");
- printf("weight2:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 5; i++){
- printf("%lf, ",weight2[j][i]);
- }
- printf("\n");
- }
- printf("\n");
- printf("weight1:\n");
- for(int j = 0; j < 4; j++){
- for(int i = 0; i < 3; i++){
- printf("%lf, ",weight1[j][i]);
- }
- printf("\n");
- }
- */
- while(fabs(error_sum) > 0.001){
- error_sum = 0.0;
- for (int i = 0; i < counter_t; i++){
- output(i);
- error(i);
- error_sum+= error_o;
- weight_change(i, 0.001);
- }
- //printf("%lf\n", error_sum);
- weight_update();
- initialize_d_weights();
- }
- //--------------------- TESTING OF NEURON --------------------------------------------
- float value = 0.0;
- for(int i= 0; i < counter_d; i++){
- test(i);
- value = output_o;
- if(value < 0){
- printf("-1\n");
- }
- else{
- printf("+1\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement