Using Scala Breeze numerics results in error: not enough arguments for method apply

338 Views Asked by At

I am developing a simple classification algorithm using Scala Breeze. I would like to use Breeze numerics to apply different functions to the matrices I'm working with, specifically sigmoid and tanh. Use of sigmoid alone is fine, but I want to choose sigmoid or tanh by specifying the function as a string parameter and then using it in the method. I have tried:

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

The error is

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)

and the next error is:

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.

Unspecified value parameter impl. val H: BDM[Double] = activationFuncBreeze(af)((M(::,*) + bias).t)

I've looked up Breeze numerics and implementing UFuncs but sigmoid and tanh already exist in the Breeze.numerics library as elementwise UFuncs so I should be able to use them easily. Any suggestions appreciated

1

There are 1 best solutions below

0
On

Bringing the matrices to the function seems to work:

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)