You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by cm...@apache.org on 2018/08/16 01:31:09 UTC

[incubator-mxnet] 01/01: add initializer test

This is an automated email from the ASF dual-hosted git repository.

cmeier pushed a commit to branch more-clojure-tests
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git

commit e80765f4929caa5f65050b43eb89e7684b220ded
Author: gigasquid <cm...@gigasquidsoftware.com>
AuthorDate: Fri Aug 3 14:50:52 2018 -0400

    add initializer test
    
    add profiler and lr-scheduler tests
    
    basic symbol test
    
    add more symbol tests
    
    re-enable deeper visualization graph example and add simple test
    
    running cljfmt
    
    add license - fix typo
---
 .../examples/tutorial/src/tutorial/symbol.clj      |  1 -
 .../examples/visualization/README.md               |  2 +
 .../visualization/src/visualization/core.clj       | 18 +++++----
 .../clojure-package/examples/visualization/testviz | 32 +++++++++++++++
 .../src/org/apache/clojure_mxnet/profiler.clj      |  2 +-
 .../org/apache/clojure_mxnet/initializer_test.clj  | 45 ++++++++++++++++++++++
 .../org/apache/clojure_mxnet/lr_scheduler_test.clj | 24 ++++++++++++
 .../org/apache/clojure_mxnet/profiler_test.clj     | 31 +++++++++++++++
 .../test/org/apache/clojure_mxnet/symbol_test.clj  | 32 ++++++++++++++-
 .../apache/clojure_mxnet/visualization_test.clj    | 38 ++++++++++++++++++
 10 files changed, 214 insertions(+), 11 deletions(-)

diff --git a/contrib/clojure-package/examples/tutorial/src/tutorial/symbol.clj b/contrib/clojure-package/examples/tutorial/src/tutorial/symbol.clj
index 0dd0e4d..bec71de 100644
--- a/contrib/clojure-package/examples/tutorial/src/tutorial/symbol.clj
+++ b/contrib/clojure-package/examples/tutorial/src/tutorial/symbol.clj
@@ -50,7 +50,6 @@ net ;=> #object[org.apache.mxnet.Symbol 0x5c78c8c2 "org.apache.mxnet.Symbol@5c78
 (def b (sym/variable "b"))
 (def c (sym/+ a b))
 
-
 ;; Each symbol takes a (unique) string name. NDArray and Symbol both represent a single tensor. Operators represent the computation between tensors. Operators take symbol (or NDArray) as inputs and might also additionally accept other hyperparameters such as the number of hidden neurons (num_hidden) or the activation type (act_type) and produce the output.
 
 ;; We can view a symbol simply as a function taking several arguments. And we can retrieve those arguments with the following method call:
diff --git a/contrib/clojure-package/examples/visualization/README.md b/contrib/clojure-package/examples/visualization/README.md
index 8c6e2c2..54ece37 100644
--- a/contrib/clojure-package/examples/visualization/README.md
+++ b/contrib/clojure-package/examples/visualization/README.md
@@ -1,4 +1,6 @@
 # visualization
 
+You must have graphviz installed to be able to run this.
+
 Run `lein run` to have a sample network visualization printed for you
 "testviz.pdf"
