Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
add initializer test (#12196)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gigasquid authored Aug 24, 2018
1 parent 490cf99 commit 5189495
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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})))
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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)))
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;;
;; 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/flatten "fl" {:data data})
(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))))

0 comments on commit 5189495

Please sign in to comment.