lanking520 commented on a change in pull request #11292: [MXNET-531] CNN 
Examples for Scala new API
URL: https://github.com/apache/incubator-mxnet/pull/11292#discussion_r199984082
 
 

 ##########
 File path: 
scala-package/examples/src/main/scala/org/apache/mxnetexamples/cnntextclassification/CNNTextClassification.scala
 ##########
 @@ -17,78 +17,81 @@
 
 package org.apache.mxnetexamples.cnntextclassification
 
+import org.apache.mxnet.optimizer.RMSProp
+import org.apache.mxnet.{Context, Executor, Model, NDArray, Optimizer, Shape, 
Symbol, Uniform}
 import org.kohsuke.args4j.{CmdLineParser, Option}
 import org.slf4j.LoggerFactory
+
 import scala.collection.JavaConverters._
-import org.apache.mxnet.Uniform
-import org.apache.mxnet.Context
-import org.apache.mxnet.Symbol
-import org.apache.mxnet.Shape
-import org.apache.mxnet.NDArray
-import org.apache.mxnet.Executor
-import org.apache.mxnet.optimizer.RMSProp
-import org.apache.mxnet.Optimizer
-import org.apache.mxnet.Model
 import scala.util.Random
 
 /**
- * An Implementation of the paper
- * Convolutional Neural Networks for Sentence Classification
- * by Yoon Kim
- * @author Depeng Liang
- */
+  * An Implementation of the paper
+  * Convolutional Neural Networks for Sentence Classification
+  */
 object CNNTextClassification {
 
   private val logger = LoggerFactory.getLogger(classOf[CNNTextClassification])
 
   case class CNNModel(cnnExec: Executor, symbol: Symbol, data: NDArray, label: 
NDArray,
-      argsDict: Map[String, NDArray], gradDict: Map[String, NDArray])
+                      argsDict: Map[String, NDArray], gradDict: Map[String, 
NDArray])
 
   def makeTextCNN(sentenceSize: Int, numEmbed: Int, batchSize: Int,
-    numLabel: Int = 2, filterList: Array[Int] = Array(3, 4, 5), numFilter: Int 
= 100,
-    dropout: Float = 0.5f): Symbol = {
+                  numLabel: Int = 2, filterList: Array[Int] = Array(3, 4, 5), 
numFilter: Int = 100,
+                  dropout: Float = 0.5f): Symbol = {
 
     val inputX = Symbol.Variable("data")
     val inputY = Symbol.Variable("softmax_label")
     val polledOutputs = filterList.map { filterSize =>
-      val conv = Symbol.Convolution()()(
-        Map("data" -> inputX, "kernel" -> s"($filterSize, $numEmbed)", 
"num_filter" -> numFilter))
-      val relu = Symbol.Activation()()(Map("data" -> conv, "act_type" -> 
"relu"))
-      val pool = Symbol.Pooling()()(Map("data" -> relu, "pool_type" -> "max",
-        "kernel" -> s"(${sentenceSize - filterSize + 1}, 1)", "stride" -> 
"(1,1)"))
+      val conv = Symbol.api.Convolution(data = Some(inputX),
+        kernel = new Shape(filterSize, numEmbed), num_filter = numFilter)
+      val relu = Symbol.api.Activation(data = Some(conv), act_type = "relu")
+      val pool = Symbol.api.Pooling(data = Some(relu), pool_type = Some("max"),
+        kernel = Some(new Shape(sentenceSize - filterSize + 1, 1)), stride = 
Some(new Shape(1, 1)))
+      relu.dispose()
+      conv.dispose()
       pool
     }
 
     val totalFilters = numFilter * filterList.length
-    val concat = Symbol.Concat()(polledOutputs: _*)(Map("dim" -> 1))
-    val hPool = Symbol.Reshape()()(Map("data" -> concat,
-      "target_shape" -> s"($batchSize, $totalFilters)"))
+    // val concat = Symbol.Concat()(polledOutputs: _*)(Map("dim" -> 1))
+    val concat = Symbol.api.concat(data = polledOutputs,
+      num_args = polledOutputs.length, dim = Some(1))
+    val hPool = Symbol.api.reshape(data = Some(concat),
+      target_shape = Some(new Shape(batchSize, totalFilters)))
 
     val hDrop = {
-      if (dropout > 0f) Symbol.Dropout()()(Map("data" -> hPool, "p" -> 
dropout))
+      if (dropout > 0f) Symbol.api.Dropout(data = Some(hPool), p = 
Some(dropout))
       else hPool
     }
 
-    val fc = Symbol.FullyConnected()()(Map("data" -> hDrop, "num_hidden" -> 
numLabel))
-    val sm = Symbol.SoftmaxOutput()()(Map("data" -> fc, "label" -> inputY))
+    val fc = Symbol.api.FullyConnected(data = Some(hDrop), num_hidden = 
numLabel)
+    val sm = Symbol.api.SoftmaxOutput(data = Some(fc), label = Some(inputY))
+    fc.dispose()
 
 Review comment:
   Yes, I have. Not crashing

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to