Code check requested

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Code check requested

Post by kojikun »

Can someone check this code and see if there's anything wrong with it? The purpose is for it to run through a set of neurons and calculate their present state, based on the state of neurons in the previous layer and the associated weight for that input. Language is Java, but it works for c++ as well.

Code: Select all

/**********************************************************
*      Variable definitions for outputs and weights.      *
**********************************************************/

/*
ns is the array that holds neuron states by layer
and neuron. the first bracket set is the layer
number, the second is the neuron number.
*/

double ns[][] = new double[10][10];

/*
nw is the array that holds the neuron input weights.
the first two bracket sets function the same as in
no, the third determines which input neuron is being
specified, and the number contained is the weight
for that input.
*/

double nw[][][] = new double[10][10][10];

/******************************************************
*       Code for the actual neuronal functions.       *
******************************************************/

// o: output neuron selector or previous layer neuron selector
int o = 0;

// n: present neuron selector
int n = 0;

// l: layer selector
int l = 1;

// runs through the layers
while (l < 10)
{
	// runs through the neurons
	while (n < 10)
	{
		// runs through the output neuron weights
		while (o < 10)
		{
			// sets the state for the current neuron (l,n) as the current state
			// plus one tenth the product of the output for neuron (l-1,o), the
			// weight for that neuron (l,n,o)
			ns[l][n] = ns[l][n] + (ns[l-1][o] * nw[l][n][o] * 0.1);

			// increments o so the while (o < 10) loop runs down
			o = o++;
		}

		// increments n so the while (n < 10) loop runs down
		n = n++;

		// sets o to 0 so that the next implementation of the while (o < 10) loop
		// actually functions. otherwise o would equal 9 and the loop would not loop
		o = 0;
	}

	// increments l so the while (l < 10) loop runs
	l = l++;

	// sets n to 0
	n = 0;
}

/*************************************************************
* summary to present:                                        *
* :: definitions for the properties are set (ns, and nw)     *
* :: output, layer, and present neuron selectors are set     *
* :: while loops go through each output neuron, summing the  *
*    present ns with 1/10th the product of the output of the *
*    selected neuron and the associated weight, through each *
*    neuron and layer                                        *
*************************************************************/
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
Xon
Sith Acolyte
Posts: 6206
Joined: 2002-07-16 06:12am
Location: Western Australia

Re: Code check requested

Post by Xon »

Code: Select all

n = n++
This might end up giving you problems(If nothing else for readability).

IIRC This pushes the existing value of 'n' into a tmp variable, then increases the variable by one, and then stores the tmp variable into 'n'. Which is not what you want.


Just:

Code: Select all

n++
will increase the variable 'n' by one. However using:

Code: Select all

++n
Is more efficient as no tmp vars are created.

Also you should be using for loops unless there is a clear reason you dont want to use them. Dont go rolling you own version of the stuff biult into the compiler, it takes longer and is bug prone, Unless you have a good reason.

Code: Select all

for( int l = 1; l < 10; ++l )
  for ( int n = 0; n < 10; ++n )
    for( int o = 0; o < 10; ++o )
      ns[l][n] = ns[l][n] + (ns[l-1][o] * nw[l][n][o] * 0.1); 
If I'm reading this correctly, the outermost loop ('l' control variable), will only loop 9 times, instead of 10.
"Okay, I'll have the truth with a side order of clarity." ~ Dr. Daniel Jackson.
"Reality has a well-known liberal bias." ~ Stephen Colbert
"One Drive, One Partition, the One True Path" ~ ars technica forums - warrens - on hhd partitioning schemes.
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Post by kojikun »

okay, brilliant. i kiss you!

::kiss!:: :P

quick question though: wouldnt a for-loop increment the variable BEFORE running the contents of the loop?
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
Sarevok
The Fearless One
Posts: 10681
Joined: 2002-12-24 07:29am
Location: The Covenants last and final line of defense

Post by Sarevok »

kojikun wrote:okay, brilliant. i kiss you!

::kiss!:: :P

quick question though: wouldnt a for-loop increment the variable BEFORE running the contents of the loop?
No. The counter is incremented after the first iteration is complete.
I have to tell you something everything I wrote above is a lie.
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Post by kojikun »

neato. okay, thanks guys.
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Post by kojikun »

code check again please:

is this possible:

for (i = 0; i < 10; ++i; ++o)

can you increment two variables in the for loop arguments?
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
Darth Yoshi
Metroid
Posts: 7342
Joined: 2002-07-04 10:00pm
Location: Seattle
Contact:

Post by Darth Yoshi »

