【问题标题】:F# function takes too many arguments or used in a context not expectedF# 函数采用太多参数或在不期望的上下文中使用
【发布时间】:2016-03-05 03:04:33
【问题描述】:

我正在尝试实现一个成本函数,我目前有

let computeCost (X : Matrix<double>) (y : Vector<double>) (theta : Vector<double>) =
    let m = y.Count |> double
    let J = (1.0/(2.0*m))*(((X*theta - y) |> Vector.map (fun x -> x*x)).Sum)
    J

由于某种原因,我在第一个 * 之后的一半出现错误,说“此函数需要太多参数,或者在不需要函数的上下文中使用。”

但是,当我这样做时

let computeCost (X : Matrix<double>) (y : Vector<double>) (theta : Vector<double>) =
    let m = y.Count |> double
    let J = (((X*theta - y) |> Vector.map (fun x -> x*x)).Sum)
    J

它工作得很好,它说val J:float 这是我所期望的。但是只要添加第二部分,即(1.0/(2.0*m)) 部分,我就会得到错误。我对所有内容都有括号,所以我看不出它是如何应用一些部分功能或类似的东西。我确定这很愚蠢,但我似乎无法弄清楚。

【问题讨论】:

    标签: matrix f# mathnet-numerics


    【解决方案1】:

    没关系,我很笨,我又回到了我的 C# 使用 .Sum() 的方式中,实际的使用方式是

    let computeCost (X : Matrix<double>) (y : Vector<double>) (theta : Vector<double>) =
        let m = y.Count |> double
        let J = (1.0/(2.0*m)) * (((X*theta - y) |> Vector.map (fun x -> x*x)) |> Vector.sum)
        J
    

    这似乎解决了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      相关资源
      最近更新 更多