etiquette (sp?) of posting code

This is a discussion on etiquette (sp?) of posting code within the C++ Programming forums, part of the General Programming Boards category; hiya i have some work to do for a uni assignment in c++ (neural networks), and there is a bug ...

  1. #1
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20

    etiquette (sp?) of posting code

    hiya

    i have some work to do for a uni assignment in c++ (neural networks), and there is a bug in it which is causing silly values and i have NO idea why, or what the problem is. what would be more advisable:

    a) posting the function which is causing the problem and explaining what goes on

    b) posting the entire code (two .cpp and one .h file)

    c) posting the function and linking to a zip with the code


    im assuming c, but i can imagine how annoying it is for you guys to be asked about a huge project. i do have a small design/psuedocode doc which explains what is going on, but im sure that it is just a little error causing big problems.

    cheers.

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Make a backup copy of the code, then start removing stuff which you think doesn't matter until either the problem goes away or changes into another problem.

    Study what you have left and if you still can't work it out, post what you have reduced your problem to.

  3. #3
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    thats the problem, all i have is the bare minimum code for what i need to do. Basically, i got given 3 files, one .cpp file which contains main and handles the input from the user, one header file which contains the neural network class definition, and another .cpp file which contains the neuron class definition, and all functions associated with both the neuron class and the neural network class.

    all i need to add is a Learn function to the neural network class, and thats all i have done (aswell as update the neuron and neural network class as needed). it *should* be working, as i get no compile errors and, unfortunately, it only breaks sometimes. this is why im so stumped!

    thanks
    ( )

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    When it breaks, what is running then in the code? Post just that part that gives the error; if people do not see the error in it they will ask for more.

  5. #5
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    alright, well you asked for it :P

    Code:
    void MLP::Learn(int numegs, double testset[], double lrate)
    {
    	int ct=0;
    	int i=0;
    	int egct=0;
    	double sserr=0;
    	double * outputs = new double [numegs];
    
    	LinActNode ** Inputs = NULL;
    	int NumInputs = 0;
    	double * Weights = NULL;
    	double * Invec = NULL;
    
    	for (ct=0; ct<NumOutput; ct++)
    	{ 
    		i = NumInput+ct;	//use i rather than that expression, space saving
    		//Nodes[i]->blah;
    
    		Inputs = Nodes[i]->GetInputs();
    		NumInputs = Nodes[i]->GetNumInputs();
    
    		Weights = new double [NumInputs];
    
    		Invec = new double [NumInputs];
    
    
    
    		for (egct=0; egct<numegs; egct++)
    		{
    			cout << "Example " << egct << endl;
    
    			//Load the input nodes with the example set egct
    			Calculate (&testset[egct*3], outputs);
    
    			//calculate the delta value of the output nod and store it
    			Nodes[i]->SetDelta( testset[egct*3+2]  -  Nodes[i]->NodeOutput() );
    			cout << "Delta set to: " << Nodes[i]->GetDelta() << endl;
    
    			//get the current weights for the input to this node
    			Weights = Nodes[i]->GetWeights();
    
    			//Invec[] is the array of values getting passed into the node
    			//invec 0 is the bias term
    			Invec[0] = Weights[0];
    			cout << "Invec[0] = " << Invec[0] << endl;
    			for (int vecct=1; vecct<=NumInputs; vecct++)
    			{
    				//calculate invect as (output from all input nodes) * (weight associated)
    				Invec[vecct] = Inputs[vecct-1] -> NodeOutput()  *  Weights[vecct];
    				//cout << "Inputs[" << vecct-1 << "]->NodeOutput() = " <<  Inputs[vecct-1]->NodeOutput() << endl;
    				cout << "Invec[" << vecct << "] = " << Invec[vecct] << endl;
    			}
    
    			for (int wct=0; wct<=NumInputs; wct++)
    			{
    				//update the weights for the node
    				Weights[wct] = Weights[wct] + ( lrate * Invec[wct] * Nodes[i]->GetDelta() );
    				cout << "Weights["<<wct<<"] = " << Weights[wct] << endl;
    			}
    
    			//set the weights for the node
    			Nodes[i]->SetWeights(Weights);
    
    			cout << endl;
    
    		}
    
    
    	}
    
    }

    i understand that it probably wont make much sense but any offers you could give as to why it spews out -1.#IND after i call that a few times would be appreciated (it accumulates to that value, numbers getting increasingly large or small).

    thanks a lot.
    ( )

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Not seeing you delete weights and Invec, probably giving you a heap overflow after u call it a few times.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    outputs need to be deleted as well. call delete [] outputs to delete an array.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    cheers, will give it a go
    ( )

  9. #9
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    back again;

    Code:
    
    			//set the weights for the node
    			Nodes[i]->SetWeights(Weights);
    
    			cout << endl;
    
    		}
    
    	}
    
    	delete [] outputs;
    	cout << "Deleted outputs[]" << endl;
    
    }
    thats the last few lines of the function. I tried deleting Weights, Invec, and Inputs, but they threw errors at me (runtime, rather than at compile etc).

    Im fairly sure I dont need to delete Weights, as the function

    Code:
    Nodes[i]->GetWeights();
    returns a pointer, so i have also removed the line which assigned memory to Weights (Weights = new double [NumInputs]. that is correct, yes? (same goes for Inputs).

    now, i think Invec needs to be deleted as aside from outputs, it is the only other place where memory has been assigned. yet, when i try to call:

    Code:
    delete [] Invec;
    at the end, i get the runtime error which spews some funny messages at me!

    im going to attatch the files to this post so you can take a look if you like. i dont blame ya if you dont, but this is really helping me. i havent really got anyone to talk the problem through with at the moment (this doesnt have to be in for months, im getting a head start), and talking a problem out is awesome.

    files are attatched, thanks again.
    Attached Files Attached Files
    ( )

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    What compiler are you using? As you say you get "-1.#IND".
    You should be able to find in the compiler documentation or on the internet all the possible cases when this output happens so you can possibly narrow it down even more.
    Last edited by Enahs; 11-10-2005 at 02:40 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    c) posting the function and linking to a zip with the code
    Personally, I won't download anything from a non-trusted source.

    I see this in your code:

    i = NumInput+ct;

    but I don't see NumInput changing anywhere. You have another variable named NumInputs, are they separate variables or is that a mistake? Is Nodes[i] out of bounds?
    Last edited by 7stud; 11-10-2005 at 03:25 PM.

  12. #12
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    enahs:

    thanks for the advice, im using visual studio.net 2003. I have a feeling that i have a float which is getting ridiculously large or ridiculously small (ive seen it happening, so i know its this)... but crap knows how its happening. il keep a check on msdn for -1.#IND, though.


    7stud:

    dont worry about not downloading it, no worries =) the plain cpp, h, and txt files are above if you just dont want a zip, though.

    NumInput and NumInputs are separate variables (not by my choice, but unfortunately i cant change the coursework skeleton ). Nodes[i] isnt going out of bounds - its not an instantaneous error and all the checks which i have placed indicate that codewise everything is perfectly in bounds and legal. im not so used to c++, though, so i could be well wrong =)
    ( )

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I would do one of two things:

    1) Learn to use your debugger. That way you can watch what is happening to all the variables as you step through your program.

    2) Do it the old fashioned way: get a piece of paper and write all your variable names across the top of the page. Then go through your function and write down the new values for the variables as they change until you see what's going wrong.

  14. #14
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    *sulk*

    i think you have a fair point. I'll let you know how it goes.
    ( )

  15. #15
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You say it does happen the first time, but after a few calls to the function?

    Can you run the program, make the call to the function with certain variables, make it again with other variables, and repeat till you get the error.
    Can you then do it again, passing the same exact variables as before and get the error at the same place?

    If so, then change your double in the function to floats (less precision) and then retry again, if you get the error quicker…good! Then go back and make everything double, and one by one make one variable a float, run it and see where the error comes out at. If at the same point as when they where all double or the same point when they where all float; well, if that one errors out when it is a float it is probably your problem.

    That is one way to TRY and narrow down the problem even more.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21