I don't think so. You'd have to alter the second variable within the loop code itself.
Image
Fragment of the Lord of Nightmares, release thy heavenly retribution. Blade of cold, black nothingness: become my power, become my body. Together, let us walk the path of destruction and smash even the souls of the Gods! RAGNA BLADE!
Lore Monkey | the Pichu-master™
Secularism—since AD 80
Av: Elika; Prince of Persia
User avatar
Mad
Jedi Council Member
Posts: 1923
Joined: 2002-07-04 01:32am
Location: North Carolina, USA
Contact:

Post by Mad »

kojikun wrote:code check again please:

is this possible:

for (i = 0; i < 10; ++i; ++o)

can you increment two variables in the for loop arguments?
Try:

Code: Select all

for (i = 0; i < 10; ++i, ++o)
Later...
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Post by kojikun »

hey, thanks guys. i got it fixed up nice and good. i eventually realized that i didnt need that junk. heh. :P

here we go, the final version of the neuron class, no comments:

Code: Select all

public class Neuron
{

 private double nsa[][] = new double[10][10];

 private double nsb[][] = new double[10][10];

 private double nw[][][] = new double[10][10][10];

 public void synapser()
 {
  for (int l = 1; l < 10; ++l)
  {
   for (int n = 0; n < 10; ++n)
   {
    for (int o = 0; o < 10; ++o)
     nsb[l][n] = nsb[l][n] + (nsa[l-1][o] * nw[l][n][o] * 0.1);
   }
  }
 }

 public void updater()
 {
  for (int l = 1; l < 10; ++l)
  {
   for (int n = 0; n < 10; ++n)
   {
    if (nsb[l][n] > 2)
     nsa[l][n] = 2;
    else if (nsb[1][n] > 1)
     nsa[l][n] = nsb[l][n];
    else
     nsa[l][n] = 0;
   }
  }
 }

}
[/url]
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
Sarevok
The Fearless One
Posts: 10681
Joined: 2002-12-24 07:29am
Location: The Covenants last and final line of defense

Post by Sarevok »

Kojikun you have a bad coding style. You class should look like this.

Code: Select all


class Neuron
{

public :

inline void sysanpder();

inline void updater();

private :

double nsa[][] = new double[10][10];

double nsb[][] = new double[10][10];

double  nw[][] = new double[10][10][10];

};

Some things you forgot while writting this class.

Where is the constructor in this class ? You should initialize member variables in the constructor.

Where is the destructor in this class ? You allocating dynamic memory and yet not freeing it.

Why are you using inline functions ? Only small and simple functions be inline since inline functions memory intensive.

I have to tell you something everything I wrote above is a lie.
User avatar
Xon
Sith Acolyte
Posts: 6206
Joined: 2002-07-16 06:12am
Location: Western Australia

Post by Xon »

evilcat4000 wrote: Some things you forgot while writting this class.

Where is the constructor in this class ? You should initialize member variables in the constructor.

Where is the destructor in this class ? You allocating dynamic memory and yet not freeing it.

Why are you using inline functions ? Only small and simple functions be inline since inline functions memory intensive.
From the original post:
Language is Java
Which answers most(if not all) your questions :lol:
"Okay, I'll have the truth with a side order of clarity." ~ Dr. Daniel Jackson.
"Reality has a well-known liberal bias." ~ Stephen Colbert
"One Drive, One Partition, the One True Path" ~ ars technica forums - warrens - on hhd partitioning schemes.
User avatar
Sarevok
The Fearless One
Posts: 10681
Joined: 2002-12-24 07:29am
Location: The Covenants last and final line of defense

Post by Sarevok »

ggs wrote:
evilcat4000 wrote: Some things you forgot while writting this class.

Where is the constructor in this class ? You should initialize member variables in the constructor.

Where is the destructor in this class ? You allocating dynamic memory and yet not freeing it.

Why are you using inline functions ? Only small and simple functions be inline since inline functions memory intensive.
From the original post:
Language is Java
Which answers most(if not all) your questions :lol:
Damn it. I thought it was C++. :D Kojkun did say it works for C++ as well however.
I have to tell you something everything I wrote above is a lie.
User avatar
kojikun
BANNED
Posts: 9663
Joined: 2002-07-04 12:23am
Contact:

Post by kojikun »

I said the original junk should work for c++ :P
Sì! Abbiamo un' anima! Ma è fatta di tanti piccoli robot.
User avatar
Dahak
Emperor's Hand
Posts: 7292
Joined: 2002-10-29 12:08pm
Location: Admiralty House, Landing, Manticore
Contact:

Post by Dahak »

kojikun wrote:I said the original junk should work for c++ :P
It wouldn't be so perfect in C++... :)
Image
Great Dolphin Conspiracy - Chatter box
"Implications: we have been intercepted deliberately by a means unknown, for a purpose unknown, and transferred to a place unknown by a form of intelligence unknown. Apart from the unknown, everything is obvious." ZORAC
GALE Force Euro Wimp
Human dignity shall be inviolable. To respect and protect it shall be the duty of all state authority.
Image
Post Reply