Question

Backpropagation and C++

Asked by: crash_matrix

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?

-- CM

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-12-28 at 15:33:02ID22105647
Tags

backpropagation

Topic

Physics & Artificial Intelligence in Game Programming

Participating Experts
1
Points
250
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Algorithm
    What is the best algorithm to check a number is in which particular range of: 0 to 9 10 to 99 100 to 999 1000 to 9999 10000 to 99999 ...rate at exponential of 10 E.g X = 555 is in the range of 100 - 999 I use a if-else-then to do the checking but its looks terribly ugly. A...
  2. Algorithm
    What is the best algorithm to check a number is in which particular range of: 0 to 9 10 to 99 100 to 999 1000 to 9999 10000 to 99999 ...rate at exponential of 10 E.g X = 555 is in the range of 100 - 999 I use a if-else-then to do the checking but its looks terribly ugly. A...
  3. Algorithm
    I want to develop an algorithm that solves the Y2k problem by changing all the y's to k's in a documents.(can be assumed that a document is merely and array of characters) What are good especifications for it?
  4. string::endl or "\n"?
    When "couting", when should you use a "\n" in your string to create a new-line, and when should you use a separate << endl ?
  5. C++ fstream and cout
    i have a little problem, when im working on an encryption algorithm thing and im facing a problem that is not really related to the alg. itslef, well here is a code of what im taking about: im facing the problem that the value of char(i) [for smaller number anyways; under ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ozoPosted on 2006-12-28 at 18:50:47ID: 18212105

what is nn.recalc? nn is a neuralNetwork but the only recalc I see is for a neuron
and XOR((float)i,(float)j) doesn't seem to have enough arguments

 

by: crash_matrixPosted on 2006-12-28 at 19:05:25ID: 18212147

Sorry, I updated the program with a new backpro function last time but didn't include all the changes; this is the recalc function:
void neuralNetwork::recalc()
{
      unsigned i;
      for (i = 0 ; i < numInputLayer ; i++)
            inputLayer[i].recalc();
      for (i = 0 ; i < numHiddenLayer ; i++)
            hiddenLayer[i].recalc();
      outputLayer.recalc();
}

XOR takes 2-4 arguments; the second two have a default value of 0.0f and 1.0f, respectively.  That's there in case the network's output bounds were different than 0 and 1 (some nn programmers output -1 and 1 as the bounds).

-- CM

 

by: ozoPosted on 2006-12-28 at 22:45:44ID: 18212821

Since the inputLayer weights start at 0, and you never adjust them, they always stay 0, and your network pays no attention to the inputs.
Also, xor is very hard to do with 1 hidden layer.
changing

               inputLayer[j].setInput(input[i],i);
to
{
               inputLayer[j].setInput(input[i],i);
               inputLayer[j].setWeight(i,(float)i==j);
}
and
    neuralNetwork nn(2,2,1);
to
   I was able to get
0 XOR 0 -> 0.0110415 SHOULD BE 0
0 XOR 1 -> 0.989376 SHOULD BE 1
1 XOR 0 -> 0.990261 SHOULD BE 1
1 XOR 1 -> 0.00434188 SHOULD BE 0

 

by: ozoPosted on 2006-12-28 at 22:55:38ID: 18213056

Sorry, I actually had another change so that the hidden weight adjustments aren't always exactly the same,
(which would be like having only one hidden weight)
so I also changed
     hiddenLayer[j].setInput(inputLayer[i],i);
to
    {
               hiddenLayer[j].setInput(inputLayer[i],i);
               hiddenLayer[j].setWeight(i,2.0*rand()/RAND_MAX-1);
          }

 

by: ozoPosted on 2006-12-28 at 22:56:57ID: 18213092

I menat to say
and
    neuralNetwork nn(2,2,1);
to
    neuralNetwork nn(2,2,2);

 

by: crash_matrixPosted on 2006-12-29 at 19:43:28ID: 18217790

I attempted to run the program with the changes listed and did not come up with a proper convergence.   The output was:

0 XOR 0 -> 0.645271 SHOULD BE 0
0 XOR 1 -> 0.650663 SHOULD BE 1
1 XOR 0 -> 0.651759 SHOULD BE 1
1 XOR 1 -> 0.652395 SHOULD BE 0

0 XOR 0 -> 0.296809 SHOULD BE 0
0 XOR 1 -> 0.574047 SHOULD BE 1
1 XOR 0 -> 0.577207 SHOULD BE 1
1 XOR 1 -> 0.642519 SHOULD BE 0

Done!

And the two changed functions were main and neuralnetwork::neuralnetwork:
neuralNetwork::neuralNetwork(unsigned input_numInputs, unsigned input_numInputLayer, unsigned input_numHiddenLayer)
{
      unsigned i,j;
      srand((unsigned)time(0));
      learnRate = 0.1f;
      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);
            outputLayer.setWeight(i,((float)(rand()) / (float)(RAND_MAX)));
      }
      for (i = 0 ; i < numInputLayer ; i++)
      {
            inputLayer[i].rebuild(numInputs);
            for (j = 0 ; j < numHiddenLayer ; j++)
            {
                  hiddenLayer[j].setInput(inputLayer[i],i);
                  hiddenLayer[j].setWeight(i,2.0f*rand()/RAND_MAX-1/*((float)(rand()) / (float)(RAND_MAX))*/);
            }
      }
      for (i = 0 ; i < numInputs ; i++)
            for (j = 0 ; j < numInputLayer ; j++)
            {
                  inputLayer[j].setInput(input[i],i);
                  inputLayer[j].setWeight(i,(float)(i==j)/*1.0f*/);
            }
}

