1

I'm trying to build a multi-layer perceptron network in c++ using visual studio 2013. First I create a struct for the neutrons of the first layer but when I run it gives me a stack overflow error. This is my code:

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <fstream>
#include <vector>

// Number of Neurons
#define INPUTS 784
#define INPNEUR 784
#define HIDNEUR 500
#define OUTNEUR 10

struct inpneuron {
int number;
double weights[INPUTS];
double bias;
double inputs[INPUTS];
double output;
double delta;
};

typedef struct inpneuron InpNeuron;

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i, j, x[INPUTS];

InpNeuron InputLayer[INPNEUR];

system("PAUSE");

return 0;
}

And the exception it gives is: Unhandled exception at 0x00302CB7 in Multi Layer Perceptron MNIST.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00522000).

I have very little experience in programming but I think it's a memory problem because when I decrease the number of neurons or inputs it works. Is there any way I can make it work with this number of neurons and inputs?

3 Answers3

2

This line

 InpNeuron InputLayer[INPNEUR];

allocates a huge amount of memory on the stack, more than is available by default. You should use a std::vector instead:

 std::vector<InpNeuron> InputLayer(INPNEUR);

to allocate the necessary memory safely from the heap.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
0

Your "stack-allocated data structure" is simply too large. Please read about Stack Overflow causes and the difference between the stack and the heap.

Then try:

int _tmain(int argc, _TCHAR* argv[])
{
   int i, j, x[INPUTS];

   InpNeuron * InputLayer = new InpNeuron[INPNEUR];

   system("PAUSE");

   delete[] InputLayer;
   return 0;
}

That gets you to the basic "a-ha" of the difference between where stack and heap allocations are done. But after you've absorbed the point and read a bit, you need to read about std::vector and ways to avoid raw pointer memory management...as these are much safer ways to work in C++.

Community
  • 1
  • 1
0

Well your program seems to be too big to fit into the stack. A few suggestions:

1- Move to linux, it gives you more freedom on the size of the stack.

2- Use floats instead of doubles, they require less resources and will give you enough of accuracy for training.

3- Since you're not really skilled in programming, maybe you can use some already created libraries like Caffe? You can either implement your own network on Caffe very easily, or use some of their pre-implemented networks for MNIST there; They have implementations of LeNet and many other famous networks.

4- After reading the other answers: Use the heap! Using pointers instead, but you need to watch out for your memory management!

Best of luck

AbdulRahman AlHamali
  • 1,681
  • 1
  • 11
  • 18