【问题标题】:Using Scala Breeze numerics results in error: not enough arguments for method apply使用 Scala Breeze 数值会导致错误:方法应用的参数不足
【发布时间】:2017-09-13 21:29:47
【问题描述】:

我正在使用 Scala Breeze 开发一个简单的分类算法。我想使用 Breeze 数字将不同的函数应用于我正在使用的矩阵,特别是 sigmoid 和 tanh。单独使用 sigmoid 很好,但我想通过将函数指定为字符串参数然后在方法中使用它来选择 sigmoid 或 tanh。我试过了:

// this code works
import breeze.numerics._
val H: BDM[Double] = sigmoid((M(::,*) + bias).t)

// this code throws an error
def activationFuncBreeze(af: String) = af match {
  case "sigmoid" => sigmoid
  case "tanh" => tanh
}
val H: BDM[Double] = activationFuncBreeze(af)((M(::,*) + bias).t)

错误是

Error:(60, 50) could not find implicit value for parameter impl: breeze.generic.UFunc.UImpl[_13.type,breeze.linalg.DenseMatrix[Double],VR]
val H: BDM[Double] = activationFuncBreeze(af)((M(::,*) + bias).t)

下一个错误是:

Error:(60, 50) not enough arguments for method apply: (implicit impl: breeze.generic.UFunc.UImpl[_13.type,breeze.linalg.DenseMatrix[Double],VR])VR in trait UFunc.

未指定值参数 impl。 val H: BDM[Double] = activationFuncBreeze(af)((M(::,*) + bias).t)

我查看了 Breeze numerics 并实现了 UFuncs,但 Breeze.numerics 库中已经存在 sigmoid 和 tanh 作为元素 UFuncs,所以我应该能够轻松使用它们。任何建议表示赞赏

【问题讨论】:

    标签: scala scala-breeze


    【解决方案1】:

    将矩阵带入函数似乎可行:

    def calculateH(af: String): BDM[Double] = af match {
      case "sigmoid" => sigmoid((M(::, *) + bias).t)
      case "tanh" => tanh((M(::, *) + bias).t)
      case "sin" => sin((M(::, *) + bias).t)
      case _ => throw new IllegalArgumentException("Activation function must be sigmoid, tanh, sin")
    }
    
    val H = calculateH(af)
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      相关资源
      最近更新 更多