Hi all,
This is Meng Pan. I'm now using Scala's Type Class design pattern in developing my own Deep Learning framework in Scala. However, I have encountered a few problems and can't find solution to solve them. I found that implicits are also used intensively in Breeze, but the same problems I have met seem to not exist in the implementation of Breeze. Hence could anyone help me to solve these issues?
A simplified version of the problems I have met is as follows:
Suppose I have a trait CanAutoInit for initializing my neural network's layer:
-------------------------------------------------
trait CanAutoInit[For] {
def init(f: For): Unit
}
-------------------------------------------------
Then there are a trait Layer and a trait LayerLike:
-------------------------------------------------
trait Layer extends LayerLike[Layer]
trait LayerLike[+Repr <: Layer] extends Any{
def repr: Repr = this.asInstanceOf[Repr]
final def init[TT >: Repr](implicit op: CanAutoInit[TT]): Unit = op.init(repr)
}
-------------------------------------------------
Here I pick two implimentations of the trait Layer, namely DropoutLayer and PoolingLayer, as an example to state my question:
-------------------------------------------------
class DropoutLayer extends Layer with LayerLike[DropoutLayer]
object DropoutLayer {
implicit def dropoutCanAutoInit = new CanAutoInit[DropoutLayer] {
override def init(f: DropoutLayer): Unit = println("Init inside Dropout Layer")
}
}
class PoolingLayer extends Layer with LayerLike[PoolingLayer]
object PoolingLayer {
implicit def poolingCanAutoInit = new CanAutoInit[PoolingLayer] {
override def init(f: PoolingLayer): Unit = println("Init inside PoolingLayer")
}
}
-------------------------------------------------
Then I create instances of DropoutLayer and PoolingLayer seperately, and initialize them, everything works properly:
-------------------------------------------------
val a = new DropoutLayer
a.init
val b = new PoolingLayer
b.init
-------------------------------------------------
When executing the above code, I will see "Init inside Dropout Layer" and "Init inside PoolingLayer" printed on screen as expected.
However, if I combine a and b into a list, of the type List[Layer], then compiler will complain with "could not find implicit value for parameter op: CanAutoInit[Layer]":
-------------------------------------------------
val list: List[Layer] = List(a, b)
list.foreach(_.init)
-------------------------------------------------
In order to find solution to this problem, I have tested CSCMatrix and DenseMatrix in Breeze using the same logic, and I can get the correct result I want:
-------------------------------------------------
import breeze.linalg.{CSCMatrix, DenseMatrix}
val sparse = new CSCMatrix[Double]((1 to 100).map(_.toDouble).toArray, 9, 9, (0 to 9).toArray, (0 to 9).toArray)
val dense = DenseMatrix.ones[Double](10, 10)
sparse.t
dense.t
val list = List(sparse, dense)
list.map(_.t)
-------------------------------------------------
Thus I really want to know has anyone encountered the above problem? And If so, how did you resolve it?
Thank you very much for anyone's vire and help, I am really looking forward to your reply.
Sincerely,
Meng