Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- const double version = 0.00001;
- const int t_rows=20;
- double training_set_inputs[t_rows][2], training_set_outputs[t_rows];
- double teller, avgtot, avg, sqerrtot, f_out_requested;
- double reqf_out1,reqf_out1L,reqf_out1K,reqf_out1aL,reqf_out1aK,reqf_out2L,reqf_out2K,reqf_out1M,reqf_out1aM,reqf_out2M, reqf_out1a, reqf_out2, reqf_out3;
- double outt2(double (*array)[t_rows][2], int b)
- {
- double calc;
- calc= pow((*array)[b][0],2)+11*(*array)[b][1];
- return calc;
- }
- double fx(double x) //Tanh function
- {
- return exp(x)-exp(-x)/(exp(x)+exp(-x));
- }
- double fd(double x) //Tanh derivative
- {
- return 4/pow((exp(x)+exp(-x)),2);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand (time(NULL));
- for (int create = 0; create < t_rows; create++) {
- bool redo=false;
- do
- {
- training_set_inputs[create][0]= (rand() % 60) + 20.00;
- training_set_inputs[create][1]= (rand() % 60) + 20.00;
- training_set_outputs[create] = outt2(&training_set_inputs, create);
- if (training_set_outputs[create]==0) redo=true;
- } while (redo==true);
- }
- for(auto& rows: training_set_inputs)
- {
- for(auto& elem: rows)
- {
- //printf("\n %lf",elem);
- elem = 1.00/elem;
- }
- }
- for (auto& number : training_set_outputs)
- {
- number = 1.00/number;
- }
- double requested_data1=21;double requested_data2=31;
- requested_data1=1.00/requested_data1;
- requested_data2=1.00/requested_data2;
- double weight = 0.01+(rand()%1000)/1000.00;
- double weightK = 0.01+(rand()%1000)/1000.00;
- double weightL = 0.01+(rand()%1000)/1000.00;
- double weightM = 0.01+(rand()%1000)/1000.00;
- double weight1a = 0.01+(rand()%1000)/1000.00;
- double weight1aK= 0.01+(rand()%1000)/1000.00;
- double weight1aL= 0.01+(rand()%1000)/1000.00;
- double weight1aM= 0.01+(rand()%1000)/1000.00;
- double weight2 = 0.01+(rand()%1000)/1000.00;
- double weight2K = 0.01+(rand()%1000)/1000.00;
- double weight2L = 0.01+(rand()%1000)/1000.00;
- double weight2M = 0.01+(rand()%1000)/1000.00;
- double weight3 = 0.01+(rand()%1000)/1000.00;
- double f_out1K, f_out1aK,f_out2K, f_out3, f_out1L, f_out1aL, f_out2L, f_out1M, f_out1aM, f_out2M;
- double f_out1 = 0,f_out2 = 0, f_out1a = 0;
- double learn=0.001; //learning-rate
- double err;
- int i=0;
- teller=0;
- while (1)
- {
- i++;
- //int q = rand()%t_rows; //this does not work so well anymore with multinode
- for (int q=0; q < t_rows; q++)
- {
- // Forward propagation, training
- //node 1, input node
- //connection input node 1 with node 2
- f_out1 = fx(training_set_inputs[q][0] * weight);
- //connection input node 1 with node 2K
- f_out1K = fx(training_set_inputs[q][0] * weightK);
- //connection input node 1 with node 2L
- f_out1L = fx(training_set_inputs[q][0] * weightL);
- //connection input node 1 with node 2L
- f_out1M = fx(training_set_inputs[q][0] * weightM);
- //node 1a, input node
- //connection input node 1a with node 2
- f_out1a= fx(training_set_inputs[q][1] * weight1a);
- //connection input node 1a with node 2K
- f_out1aK= fx(training_set_inputs[q][1] * weight1aK);
- //connection input node 1a with node 2L
- f_out1aL= fx(training_set_inputs[q][1] * weight1aL);
- //connection input node 1a with node 2M
- f_out1aM= fx(training_set_inputs[q][1] * weight1aM);
- //middle nodes
- //node 2, middle node, connection with output node 3
- f_out2 = fx(f_out1 * weight2 + f_out1a * weight2);
- //node 2K, middle node, connection with output node 3
- f_out2K = fx(f_out1K * weight2K + f_out1aK * weight2K);
- //node 2L, middle node, connection with output node 3
- f_out2L = fx(f_out1L * weight2L + f_out1aL * weight2L);
- //node 2M, middle node, connection with output node 3
- f_out2M = fx(f_out1M * weight2M + f_out1aM * weight2M);
- //output node 3
- f_out3 = fx(f_out2 * weight3 + f_out2K * weight3+ f_out2L * weight3 + f_out2M * weight3);
- //printf("\nWEIGHTS %lf %lf %lf", weightK, weight1aK, weight2K);
- //error calculation
- err = 2*( f_out3 - training_set_outputs[q] );
- sqerrtot = sqerrtot + pow( f_out3 - training_set_outputs[q],2);
- //backward prop
- weight = weight - learn * err * training_set_inputs[q][0] * fd(f_out1) * weight2 * weight3 ;
- weightK = weightK - learn * err * training_set_inputs[q][0] * fd(f_out1K) * weight2K * weight3 ;
- weightL = weightL - learn * err * training_set_inputs[q][0] * fd(f_out1L) * weight2L * weight3 ;
- weightM = weightM - learn * err * training_set_inputs[q][0] * fd(f_out1M) * weight2M * weight3 ;
- weight1a = weight1a - learn * err * training_set_inputs[q][1] * fd(f_out1a) * weight2 * weight3 ;
- weight1aK= weight1aK -learn * err * training_set_inputs[q][1] * fd(f_out1aK)* weight2K * weight3 ;
- weight1aL= weight1aL -learn * err * training_set_inputs[q][1] * fd(f_out1aL)* weight2L * weight3 ;
- weight1aM= weight1aM -learn * err * training_set_inputs[q][1] * fd(f_out1aM)* weight2M * weight3 ;
- weight2 = weight2 - learn * err * (f_out1+f_out1a) * fd(f_out2) * weight3 ;
- weight2K = weight2K - learn * err * (f_out1K+f_out1aK) * fd(f_out2K) * weight3 ;
- weight2L = weight2L - learn * err * (f_out1L+f_out1aL) * fd(f_out2L) * weight3 ;
- weight2M = weight2M - learn * err * (f_out1M+f_out1aM) * fd(f_out2M) * weight3;
- weight3 = weight3 - learn * err * (f_out2+f_out2K+f_out2L+f_out2M) * fd(f_out3);
- } //q
- if (i>20) {
- //Forward pass on requested data
- reqf_out1 = fx(requested_data1 * weight);
- reqf_out1K = fx(requested_data1* weightK);
- reqf_out1L = fx(requested_data1* weightL);
- reqf_out1M = fx(requested_data1* weightM);
- //node 1a
- reqf_out1a= fx(requested_data2 * weight1a);
- reqf_out1aK= fx(requested_data2 * weight1aK);
- reqf_out1aL= fx(requested_data2 * weight1aL);
- reqf_out1aM= fx(requested_data2 * weight1aM);
- //node 2
- reqf_out2 = fx(reqf_out1 * weight2 + reqf_out1a * weight2);
- reqf_out2K = fx(reqf_out1K * weight2K + reqf_out1aK * weight2K);
- reqf_out2L = fx(reqf_out1L * weight2L + reqf_out1aL * weight2L);
- reqf_out2M = fx(reqf_out1M * weight2M + reqf_out1aM * weight2M);
- //node 3
- reqf_out3 = fx(reqf_out2 * weight3 + reqf_out2K * weight3 + reqf_out2L * weight3 + reqf_out2M * weight3);
- f_out_requested = 1.00/reqf_out3;
- teller = teller +1.00;
- avgtot = avgtot + abs(f_out_requested);
- avg=avgtot/teller;
- printf("\n\nError: %.19lf Output for value %lf as new requested1 data req2 %lf ,output would be %.20lf",sqerrtot,1.00/requested_data1,1.00/requested_data2,f_out_requested );
- sqerrtot = 0;
- }
- }
- scanf("%d", &i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement