简体   繁体   English

在 J 中创建 y 形状随机浮点数组

[英]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:我正在尝试创建 y 形随机浮点数组,这是我现在的当前:

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:似乎这段代码返回了我真正想要的,所以我尝试制作一个 function,如下所示:

rand =: 0 ?@$ ~

but rand (input_dim, hidden_dim) gives me a syntax error...但是rand (input_dim, hidden_dim)给了我一个语法错误......

I think I am missing one very important part, but I am not sure what that is.我想我错过了一个非常重要的部分,但我不确定那是什么。

Any advice would be grateful!任何建议将不胜感激!

Without the argument, the syntax of 0?@$ ~ is ambiguous and the interpreter missclassifies the parenthesization (or, more accurately, the correct parenthesization is not the one you think it is).没有参数, 0?@$ ~的语法是模棱两可的,解释器错误分类括号(或者,更准确地说,正确的括号不是你认为的那个)。 The easiest way around this is to define rand as:解决这个问题的最简单方法是将rand定义为:

rand =: 3 :'0 ?@$ ~ y'

Of course, any other way of removing the syntactic ambiguity will also work:当然,任何其他消除句法歧义的方法也可以:

rand =: [: ? 0 $~ ]
rand =: ?@(0$~])
rand =: ?@(0&($~))
...

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.可能你的困惑是因为你想创建一个形式的分支(noun verb verb) ,但是~是一个副词,因此与它左边的动词结合起来创建一个新动词 (在你的情况下?@$~ ) 所以您的rand具有 J 无法识别的(0?@$~)(noun verb)形式 - 因此出现语法错误。

It makes sense to use the combination ?@$ if possible because it is supported by special code and does not create x $ y .如果可能,使用组合?@$是有意义的,因为它由 特殊代码支持并且不会创建x $ y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM