Skip to content

Instantly share code, notes, and snippets.

@whilo
Created July 25, 2017 22:59
Show Gist options
  • Save whilo/ce56bd68934e64e9811bae5d29d01fa7 to your computer and use it in GitHub Desktop.
Save whilo/ce56bd68934e64e9811bae5d29d01fa7 to your computer and use it in GitHub Desktop.
(let [f (fn [args]
(let [w1 (nth args 0)
w2 (nth args 1)
x (nth args 2)
y (nth args 3)
h1 (mmul w1 x)
h2 (mmul w2 h1)
;; TODO dot with vector of ones yields sum
;; this is euclidian distance or squared error
error (** (- (dot [1 1] h2) y) 2)]
error))
f-inspect (fn [args]
(let [w1 (nth args 0)
w2 (nth args 1)
x (nth args 2)
y (nth args 3)
h1 (mmul w1 x)
h2 (mmul w2 h1)
pred (dot [1 1] h2)
error (** (- pred y) 2)]
[h1 h2 error pred]))
f' (gradient-vector-F f)]
(loop [i 1000
w1 [[0.3509216367936949 0.8478685293908089]
[0.04224372281103417 0.7689310188327428]]
w2 [[0.7021497660571601 0.4090020951653528]
[0.9584080028515212 0.3988865031355743]]]
(if (pos? i)
(let [x [1 2]
y 2
[w1' w2'] (f' [w1 w2 x y])]
(when (= (mod i 100) 0)
(let [[h1 h2 error pred] (f-inspect [w1 w2 x y])]
(prn "h1" h1)
(prn "h2" h2)
(prn "Pred:" pred)
(prn "Error:" error)))
(recur (dec i) (- w1 (* 0.01 w1')) (- w2 (* 0.01 w2'))))
[w1 w2] ;; trained model
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment