【发布时间】:2017-01-23 21:24:41
【问题描述】:
我正在尝试将一些 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(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 ]])
代码用于Neural Networks and Deep Learning,由于性能原因需要可变值,因此不能选择使用不可变列表。
【问题讨论】:
-
你需要使用 DU
-
可能有与 DU 不同的解决方法:member self.differntarrays ([
] dimensions: Object[]) : Object[] -- 注意它返回 Object[]。这可能会导致下游出现问题,因此尚未提交。