我正在尝试实现自定义丢失函数(DL4J),请遵循以下代码示例:https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/misc/lossfunctions/CustomLossL1L2.java。
我需要扩展ILossFunction 并覆盖一些方法。
问题:
该函数返回org.nd4j.linalg.primitives.Pair<Double, INDArray>
。
我试过了:
override def computeGradientAndScore(
labels: INDArray,
preOutput: INDArray,
activationFn: IActivation,
mask: INDArray,
average: Boolean
): Pair[Double, INDArray] = {
Pair.makePair(
computeScore(labels, preOutput, activationFn, mask, average),
computeGradient(labels, preOutput, activationFn, mask)
)
}
并获得以下编译错误:
[info] Compiling 2 Scala sources to PATH
[error] PATH/CosineSimilarity.scala:78: overriding method computeGradientAndScore in trait ILossFunction of type (x$1: org.nd4j.linalg.api.ndarray.INDArray, x$2: org.nd4j.linalg.api.ndarray.INDArray, x$3: org.nd4j.linalg.activations.IActivation, x$4: org.nd4j.linalg.api.ndarray.INDArray, x$5: Boolean)org.nd4j.linalg.primitives.Pair[Double,org.nd4j.linalg.api.ndarray.INDArray];
[error] method computeGradientAndScore has incompatible type
[error] override def computeGradientAndScore(labels: INDArray, preOutput: INDArray, activationFn: IActivation, mask: INDArray, average: Boolean): Pair[Double, INDArray] = {
[error] ^
[error] one error found
[error] (root/compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed 16 mai 2018 16:45:48
问题: 我该如何覆盖此方法?
答案 0 :(得分:0)
以下代码
import java.lang
import org.nd4j.linalg.activations.IActivation
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.lossfunctions.ILossFunction
import org.nd4j.linalg.primitives
class MyLossFunction extends ILossFunction {
override def computeScore(labels: INDArray, preOutput: INDArray, activationFn: IActivation, mask: INDArray, average: Boolean): Double = ???
override def computeScoreArray(labels: INDArray, preOutput: INDArray, activationFn: IActivation, mask: INDArray): INDArray = ???
override def computeGradient(labels: INDArray, preOutput: INDArray, activationFn: IActivation, mask: INDArray): INDArray = ???
override def computeGradientAndScore(labels: INDArray, preOutput: INDArray, activationFn: IActivation, mask: INDArray, average: Boolean): primitives.Pair[lang.Double, INDArray] = {
primitives.Pair.makePair(
computeScore(labels, preOutput, activationFn, mask, average),
computeGradient(labels, preOutput, activationFn, mask)
)
}
override def name(): String = ???
}
编译
scalaVersion := "2.12.6"
libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.9.1"
libraryDependencies += "org.nd4j" % "nd4j-native-platform" % "0.9.1" % Test
libraryDependencies += "org.datavec" % "datavec-api" % "0.9.1"