Search code examples
j

Creating y shape random float array in J


I am trying to creating y shape random float array, and this is my current right now:

input_dim =: 2
hidden_dim =: 16

0 ?@$ ~ (input_dim, hidden_dim) 

0.838135  0.96131 0.766721 0.420625 0.640265 0.683779 0.683311 0.427981 0.281479 0.305607 0.385446 0.898389  0.24596 0.452391 0.739534 0.973384
0.914155 0.172582 0.146184 0.624908 0.333564 0.132774 0.475515 0.802788 0.277571 0.146896  0.40596 0.735201 0.943969 0.259493 0.442858 0.374871

It seems like this code returns what I exactly want, so I tried to make a function like below:

rand =: 0 ?@$ ~

but rand (input_dim, hidden_dim) gives me a syntax error...

I think I am missing one very important part, but I am not sure what that is.

Any advice would be grateful!


Solution

  • The only thing missing from your verb is ]. That is:

       rand =: 0 ?@$~ ]
       rand 2 3
    0.891663 0.888594 0.716629
      0.9962 0.477721 0.946355
    

    Potentially your confusion arose because you were wanting to create a fork of the form (noun verb verb), however ~ is an adverb and so combines with the verb to its left to create a new verb (in your case ?@$~) so your rand had the form (0 ?@$~) or (noun verb) which J does not recognise - hence the syntax error.

    It makes sense to use the combination ?@$ if possible because it is supported by special code and does not create x $ y.