-
Notifications
You must be signed in to change notification settings - Fork 87
/
INDArrayTraining.scala
50 lines (45 loc) · 1.76 KB
/
INDArrayTraining.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.thoughtworks.deeplearning
package plugins
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import algebra.ring.MultiplicativeMonoid
import org.nd4s.Implicits._
private object INDArrayTraining {
private val One: INDArray = {
// Workaround for https://github.com/deeplearning4j/nd4j/issues/1869
def workaround1869(underlying: INDArray): INDArray = {
import java.lang.reflect.{Proxy, InvocationHandler, Method}
val invocationHandler = new InvocationHandler {
override def invoke(proxy: scala.Any, method: Method, args: Array[AnyRef]): AnyRef = {
method.getName match {
case "reshape" =>
workaround1869(method.invoke(underlying, args: _*).asInstanceOf[INDArray])
case "broadcast" =>
val Array(newShape: Array[Int]) = args
workaround1869(Nd4j.ones(newShape: _*))
case _ =>
method.invoke(underlying, args: _*)
}
}
}
Proxy
.newProxyInstance(getClass.getClassLoader, Array(classOf[INDArray]), invocationHandler)
.asInstanceOf[INDArray]
}
workaround1869(Nd4j.ones(1, 1))
}
}
/** A DeepLearning.scala plugin that enables [[DeepLearning.Ops.train train]] method for neural networks whose loss is a [[org.nd4j.linalg.api.ndarray.INDArray]].
*
* @author 杨博 (Yang Bo)
*/
trait INDArrayTraining extends Training {
trait ImplicitsApi extends super.ImplicitsApi {
implicit final def indArrayMultiplicativeMonoid: MultiplicativeMonoid[INDArray] =
new MultiplicativeMonoid[INDArray] {
override def one: INDArray = INDArrayTraining.One
override def times(x: INDArray, y: INDArray): INDArray = x * y
}
}
type Implicits <: ImplicitsApi
}