Skip to content

Commit

Permalink
Removed profile code
Browse files Browse the repository at this point in the history
  • Loading branch information
deltam committed Sep 30, 2019
1 parent 396b792 commit b21be19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
38 changes: 17 additions & 21 deletions src/neuro/layer.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns neuro.layer
"Neural Network Layer"
(:require [taoensso.tufte :refer [p]])
; (:require [taoensso.tufte :refer [p]])
(:require [neuro.vol :as vl]))

(defprotocol Executable
Expand Down Expand Up @@ -34,18 +34,16 @@
(defrecord FullConn [in out w bias in-vol out-vol dw dbias delta-vol]
Executable
(forward [this in-vol]
(p :fc-for
(let [{w :w, bias :bias} this
[len _] (vl/shape in-vol)]
(assoc this
:in-vol in-vol
:out-vol (vl/w+ (vl/dot in-vol w) (vl/repeat bias len))))))
(let [{w :w, bias :bias} this
[len _] (vl/shape in-vol)]
(assoc this
:in-vol in-vol
:out-vol (vl/w+ (vl/dot in-vol w) (vl/repeat bias len)))))
(backward [this grad-vol]
(p :fc-back
(assoc this
:dw (vl/dot (vl/T (:in-vol this)) grad-vol)
:dbias (vl/sum-row grad-vol)
:delta-vol (vl/dot grad-vol (vl/T (:w this))))))
(assoc this
:dw (vl/dot (vl/T (:in-vol this)) grad-vol)
:dbias (vl/sum-row grad-vol)
:delta-vol (vl/dot grad-vol (vl/T (:w this)))))
(output [this] (:out-vol this))
(grad [this] (:delta-vol this))
Optimizable
Expand Down Expand Up @@ -171,16 +169,14 @@
(defrecord Softmax [out out-vol delta-vol loss]
Executable
(forward [this in-vol]
(p :softmax-for
(assoc this
:out-vol (softmax-f-n in-vol))))
(assoc this
:out-vol (softmax-f-n in-vol)))
(backward [this answer-vol]
(p :softmax-back
(assoc this
:delta-vol (let [[batch-size _] (vl/shape answer-vol)]
(vl/map-w #(/ % batch-size)
(vl/w- (:out-vol this) answer-vol)))
:loss (cross-entropy-n answer-vol (:out-vol this)))))
(assoc this
:delta-vol (let [[batch-size _] (vl/shape answer-vol)]
(vl/map-w #(/ % batch-size)
(vl/w- (:out-vol this) answer-vol)))
:loss (cross-entropy-n answer-vol (:out-vol this))))
(output [this] (:out-vol this))
(grad [this] (:delta-vol this))
Optimizable
Expand Down
38 changes: 18 additions & 20 deletions src/neuro/vol.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns neuro.vol
"Matrix for Neural Network"
(:require [taoensso.tufte :refer [p]])
; (:require [taoensso.tufte :refer [p]])
(:refer-clojure :exclude [repeat shuffle partition print rand])
)

Expand Down Expand Up @@ -141,22 +141,21 @@
(defn dot
"w行列の掛け算 (dot [N M] [M K]) = [N K]"
[v1 v2]
(p :dot
(let [[col1 row1] (shape v1)
[col2 row2] (shape v2)]
(vol col1 row2
(loop [c 0, r 0, ret (transient [])]
(cond
(<= col1 c) (persistent! ret)
(<= row2 r) (recur (inc c) 0 ret)
:else (recur c (inc r)
(conj! ret
(loop [i 0, acc 0]
(if (<= row1 i)
acc
(recur (inc i)
(+ acc (* (wget v1 c i)
(wget v2 i r))))))))))))))
(let [[col1 row1] (shape v1)
[col2 row2] (shape v2)]
(vol col1 row2
(loop [c 0, r 0, ret (transient [])]
(cond
(<= col1 c) (persistent! ret)
(<= row2 r) (recur (inc c) 0 ret)
:else (recur c (inc r)
(conj! ret
(loop [i 0, acc 0]
(if (<= row1 i)
acc
(recur (inc i)
(+ acc (* (wget v1 c i)
(wget v2 i r)))))))))))))


(defn w-elm-op [f this other]
Expand Down Expand Up @@ -188,9 +187,8 @@
(defn sum-row
"行を足し合わせて1xNの行列にする"
[v]
(p :sum-row
(let [[n _] (shape v)]
(dot (ones n) v))))
(let [[n _] (shape v)]
(dot (ones n) v)))


(defn w-max
Expand Down

0 comments on commit b21be19

Please sign in to comment.