neural net code problem

Hi all,

New to coding and forum, hoping to get assistance in getting up & running with an artificial neural network code using arrays & for loops. General practice code I am writing just now to carry out a forward pass on a 4 pixel image, 1 per input of network, 4 hidden nodes & 2 outputs.

I have come up with the code below but getting random error numbers/code returned. Can't seem to see where I am going wrong & keep coming up short of the solution. Any help would be greatly appreciated, thanks.



#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <fstream>
#include <float.h>
using namespace std;




int main()
{

//array input declaration
float in[1][4] = {
{ 1, 0, 1, 0 }
};
//declare weight array
float w[4][3] = {
{ 0.326, 0.577, 0.101},
{ 0.911, 0.651, 0.665},
{ 0.308, 0.052, 0.074},
{ 0.656, 0.430, 0.698}
};

int count1, count2;
float sum = 0, hidout[3];

for (count1 = 0; count1 < 3; count1++)
{
for (count2 = 0; count2 < 4; count2++)
{
sum += (in[1][count2] * w[count2][count1]);
cout << "sum = " << sum << endl;
} hidout[count1] = (1 / (1 + exp(sum * -1)));
cout << "Hidden layer output is = " << hidout << endl;
}


return 0;
}



"in" is a 1x4 array which means the first index can only be 0, but you index it with 1, which accesses outside the array, picking up arbitrary values. But it should probably just be a 1D array in the first place.

Unless you have a special reason to use float it's more normal to use double.

And count1, count2 are stupid names! i and j are better, IMAO. :-)

Also, remember to use [code] [/code] tags in the future (as I've done below).

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <float.h>
using namespace std;

int main() {
    // input array
    double in[4] = { 1, 0, 1, 0 };
    // weight array
    double w[4][3] = {
        { 0.326, 0.577, 0.101},
        { 0.911, 0.651, 0.665},
        { 0.308, 0.052, 0.074},
        { 0.656, 0.430, 0.698}
    };
    double sum = 0, hidout[3];

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            sum += in[j] * w[j][i];
            cout << "sum = " << sum << '\n';
        }
        hidout[i] = 1.0 / (1.0 + exp(-sum));
        cout << "Hidden layer output is = " << hidout[i] << '\n';
    }
}

Last edited on
Topic archived. No new replies allowed.