Sometimes starting from simple examples allows you to figure out what is
happening.

a=:7
   b=:11
   +/"1 (3 2 1) *"1 a (+,-,*)"0 b
123
   +/(3 2 1) * a (+,-,*) b
123
   
   a=:7 12
   b=:11 5
   +/"1 (3 2 1) *"1 a (+,-,*)"0 b
123 125
   +/(3 2 1) * a (+,-,*) b
|length error
|   +/(3 2 1)    *a(+,-,*)b
   
       a (+,-,*)"0 b
18 _4 77
17  7 60
   a (+,-,*) b
18 17 _4 7 77 60
   
   
   (3 2 1) *"1 a (+,-,*)"0 b
54 _8 77
51 14 60
   (3 2 1) * a (+,-,*)"0 b
|length error
|   (3 2 1)    *a(+,-,*)"0 b
   
      
   +/"1 (3 2 1) *"1 a (+,-,*)"0 b
123 125
   +/ (3 2 1) *"1 a (+,-,*)"0 b
105 6 137
   
I find it easier to get what I want and then add names to help others
understand why I might want that expression rather than some other one.
Linda


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Raul Miller
Sent: Friday, March 25, 2011 2:08 PM
To: Adrian May
Cc: Programming forum
Subject: Re: [Jprogramming] Problem with random numbers

On Fri, Mar 25, 2011 at 11:14 AM, Adrian May
<[email protected]> wrote:
>> If I understand correctly, the sum you want is:
>>   (weightlearn y) + (jumblerate@rand w y) + w y
>>
>
> In C syntax, I want
> new_w =
>     learnrate*learn(old_w, old_i, old_o, new_i, new_o )
>     + jumblerate * random_matrix_of_-1s_and_1s
>     + rememberreate * old_w
>
> where the rates are constants. My idea was to put the three functions in a
> list and sum over them, but I guess that's the Haskell talking. I did
kinda
> want to put the rates into some tidy place, but it's not essential.

So.. ok... I do not have a definition for learn, yet.  But I believe
that old_w is a matrix of weights, so new_w should also be a matrix of
weights.  And learnrate, jumblerate and rememberrate I expect to all
be scalars (just single numbers with no dimensions).

So... I am going to guess
learn=: */~&>/@((avgio io)~)@think

And, for the random matrix of _1 and 1:
jumble=: _1 1 {~ ? bind (2 2$2)

And, some rates:
  learnrate=: 0.3&[
  jumblerate=: 0.2&[
  rememberrate=: 0.5&[

That said, I am going to be careful to construct my sentences so you
could use the more simplistic
  learnrate=: 0.3
  jumblerate=: 0.2
  rememberrate=: 0.5

This leaves me with:
   ((learnrate * learn) + (jumblerate * jumble) + (rememberrate * w)) seed

With this structure, using a sum might be meaningful:

   rates=: learnrate,jumblerate,rememberrate
   weights=: learn, jumble,: w
   +/ (rates * weights) seed
or
   (rates +/ .* weights) seed

Note that when I combined arrays, I needed to be careful to indicate
which dimensions were "content dimensions" and which dimensions were
"container dimensions".  Thus, I combine the last pair with ,: (which
introduces a new dimension) and the rest of the arrays are introduced
using , (which does not introduce a new dimension).

>> But if by sum you meant +/ I am not sure that this is a good idea.  +/
>> would make sense if you had a reason for these three things to be in a
>> list already, but since you are computing each value independently,
>> forming a list form them seems like a meaningless operation.
>
> That seems like a funny thing to say. I thought J loved making lists.

It depends on what you are doing.  For example, you can do:
      1 + 2 + 3
or you can do
   +/ 1 , 2 , 3

If the only thing you are going to do with the items in that array is
add them up, it's simpler to just add them when combining them.


> I'd like to get it working both ways just as a learning exercise. My
program
> works already but it bugs me that I can't seem to get this function list
> working when it clearly should work in principle given that I already made
> it half work out of context. I got this far:
>
>    a    +/@((3 2 1)&*)"2@(+,-,*)"0 b
> 15 28
> 43 60
>
> but when I try to factor out the (3 2 1)&* it barfs.

Note that binary operations can only combine two values, so you would
need to combine a and b into a list before you can factor out 3 2 1.

   +/@((3 2 1)&*)"2@(+,-,*)"0/ a,:b

Then you can take advantage of the fact that you are trying to find an
inner product, and:

   3 2 1 +/ .*"1 (+,-,*)"0/ a,:b

or, rather than constructing an array of the results of (+,-,*) we
could instead assemble those three results into an array like this:

   3 2 1 +/ .* (+,-,:*)/ a,:b

And, since we have factored out 3 2 1 and we do not need a and b in a list:

   3 2 1 +/ .* a (+,-,:*) b
or
   +/ 3 2 1 * a (+,-,:*) b

But there might be simpler ways of getting here...

And, now that I think I understand what you were trying to do, I see
that you could also have done:

   +/"1 (3 2 1) *"1 a (+,-,*)"0 b

FYI,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to