int main(int argc, char* argv[])
{
      unsigned i,j,k;
      neuralNetwork nn(2,2,2);
      for (i = 0 ; i < 2 ; i++)
            for (j = 0 ; j < 2 ; j++)
                  nn.inputLayer[i].weight[j] = 1.0f;
      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;
      }
      cout << "Done!" << endl;
      getch();
      return 0;
}

Is that the same thing you wrote or did I miss something?

Thanks,
-- Crash Matrix

 

by: ozoPosted on 2006-12-29 at 20:36:23ID: 18217939

Try removing the
     for (i = 0 ; i < 2 ; i++)
          for (j = 0 ; j < 2 ; j++)
               nn.inputLayer[i].weight[j] = 1.0f;

 

by: crash_matrixPosted on 2006-12-29 at 22:33:38ID: 18218155

OK, when I comment those lines out, it works:
int main(int argc, char* argv[])
{
      unsigned i,j,k;
      neuralNetwork nn(2,2,2);
/*      for (i = 0 ; i < 2 ; i++)
            for (j = 0 ; j < 2 ; j++)
                  nn.inputLayer[i].weight[j] = 1.0f;*/
      for (k = 0 ; k < 1000000 ; 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 == 999999)
                        {
                              nn.recalc();
                              cout << i << " XOR " << j << " -> " << nn.outputLayer.output << " SHOULD BE " << XOR((float)i,(float)j) << endl;
                        }
                  }
            if (k == 0 || k == 999999)
                  cout << endl;
      }
      cout << "Done!" << endl;
      getch();
      return 0;
}
Any idea why that works but the other way didn't?

-- CM

 

by: ozoPosted on 2006-12-29 at 23:28:42ID: 18218252

When all the inputLayer weights are the same, the network can't tell the difference between the inputs.

 

by: crash_matrixPosted on 2006-12-30 at 08:21:57ID: 18219165

OK; so does that mean I should do that on any problem I run on the network (problems beside XOR), or is it really just applicable to this problem? (Don't worry, I'm going to give you the points, I just want to make sure I understand the network before the question closes).

-- CM

 

by: ozoPosted on 2006-12-30 at 16:10:31ID: 18220132

You don't want to do
    for (i = 0 ; i < 2 ; i++)
          for (j = 0 ; j < 2 ; j++)
               nn.inputLayer[i].weight[j] = 1.0f;
for any problem.  nn.backpropagate(i) for example, would be impossible with all input weights the same.

 

by: crash_matrixPosted on 2006-12-30 at 16:14:48ID: 18220141

I'm sorry, I guess I didn't phrase the question right; what I meant was: Should I stagger the input weights in the same fashion as you recommended for every problem, or can they be any value as long as they're different (or is there a pattern that they should follow)?

-- CM

 

by: crash_matrixPosted on 2006-12-30 at 16:19:48ID: 18220155

Wait; I think I understand it now.  The zeroing of all but the current input of the input layer means that only 1 input gets calculated on each input layer neuron.  Consequently, I would have to have the same number of neurons on the input layer as the number of inputs.  So, neuron #1 of the input layer would only get input #1.
Is this correct?

-- CM

 

by: crash_matrixPosted on 2006-12-31 at 20:43:02ID: 18223024

Sorry for the delay (stupid airlines).  I've awarded the points to ozo for fixing the problem :)

-- CM

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...