diff --git a/contrib/clojure-package/examples/visualization/src/visualization/core.clj b/contrib/clojure-package/examples/visualization/src/visualization/core.clj
index 31cce92..8e51d87 100644
--- a/contrib/clojure-package/examples/visualization/src/visualization/core.clj
+++ b/contrib/clojure-package/examples/visualization/src/visualization/core.clj
@@ -22,16 +22,18 @@
 (defn get-symbol []
   (as-> (sym/variable "data") data
 
-    #_(sym/convolution "conv1" {:data data :kernel [3 3] :num-filter 32 :stride [2 2]})
-    #_(sym/batch-norm "bn1" {:data data})
-    #_(sym/activation "relu1" {:data data :act-type "relu"})
-    #_(sym/pooling "mp1" {:data data :kernel [2 2] :pool-type "max" :stride [2 2]}) #_(sym/convolution "conv2" {:data data :kernel [3 3] :num-filter 32 :stride [2 2]})
-    #_(sym/batch-norm "bn2" {:data data})
-    #_(sym/activation "relu2" {:data data :act-type "relu"})
-    #_(sym/pooling "mp2" {:data data :kernel [2 2] :pool-type "max" :stride [2 2]})
+    (sym/convolution "conv1" {:data data :kernel [3 3] :num-filter 32 :stride [2 2]})
+
+    (sym/batch-norm "bn1" {:data data})
+    (sym/activation "relu1" {:data data :act-type "relu"})
+    (sym/pooling "mp1" {:data data :kernel [2 2] :pool-type "max" :stride [2 2]})
+    (sym/convolution "conv2" {:data data :kernel [3 3] :num-filter 32 :stride [2 2]})
+    (sym/batch-norm "bn2" {:data data})
+    (sym/activation "relu2" {:data data :act-type "relu"})
+    (sym/pooling "mp2" {:data data :kernel [2 2] :pool-type "max" :stride [2 2]})
 
     (sym/flatten "fl" {:data data})
-    #_(sym/fully-connected "fc2" {:data data :num-hidden 10})
+    (sym/fully-connected "fc2" {:data data :num-hidden 10})
     (sym/softmax-output "softmax" {:data data})))
 
 (defn test-viz []
diff --git a/contrib/clojure-package/examples/visualization/testviz b/contrib/clojure-package/examples/visualization/testviz
new file mode 100644
index 0000000..c0161e9
--- /dev/null
+++ b/contrib/clojure-package/examples/visualization/testviz
@@ -0,0 +1,32 @@
+digraph foo{
+	data [label=data  fixedsize=false style=filled height=0.8034 fillcolor="#8dd3c7" shape=oval width=1.3]
+	conv1 [label="Convolution\n3x3/2x2, 32"  fixedsize=false style=filled height=0.8034 fillcolor="#fb8072" shape=oval width=1.3]
+	bn1 [label=bn1  fixedsize=false style=filled height=0.8034 fillcolor="#bebada" shape=oval width=1.3]
+	relu1 [label="Activation
+relu"  fixedsize=false style=filled height=0.8034 fillcolor="#ffffb3" shape=oval width=1.3]
+	mp1 [label="Pooling
+max, 2x2/2x2"  fixedsize=false style=filled height=0.8034 fillcolor="#80b1d3" shape=oval width=1.3]
+	conv2 [label="Convolution\n3x3/2x2, 32"  fixedsize=false style=filled height=0.8034 fillcolor="#fb8072" shape=oval width=1.3]
+	bn2 [label=bn2  fixedsize=false style=filled height=0.8034 fillcolor="#bebada" shape=oval width=1.3]
+	relu2 [label="Activation
+relu"  fixedsize=false style=filled height=0.8034 fillcolor="#ffffb3" shape=oval width=1.3]
+	mp2 [label="Pooling
+max, 2x2/2x2"  fixedsize=false style=filled height=0.8034 fillcolor="#80b1d3" shape=oval width=1.3]
+	fl [label=fl  fixedsize=false style=filled height=0.8034 fillcolor="#fdb462" shape=oval width=1.3]
+	fc2 [label="FullyConnected
+10"  fixedsize=false style=filled height=0.8034 fillcolor="#fb8072" shape=oval width=1.3]
+	softmax_label [label=softmax_label  fixedsize=false style=filled height=0.8034 fillcolor="#8dd3c7" shape=oval width=1.3]
+	softmax [label=softmax  fixedsize=false style=filled height=0.8034 fillcolor="#fccde5" shape=oval width=1.3]
+		conv1 -> data [ arrowtail=open dir=back label="1x28x28"]
+		bn1 -> conv1 [ arrowtail=open dir=back label="32x13x13"]
+		relu1 -> bn1 [ arrowtail=open dir=back label="32x13x13"]
+		mp1 -> relu1 [ arrowtail=open dir=back label="32x13x13"]
+		conv2 -> mp1 [ arrowtail=open dir=back label="32x6x6"]
+		bn2 -> conv2 [ arrowtail=open dir=back label="32x2x2"]
+		relu2 -> bn2 [ arrowtail=open dir=back label="32x2x2"]
+		mp2 -> relu2 [ arrowtail=open dir=back label="32x2x2"]
+		fl -> mp2 [ arrowtail=open dir=back label="32x1x1"]
+		fc2 -> fl [ arrowtail=open dir=back label="32"]
+		softmax -> fc2 [ arrowtail=open dir=back label="10"]
+		softmax -> softmax_label [ arrowtail=open dir=back label=""]
+}
diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/profiler.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/profiler.clj
index 48fd041..5b4f9b1 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/profiler.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/profiler.clj
@@ -36,7 +36,7 @@
   ([state]
    (Profiler/profilerSetState state))
   ([]
-   (profiler-set-state false)))
+   (profiler-set-state "stop")))
 
 (defn dump-profile
   " Dump profile and stop profiler. Use this to save profile
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/initializer_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/initializer_test.clj
new file mode 100644
index 0000000..288a414
--- /dev/null
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/initializer_test.clj
@@ -0,0 +1,45 @@
+;;
+;; Licensed to the Apache Software Foundation (ASF) under one or more
+;; contributor license agreements.  See the NOTICE file distributed with
+;; this work for additional information regarding copyright ownership.
+;; The ASF licenses this file to You under the Apache License, Version 2.0
+;; (the "License"); you may not use this file except in compliance with
+;; the License.  You may obtain a copy of the License at
+;;
+;;    http://www.apache.org/licenses/LICENSE-2.0
+;;
+;; Unless required by applicable law or agreed to in writing, software
+;; distributed under the License is distributed on an "AS IS" BASIS,
+;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;; See the License for the specific language governing permissions and
+;; limitations under the License.
+;;
+
+(ns org.apache.clojure-mxnet.initializer-test
+  (:require [org.apache.clojure-mxnet.initializer :as initializer]
+            [org.apache.clojure-mxnet.ndarray :as ndarray]
+            [clojure.test :refer :all]))
+
+(defn exercise-initializer [init]
+  (-> init
+      (initializer/init-weight "test-weight" (ndarray/zeros [3 3])))
+
+  (is (number?
+       (-> init
+           (initializer/apply "test-weight" (ndarray/zeros [3 3]))
+           (ndarray/->vec)
+           (first)))))
+
+(deftest test-uniform
+  (exercise-initializer (initializer/uniform))
+  (exercise-initializer (initializer/uniform 0.8)))
+
+(deftest test-normal
+  (exercise-initializer (initializer/normal))
+  (exercise-initializer (initializer/normal 0.2)))
+
+(deftest test-xavier
+  (exercise-initializer (initializer/xavier))
+  (exercise-initializer (initializer/xavier {:rand-type "gaussian"
+                                             :factor-type "in"
+                                             :magnitude 2})))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/lr_scheduler_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/lr_scheduler_test.clj
new file mode 100644
index 0000000..c60389a
--- /dev/null
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/lr_scheduler_test.clj
@@ -0,0 +1,24 @@
+;;
+;; Licensed to the Apache Software Foundation (ASF) under one or more
+;; contributor license agreements.  See the NOTICE file distributed with
+;; this work for additional information regarding copyright ownership.
+;; The ASF licenses this file to You under the Apache License, Version 2.0
+;; (the "License"); you may not use this file except in compliance with
+;; the License.  You may obtain a copy of the License at
+;;
+;;    http://www.apache.org/licenses/LICENSE-2.0
+;;
+;; Unless required by applicable law or agreed to in writing, software
+;; distributed under the License is distributed on an "AS IS" BASIS,
+;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;; See the License for the specific language governing permissions and
+;; limitations under the License.
+;;
+
+(ns org.apache.clojure-mxnet.lr-scheduler-test
+  (:require [org.apache.clojure-mxnet.lr-scheduler :as lr-scheduler]
+            [clojure.test :refer :all]))
+
+(deftest test-factor-scheduler
+  ;; just excercising
+  (lr-scheduler/factor-scheduler 2 0.3))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/profiler_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/profiler_test.clj
new file mode 100644
index 0000000..f4b7434
--- /dev/null
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/profiler_test.clj
@@ -0,0 +1,31 @@
+;;
+;; Licensed to the Apache Software Foundation (ASF) under one or more
+;; contributor license agreements.  See the NOTICE file distributed with
+;; this work for additional information regarding copyright ownership.
+;; The ASF licenses this file to You under the Apache License, Version 2.0
+;; (the "License"); you may not use this file except in compliance with
+;; the License.  You may obtain a copy of the License at
+;;
+;;    http://www.apache.org/licenses/LICENSE-2.0
+;;
+;; Unless required by applicable law or agreed to in writing, software
+;; distributed under the License is distributed on an "AS IS" BASIS,
+;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;; See the License for the specific language governing permissions and
+;; limitations under the License.
+;;
+
+(ns org.apache.clojure-mxnet.profiler-test
+  (:require [org.apache.clojure-mxnet.profiler :as profiler]
+            [clojure.test :refer :all]))
+
+;; Just excercising the interop
+
+(deftest test-profiler
+  (do
+    (profiler/profiler-set-config  {:filename "test-profile.json"
+                                    :profile-symbolic 1})
+    (profiler/profiler-set-state "run")
+    (profiler/profiler-set-state "stop")
+    (profiler/profiler-set-state)
+    (profiler/dump-profile 0)))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/symbol_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/symbol_test.clj
index 6df2a10..89b5123 100644
--- a/contrib/clojure-package/test/org/apache/clojure_mxnet/symbol_test.clj
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/symbol_test.clj
@@ -17,9 +17,12 @@
 
 (ns org.apache.clojure-mxnet.symbol-test
   (:require [org.apache.clojure-mxnet.dtype :as dtype]
+            [org.apache.clojure-mxnet.executor :as executor]
+            [org.apache.clojure-mxnet.ndarray :as ndarray]
             [org.apache.clojure-mxnet.symbol :as sym]
             [org.apache.clojure-mxnet.util :as util]
-            [clojure.test :refer :all]))
+            [clojure.test :refer :all]
+            [org.apache.clojure-mxnet.context :as context]))
 
 (deftest test-compose
   (let [data (sym/variable "data")
@@ -61,3 +64,30 @@
   (let [data (sym/variable "data")
         data2 (sym/clone data)]
     (is (= (sym/to-json data) (sym/to-json data2)))))
+
+(deftest test-basic-bind
+  (let [a (sym/variable "a")
+        b (sym/variable "b")
+        c (sym/+ a b)
+        ex (sym/bind c {"a" (ndarray/ones [2 2]) "b" (ndarray/ones [2 2])})]
+    (is (= [2.0 2.0 2.0 2.0]) (-> (executor/forward ex)
+                                  (executor/outputs)
+                                  (first)
+                                  (ndarray/->vec)))))
+(deftest test-simple-bind
+  (let [a (sym/ones [3])
+        b (sym/ones [3])
+        c (sym/+ a b)
+        ex (sym/simple-bind c (context/default-context))]
+    (is (= [2.0 2.0 2.0]  (-> (executor/forward ex)
+                              (executor/outputs)
+                              (first)
+                              (ndarray/->vec))))))
+
+(deftest test-infer-shape
+  (let [a (sym/variable "a")
+        b (sym/variable "b")
+        c (sym/+ a b)
+        [arg-shapes out-shapes] (sym/infer-shape c {"a" [2 2] "b" [2 2]})]
+    (is (= [[2 2] [2 2]] arg-shapes))
+    (is (= [[2 2]] out-shapes))))
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/visualization_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/visualization_test.clj
new file mode 100644
index 0000000..2332941
--- /dev/null
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/visualization_test.clj
@@ -0,0 +1,38 @@
+;;
+;; Licensed to the Apache Software Foundation (ASF) under one or more
+;; contributor license agreements.  See the NOTICE file distributed with
+;; this work for additional information regarding copyright ownership.
+;; The ASF licenses this file to You under the Apache License, Version 2.0
+;; (the "License"); you may not use this file except in compliance with
+;; the License.  You may obtain a copy of the License at
+;;
+;;    http://www.apache.org/licenses/LICENSE-2.0
+;;
+;; Unless required by applicable law or agreed to in writing, software
+;; distributed under the License is distributed on an "AS IS" BASIS,
+;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;; See the License for the specific language governing permissions and
+;; limitations under the License.
+;;
+
+(ns org.apache.clojure-mxnet.visualization-test
+  (:require [org.apache.clojure-mxnet.symbol :as sym]
+            [org.apache.clojure-mxnet.visualization :as viz]
+            [clojure.test :refer :all])
+  (:import (org.apache.mxnet Visualization$Dot)))
+
+(deftest test-plot-network
+  (let [to-plot-sym (as-> (sym/variable "data") data
+                      (sym/convolution "conv1" {:data data
+                                                :kernel [3 3]
+                                                :num-filter 32
+                                                :stride [2 2]})
+                      (sym/batch-norm "bn1" {:data data})
+                      (sym/activation "relu1" {:data data :act-type "relu"})
+                      (sym/fully-connected "fc2" {:data data :num-hidden 10})
+                      (sym/softmax-output "softmax" {:data data}))
+        dot (viz/plot-network to-plot-sym
+                              {"data" [1 1 28 28]}
+                              {:title "foo"
+                               :node-attrs {:shape "oval" :fixedsize "false"}})]
+    (is (instance? Visualization$Dot dot))))