Page 1 of 1

Code check requested

Posted: 2004-02-15 03:54pm
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                                        *
*************************************************************/

Re: Code check requested

Posted: 2004-02-15 07:19pm
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.

Posted: 2004-02-16 01:25am
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?

Posted: 2004-02-16 01:32am
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.

Posted: 2004-02-16 01:36am
by kojikun
neato. okay, thanks guys.

Posted: 2004-02-18 11:18pm
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?

Posted: 2004-02-18 11:46pm
by Darth Yoshi
I don't think so. You'd have to alter the second variable within the loop code itself.

Posted: 2004-02-18 11:53pm
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)

Posted: 2004-02-19 02:27am
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]

Posted: 2004-02-19 02:55am
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.


Posted: 2004-02-19 08:06am
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:

Posted: 2004-02-19 09:40am
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.

Posted: 2004-02-19 06:10pm
by kojikun
I said the original junk should work for c++ :P

Posted: 2004-02-19 06:13pm
by Dahak
kojikun wrote:I said the original junk should work for c++ :P
It wouldn't be so perfect in C++... :)