NYCPHP Meetup

NYPHP.org

[nycphp-talk] 2D array

Carlos Hoyos cahoyos at us.ibm.com
Tue Mar 25 11:16:27 EST 2003






Multi dimensional arrays work ok in php:

instead of
> $hiddenW = array ($i => array ($n => frandom(-0.1, 0.15)));

you can use,
$hiddenW[$i][$n] = frandom(-0.1, 0.15);

The first part of code generates 2 matrices with random values. The second
one
calculates in $hiddenN the matrix product of hiddenW and inputA, and
applies the
calculates the sigmoid transform on that product (stored in hiddenA).
(for anybody remembering lineal algebra, this is just a matrix
multiplication)


You should get a numImput-fold performance improve by rewriting this:

for ($i=1; $i<=numhidden; $i++) {
 $sum2=0;
 for ($j=1; $j<=numInput; $j++) {
   // store in temp variable $sum2 the vector product (dot product)
   // of vectors  $hiddenW[$i] and $inputA
  $sum2 += $hiddenW[$i][$j] * $inputA[$j];
 }
  $hiddenN[$i]=$sum2;
  $hiddenA[$i]=sigmoid($sum2);
}



Carlos



                                                                                                                                       
                      Tracy                                                                                                            
                      <tech_learner at yah        To:       NYPHP Talk <talk at nyphp.org>                                                   
                      oo.com>                  cc:                                                                                     
                                               Subject:  [nycphp-talk] 2D array                                                         
                      03/25/2003 01:08                                                                                                 
                      AM                                                                                                               
                      Please respond to                                                                                                
                      talk                                                                                                             
                                                                                                                                       
                                                                                                                                       




Hi,
below are few lines from a PASCAL code

PROCEDURE init;
 VAR i, n:integer;
 BEGIN
  FOR i:=1 TO numhidden DO
   BEGIN
    hiddenA[i]:=frandom(0.005, 0.2);
    FOR n:=1 TO numInput DO
     hiddenW[i, n]:=frandom(-0.1, 0.15);
   END;
  FOR i:=1 TO numOutput DO
   BEGIN
    FOR n:=1 TO numhidden DO
     outputW[i, n]:=frandom(-0.1, 0.15);
   END;
 END;  { init }

the problem is i know nothing about PASCAL and there isint an elaborate
example on how to create a two-dimensional array. i am stuck at the line
hiddenW[i, n]:=frandom(-0.1, 0.15);

here's my version :
function init() {
 for ($i=1; $i<=numhidden; $i++) {
  $hiddenA[$i]=frandom(0.005, 0.2);
  for ($n=1; $n<=numInput; $n++) {
   $hiddenW = array ($i => array ($n => frandom(-0.1, 0.15)));
   print("<pre>");
   print_r($hiddenW);
   print("</pre>");
  }
 }
 print("<pre>");
 print_r($hiddenA);
 print("</pre>");
 print_r($hiddenW);
 for ($i=1; $i<=$numOutput; $i++) {
  for ($n=1; $n<=numhidden; $n++) {
   //$outputW[$i, $n]=frandom(-0.1, 0.15);
  }
 }
} // { init }
it seems to dump values... i am unsure if its right.
i need the values elsewhere...
for ($i=1; $i<=numhidden; $i++) {
 $sum2=0;
 for ($j=1; $j<=numInput; $j++) {
  $sum2=$sum2 + $hiddenW[$i, $j] * $inputA[$j]; //at this point i am
totally blank
  echo $sum2;
  $hiddenN[$i]=$sum2;
  $hiddenA[$i]=sigmoid($sum2);
 }
}
has someone done this sort of programming before. there sure must be a
better way to code this procedure out. plz help
Thz
Tracy


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Coming together is a beginning...
   keeping together is progress...
      working together is success !!!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


---------------------------------
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!


--- Unsubscribe at http://nyphp.org/list/ ---








More information about the talk mailing list