我正在尝试将一些 Python 转换为 F#,特别是numpy.random.randn.
该函数采用可变数量的 int 参数,并根据参数的数量返回不同维度的数组。
我相信这是不可能的,因为不能有一个返回不同类型的函数(int[]
, int[][]
, int[][][]
等),除非他们是受歧视工会的一部分,但希望在采取解决方法之前确定。
健全性检查:
member self.differntarrays ([<ParamArray>] dimensions: Object[]) =
match dimensions with
| [| dim1 |] ->
[|
1
|]
| [| dim1; dim2 |] ->
[|
[| 2 |],
[| 3 |]
|]
| _ -> failwith "error"
导致错误:
This expression was expected to have type
int
but here has type
'a * 'b
与expression
存在 :[| 2 |], [| 3 |]
和int
指的是1中的[| 1 |]
即类型1
与 的类型不同[| 2 |], [| 3 |]
TLDR;
numpy.random.randn
numpy.random.randn(d0, d1, ..., dn)
从“标准正态”分布返回一个(或多个)样本。
如果提供正数、int_like 或 int-convertible 参数,则 randn
生成形状为 (d0, d1, ..., dn) 的数组,并随机填充
从单变量“正态”(高斯)分布中采样的浮点数
均值 0 和方差 1(如果任何 d_i 是浮点数,则它们首先
通过截断转换为整数)。随机采样的单个浮点数
如果未提供参数,则从分布中返回。
交互式 python 会话的示例:
np.random.randn(1) - array([-0.28613356])
np.random.randn(2) - array([-1.7390449 , 1.03585894])
np.random.randn(1,1)- array([[ 0.04090027]])
np.random.randn(2,3)- array([[-0.16891324, 1.05519898, 0.91673992],
[ 0.86297031, 0.68029926, -1.0323683 ]])
代码是为了神经网络和深度学习并且由于出于性能原因这些值需要可变,因此不能选择使用不可变列表。