Advertisement

12.28.2006 at 03:33PM PST, ID: 22105647
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2

Backpropagation and C++

Asked by crash_matrix in Physics & Artificial Intelligence in Game Programming

Tags:

On recommendation form a fellow coder, I'm reposting this question here.

I can never seem to get backpropagation right, and every time I ask for help, noone seems to be able to answer or their suggestions don't solve the problem.  So, I thought I might ask for help from a different point -- my classes without the backprop algorithm.
Can anyone tell me the procedure for backpropagating, based on the source below?  I merely need to know how the backpropagation algorithm would work for my class setup (I've been trying to get the darned thing for 2 years now -- clearly I'm using the wrong approach).

#include <iostream.h>
#include <conio.h>
#include <math.h>

class neuron
{
public:
     float ** input;
     float * weight;
     unsigned numInputs;
     float output;
     float dummy;     // for returning bad references

     neuron(unsigned input_numInputs);
     neuron();
     void rebuild(unsigned input_numInputs);
     ~neuron();
     void setInput(neuron& n, unsigned inputIndex);
     void setInput(float& floatInput, unsigned inputIndex);
     float& getInput(unsigned inputIndex);
     void recalc();
     void setWeight(unsigned inputIndex, float value);
};

class neuralNetwork
{
public:
     unsigned numInputLayer;
     unsigned numHiddenLayer;
     unsigned numInputs;
     float * input;
     neuron * inputLayer;
     neuron * hiddenLayer;
     neuron outputLayer;

     neuralNetwork(unsigned input_numInputs, unsigned input_numInputLayer, unsigned input_numHiddenLayer);
     ~neuralNetwork();
};

float XOR(float input1, float input2, float biasLower = 0.0f, float biasUpper = 1.0f);

void neuron::setWeight(unsigned inputIndex, float value)
{
     if (inputIndex >= numInputs)
          return;
     weight[inputIndex] = value;
}
void neuron::recalc()
{
     if (numInputs <= 0)
          return;
     float weightedInput = 0.0f;
     for (unsigned i = 0 ; i < numInputs ; i++)
          weightedInput += weight[i] * input[i][0];
     output = 1.0f/ (1.0f + expf(0.0f-weightedInput));
}
void neuron::setInput(float& floatInput, unsigned inputIndex)
{
     if (inputIndex >= numInputs)
          return;
     else
          input[inputIndex] = &floatInput;
}
float& neuron::getInput(unsigned inputIndex)
{
     if (inputIndex >= numInputs)
          return(dummy);
     else
          return(input[inputIndex][0]);
}
neuron::neuron()
{
     numInputs = 0;
}
neuron::neuron(unsigned input_numInputs)
{
     numInputs = 0;
     rebuild(input_numInputs);
}
void neuron::rebuild(unsigned input_numInputs)
{
     if (numInputs > 0)
     {
          delete[] input;
          delete[] weight;
     }
     numInputs = input_numInputs;
     input = new float*[numInputs];
     weight = new float[numInputs];
}
neuron::~neuron()
{
     if (numInputs >= 0)
     {
          delete input;
          delete weight;
     }
}
void neuron::setInput(neuron& n, unsigned inputIndex)
{
     if (inputIndex >= numInputs)
          return;
     input[inputIndex] = &n.output;
}

neuralNetwork::neuralNetwork(unsigned input_numInputs, unsigned input_numInputLayer, unsigned input_numHiddenLayer)
{
     unsigned i,j;
     numInputs = input_numInputs;
     numHiddenLayer = input_numHiddenLayer;
     numInputLayer = input_numInputLayer;
     input = new float[numInputs];
     inputLayer = new neuron[numInputLayer];

     hiddenLayer = new neuron[numHiddenLayer];
     outputLayer.rebuild(numHiddenLayer);
     for (i = 0 ; i < numHiddenLayer ; i++)
     {
          hiddenLayer[i].rebuild(numInputLayer);
          outputLayer.setInput(hiddenLayer[i],i);
     }
     for (i = 0 ; i < numInputLayer ; i++)
     {
          inputLayer[i].rebuild(numInputs);
          for (j = 0 ; j < numHiddenLayer ; j++)
               hiddenLayer[j].setInput(inputLayer[i],i);
     }
     for (i = 0 ; i < numInputs ; i++)
          for (j = 0 ; j < numInputLayer ; j++)
               inputLayer[j].setInput(input[i],i);
}
neuralNetwork::~neuralNetwork()
{
     if (numInputs > 0)
          delete input;
     if (numInputLayer > 0)
          delete[] inputLayer;
     if (numHiddenLayer > 0)
          delete[] hiddenLayer;
     
}

float XOR(float input1, float input2, float biasLower, float biasUpper)
{
     float correctedInput1, correctedInput2;
     if (input1 < ((biasLower + biasUpper)/2.0f))
          correctedInput1 = biasLower;
     else
          correctedInput1 = biasUpper;
     if (input2 < ((biasLower + biasUpper)/2.0f))
          correctedInput2 = biasLower;
     else
          correctedInput2 = biasUpper;
     if (correctedInput1 == correctedInput2)
     {
          return(biasLower);
     }
     else
     {
          return(biasUpper);
     }
}

int main(int argc, char* argv[])
{
     neuralNetwork nn(2,2,1);
     cout << "Done!" << endl;
     getch();
     return 0;
}

-- Crash Matrix

Followup:  Following is the backpropagation code I use (which, btw, doesn't work).

void neuralNetwork::backpropagate(float target)
{
     if (numInputs == 0 || numInputLayer == 0 || numHiddenLayer == 0)
          return;
     float deltaOutput;
     float * deltaHidden = new float[numHiddenLayer];
     unsigned i,j;
     /*
          Note that there are two outputs used here:
          1) The output of the current layer determines the delta value,
          2) The output of the neuron connected to the input of the current neuron is used in
               readjustment of the weight.
     */
     // Do the output layer first
     deltaOutput = (target - outputLayer.output) * outputLayer.output * (1.0f - outputLayer.output);
     for (i = 0 ; i < numHiddenLayer ; i++)
          outputLayer.weight[i] += learnRate * deltaOutput * hiddenLayer[i].output;
     // Now do the hidden layer
     for (j = 0 ; j < numHiddenLayer ; j++)
     {
          deltaHidden[j] = outputLayer.weight[j] * deltaOutput *
               hiddenLayer[j].output * (1.0f - hiddenLayer[j].output);
          for (i = 0 ; i < numInputLayer ; i++)
          {
               hiddenLayer[j].weight[i] += deltaHidden[j] * learnRate * inputLayer[i].output;
          }
     }
}


And the way I run it is as follows:

     unsigned i,j,k;
     neuralNetwork nn(2,2,1);
     for (k = 0 ; k < 100000 ; k++)
     {
          for (i = 0 ; i < 2 ; i++)
               for (j = 0 ; j < 2 ; j++)
               {
                    nn.input[0] = (float)i;
                    nn.input[1] = (float)j;
                    nn.recalc();
                    nn.backpropagate(XOR((float)i,(float)j));
                    if (k == 0 || k == 99999)
                    {
                         nn.recalc();
                         cout << i << " XOR " << j << " -> " << nn.outputLayer.output << " SHOULD BE " << XOR((float)i,(float)j) << endl;
                    }
               }
          if (k == 0 || k == 99999)
               cout << endl;
     }

I know; I don't set a precision limiter on it.  But, the outputs never come any closer than 0.4 out of 1.0 (The low and high are 0.0 and 1.0 respectively and the outputs never go outside of 0.45 and 0.55).  The outputs should move toward 1.0 and 0.0 (obviously), but they don't.
Any ideas what's wrong?

-- CMStart Free Trial
 
 
Loading Advertisement...
 
[+][-]12.28.2006 at 06:50PM PST, ID: 18212105

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.28.2006 at 07:05PM PST, ID: 18212147

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.28.2006 at 10:45PM PST, ID: 18212821

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.28.2006 at 10:55PM PST, ID: 18213056

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Physics & Artificial Intelligence in Game Programming
Tags: backpropagation
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12.28.2006 at 10:56PM PST, ID: 18213092

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.29.2006 at 07:43PM PST, ID: 18217790

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.29.2006 at 08:36PM PST, ID: 18217939

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.29.2006 at 10:33PM PST, ID: 18218155

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.29.2006 at 11:28PM PST, ID: 18218252

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.30.2006 at 08:21AM PST, ID: 18219165

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.30.2006 at 04:10PM PST, ID: 18220132

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.30.2006 at 04:14PM PST, ID: 18220141

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.30.2006 at 04:19PM PST, ID: 18220155

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.31.2006 at 08:43PM PST, ID: 18223024

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42