从一个函数返回不同维数的数组;在F#中有可能吗? [英] Returning arrays of different dimensions from one function; is it possible in F#?

查看:50
本文介绍了从一个函数返回不同维数的数组;在F#中有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些Python转换为F#,特别是 numpy.random.randn .

I am trying to convert some Python to F#, specifically numpy.random.randn.

该函数采用可变数量的int参数,并根据参数的数量返回不同维的数组.

The function takes a variable number of int arguments and returns arrays of different dimensions based on the number of arguments.

我相信这是不可能的,因为除非它们是已区分联合的一部分,否则不能拥有返回不同类型(int[]int[][]int[][][]等)的函数,但是要确保在采取替代方法之前.

I believe that this is not possible because one cannot have a function that returns different types (int[], int[][], int[][][], etc.) unless they are part of a discriminated union, but want to be sure before committing to a workaround.

健全性检查:

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 |]

with the expression being : [| 2 |], [| 3 |]
and the int referring to the 1 in [| 1 |]
i.e. the type of 1 is not the same as the type of [| 2 |], [| 3 |]

TLDR;

numpy.random.randn

numpy.random.randn(d0,d1,...,dn)

numpy.random.randn(d0, d1, ..., dn)

从标准正态"分布中返回一个或多个样本.

Return a sample (or samples) from the "standard normal" distribution.

如果提供了肯定的,类似int_like或int可转换的参数,则randn 生成形状为(d0,d1,...,dn)的数组,并填充随机 从单变量正态"(高斯)分布中采样的浮点数 均值0和方差1(如果d_i中的任何一个为浮点数,则它们为第 通过截断转换为整数).单个浮标随机采样 如果未提供任何参数,则从分发中返回.

If positive, int_like or int-convertible arguments are provided, randn generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate "normal" (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.

来自交互式python会话的示例:

Examples from interactive python session:

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 ]])

该代码适用于神经网络和深度学习,并且由于性能原因,这些值需要可变,则不能使用不可变列表.

The code is for Neural Networks and Deep Learning and since the values need to mutable for performance reasons, using immutable list is not an option.

推荐答案

您是正确的-浮点数数组float[]是与浮点数数组不同的类型
float[][]或浮点数float[,]的2D数组,因此您不能编写根据输入参数返回一个或另一个的函数.

You are correct - an array of floats float[] is a different type than array of arrays of floats
float[][] or 2D array of floats float[,] and so you cannot write a function that returns one or the other depending on the input argument.

如果您想执行Python的rand之类的操作,则可以编写一个重载方法:

If you wanted to do something like the Python's rand, you could write an overloaded method:

type Random() = 
  static let rnd = System.Random()
  static member Rand(n) = [| for i in 1 .. n -> rnd.NextDouble() |]
  static member Rand(n1, n2) = [| for i in 1 .. n1 -> Random.Rand(n2) |]
  static member Rand(n1, n2, n3) = [| for i in 1 .. n1 -> Random.Rand(n2, n3) |]

这篇关于从一个函数返回不同维数的数组;在F#中有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