This basic neural network keeps returning NaN

I'm using this project to teach my self C++ and it mostly works but if I give is any amount of varying input it just outputs NaN and variables become monstrously large or small. I have gone thought it with Xcode breakpoint and debuged it the best I could. Some help would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <math.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

double derFun(double var);
double resFun(double var);
float trueRes(int n1,int n2);

float e = 2.718281828459045;
float s1 = 0.8;
float s2 = 0.2;
float s3 = 0.4;
float s4 = 0.9;
float s5 = 0.3;
float s6 = 0.5;
float s7 = 0.3;
float s8 = 0.5;
float s9 = 0.9;

float n1 = 1; //Input 1
float n2 = 1; //Input 2
float n3;
float n4;
float n5;
float n6 = 1; //Output Neuron

float target = 0;


int main() {
    
    srand(time(NULL));
    
    while(1 > 0){
    n1 = rand()%2;
    n2 = rand()%2;
        
    n3 = resFun((n1*s1)+(n2*s2));
    n4 = resFun((n1*s3)+(n2*s4));
    n5 = resFun((n1*s5)+(n2*s6));

    n6 = resFun((n3*s7)+(n4*s8)+(n5*s9));
    printf("Calculated Result = %f \n", n6);
    float dSum = derFun((n3*s7)+(n4*s8)+(n5*s9))*(trueRes(n1,n2)-n6);
    printf("DSUM = %f \n", dSum);
    printf("Wanted Result = %f \n", trueRes(n1,n2));
    
    
    float dHS1 = (dSum/s7)*derFun((n1*s1)+(n2*s2));
    float dHS2 = (dSum/s8)*derFun((n1*s3)+(n2*s4));
    float dHS3 = (dSum/s9)*derFun((n1*s5)+(n2*s6));
    
    s1 = s1+(dHS1/n1);
    s2 = s2+(dHS1/n2);
    s3 = s3+(dHS2/n1);
    s4 = s4+(dHS2/n2);
    s5 = s5+(dHS3/n1);
    s6 = s6+(dHS3/n2);

    s7 = s7+(dSum/n3);
    s8 = s8+(dSum/n4);
    s9 = s9+(dSum/n5);
    
    }
    
    return 0;
    
}

float trueRes(int n1,int n2){
    if(n1==1&n2==0){
        return 1;
    }
    if(n1==0&n2==1){
        return 1;
    }
    if(n1==1&n2==1){
        return 0;
    }
    return 0;
}

double resFun(double var){
    return 1/(1+pow(e,-var));
}

double derFun(double var){
    return pow(e,-var)/pow(1+pow(e,-var),2);}



In you're while loop you state that

1
2
3
4
5
While {1 > 0}//While 1 is greater than 0
{
//run the inside

}


The problem here is that, 1 will always be greater than 0 so this will never cease to run.
This is something to be highly discouraged.

Your functions return 0 or 1 for true/false, but neither is used in the while loop so that loop will never exit and your program will never stop.
The solution is to have your while loop compared to a variable. So that you can exit the program.

1
2
3
4
While(RunProgram == true)
{
//run until RunProgram set to false
}

I have it to be an infinite loop because I'm testing. this is not the problem i want it to repeat hundreds of times. What I'm wanting to know is why, when it repeats, dose it tend to stop producing answers? The answers it gives is "nan".

This is a example of output information from the network.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Console output

//this is what it should do
Calculated Result = 0.746615 
DSUM = 0.047936 
Wanted Result = 1.000000 

//This is the problem
Calculated Result = nan 
DSUM = nan 
Wanted Result = 0.000000 
Calculated Result = nan 
DSUM = nan 
Wanted Result = 0.000000 
Calculated Result = nan 
DSUM = nan 
Wanted Result = 0.000000 
Calculated Result = nan 
DSUM = nan 
Wanted Result = 0.000000 
Calculated Result = nan 
DSUM = nan 
Wanted Result = 1.000000 
One of the commonest reasons for getting NaN is dividing by zero. Your program has every chance of doing that.

In setting
1
2
    n1 = rand()%2;
    n2 = rand()%2;

then each of n1 and n2 can take the values 0 or 1 at random.

When you then come to run the lines
1
2
    s1 = s1+(dHS1/n1);
    s2 = s2+(dHS1/n2);

this will fail, on average, three-quarters of the time, because one of n1 or n2 will be zero.
Topic archived. No new replies allowed.