0

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!

3
  • You could start by not hardcoding all those sizes, so you can easily plug in some small numbers and go through the result by hand. Commented Sep 4, 2012 at 17:49
  • Yea its a good start. But actually my problem is if multiplications works as the way i want! Commented Sep 4, 2012 at 17:57
  • Check this library: sourceforge.net/projects/c-c-neural-networks Commented Aug 15, 2015 at 12:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

outputLayer the output of NN has 19x1 size. Wrong definition! Just forgot to change 50 to 19!!
@FereRes Ok. But of course b) is really the main problem and would explain why your results are 'weird'--using undefined values won't in this case create compile- or run-time errors, but just lead to undefined behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.