Jose Ramon
Jose Ramon

Reputation: 5444

Neural network output in c++

I want to create a function that calculates neural network output. The elements of my NN is a 19D input vector and a 19D output vector. I choose one hidden layer with 50 neurons. My code is the following but i am not quite sure if it works properly.

double *BuildPlanner::neural_tactics(){


    norm();  //normalize input vector
    ReadFromFile();   // load weights W1 W2 b1

    double hiddenLayer [50][1];


    for(int h=0; h<50; h++){
            hiddenLayer[h][0] =0;
            for(int f = 0; f < 19; f++){

                    hiddenLayer[h][0] = hiddenLayer[h][0] + W1[h][f]*input1[f][0];
            }
    }

    double HiddenLayer[50][1];

    for(int h=0; h<50; h++){
            HiddenLayer[h][0] = tanh(hiddenLayer[h][0] + b1[h][0]);
    }

    double outputLayer[50][1];

    for(int h=0; h<19; h++){
            for(int k=0; k<50; k++){
                    outputLayer[h][0] = outputLayer[h][0] + W2[h][k]*HiddenLayer[k][0];
            }
    }

    double Output[19];

    for(int h=0; h<19; h++){

            Output[h] = tanh(outputLayer[h][0]);
    }

    return Output;
}

Actually I not quite sure about the matrices multiplication. W1*input+b1 where the size of the matrices are 50x19 * 19x1 + 50x1 and W2*outHiddenLayer 19x50*50x1!

Upvotes: 0

Views: 295

Answers (1)

Matt Phillips
Matt Phillips

Reputation: 9691

Your matrix multiplication looks ok to me, but there are other problems--`outputLayer is 50x1 but a) you only iterate through the first 19 elements, and b) you have it on the RHS of your equation

outputLayer[h][0] = outputLayer[h][0] + W2[h][k]...

before that element has ever been defined. That could be causing all your problems. Also, although I assume you're making outputLayer 2-dimensional to make them look matrix-like, it's completely gratuitous and slows things down when the second dimension has size 1--just declare it and the others as

double outputLayer[50];

since it's a vector and those are always one dimensional so it will actually make your code clearer.

Upvotes: 1

Related Questions