Skip to content

Commit

Permalink
add no argument apply api for softmax (intel-analytics#2945)
Browse files Browse the repository at this point in the history
* add no argument apply api for softmax

* add no argument apply api for softmax
  • Loading branch information
lingxiao1989 committed Oct 28, 2019
1 parent 3bed999 commit 5c28575
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion torch/SoftMaxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import scala.util.Random

@com.intel.analytics.bigdl.tags.Serial
class SoftMaxSpec extends TorchSpec {
"A SoftMax 1D input" should "generate correct output and grad" in {
"A SoftMax 1D input" should "generate correct output and grad" in {
torchCheck()
val layer = new SoftMax[Double]()
val input = Tensor[Double](10)
Expand Down Expand Up @@ -52,6 +52,33 @@ class SoftMaxSpec extends TorchSpec {
println("Test case : SoftMax, Torch : " + luaTime + " s, Scala : " + scalaTime / 1e9 + " s")
}

"A SoftMax 1D input without argument" should "generate correct output and grad" in {
torchCheck()
val layer = new SoftMax[Double]
val input = Tensor[Double](10)
input.apply1(_ => Random.nextDouble())
val gradOutput = Tensor[Double](10)
gradOutput.apply1(_ => Random.nextDouble())

val start = System.nanoTime()
val output = layer.forward(input)
val gradInput = layer.backward(input, gradOutput)
val end = System.nanoTime()
val scalaTime = end - start

val code = "module = nn.SoftMax()\n" +
"output = module:forward(input)\n" +
"gradInput = module:backward(input,gradOutput)"

val (luaTime, torchResult) = TH.run(code, Map("input" -> input, "gradOutput" -> gradOutput),
Array("output", "gradInput"))
val luaOutput = torchResult("output").asInstanceOf[Tensor[Double]]
val luaGradInput = torchResult("gradInput").asInstanceOf[Tensor[Double]]

output should be (luaOutput)
gradInput should be (luaGradInput)
}

"A SoftMax 2D input" should "generate correct output and grad" in {
torchCheck()
val layer = new SoftMax[Double]()
Expand Down

0 comments on commit 5c28575

Please sign in to comment.