From 8747e6ff0cb5b614f1b488b6f6786cdebdeb009c Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Tue, 9 Nov 2010 16:41:48 -0700 Subject: [PATCH] working with single-neuron back propagation training --- neural_net/core.clj | 87 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/neural_net/core.clj b/neural_net/core.clj index e6aa75e..9ae16d7 100644 --- a/neural_net/core.clj +++ b/neural_net/core.clj @@ -76,41 +76,6 @@ {:at i :from (second a) :to (first b)})) (map (fn [a b] [a b]) boundries (rest boundries))))))) -;; learning binary representations with back-propagation -(let [n {:phi back-prop:run - :d-phi (fn [_] 1) - :accum (comp (partial reduce +) - (partial map (fn [x w] (* (x :y) w)))) - :learn back-prop:learn - :train (fn [n delta] (assoc n :weights - (vec (map + (n :weights) - (map :delta-w delta))))) - :eta 0.1} - n1 (assoc n :weights [0.5]) - n3 (assoc n :weights [0.5 0.5 0.5]) - epic [[[0 0 0] 0] - [[0 0 1] 1] - [[0 1 0] 2] - [[0 1 1] 3] - [[1 0 0] 4] - [[1 0 1] 5] - [[1 1 0] 6] - [[1 1 1] 7]] - net [(repeat 3 n1) n3]] - - ;; converting binary to decimal - (collect - (reduce (fn [m _] - (reduce - (fn [n p] - (let [x (vec (map (fn [el] {:y el}) (first p))) - d {:desired (second p)}] - (println - (format "x:%S->%S run:%S" x (d :desired) ((run n x) :y))) - (second (train n x (run n x) d)))) - m epic)) net (range 10)) - :weights)) - (comment ;; run a single simple neuron (let [n {:phi identity @@ -243,6 +208,56 @@ ;; {:delta-w -1.8, :weight 2, :gradient -18}]] ) + ;; learning binary representations with back-propagation + (let [n {:phi back-prop:run + :d-phi (fn [_] 1) + :accum (comp (partial reduce +) + (partial map (fn [x w] (* (x :y) w)))) + :learn back-prop:learn + :train (fn [n delta] (assoc n :weights + (vec (map + (n :weights) + (map :delta-w delta))))) + :eta 0.1} + n1 (assoc n :weights [0.5]) + n3 (assoc n :weights [0.5 0.5 0.5]) + epic [[[0 0 0] 0] + [[0 0 1] 1] + [[0 1 0] 2] + [[0 1 1] 3] + [[1 0 0] 4] + [[1 0 1] 5] + [[1 1 0] 6] + [[1 1 1] 7]] + ;; net [(repeat 3 n1) n3] + net n3 + trained (reduce + (fn [m _] + (reduce + (fn [n p] + (let [x (vec (map (fn [el] {:y el}) (first p))) + d {:desired (second p)}] + ;; (println + ;; (format "x:%S->%S run:%S" + ;; x (d :desired) ((run n x) :y))) + (second (train n x (run n x) d)))) + m epic)) net (range 20))] + + ;; converting binary to decimal + (map + (fn [p] + [(first p) (second p) + ((run trained (map (fn [el] {:y el}) (first p))) :y)]) + epic)) + ;; => + ;; ([[0 0 0] 0 0.0] + ;; [[0 0 1] 1 1.0202223960489385] + ;; [[0 1 0] 2 2.003127539803455] + ;; [[0 1 1] 3 3.0233499358523934] + ;; [[1 0 0] 4 3.977900692950968] + ;; [[1 0 1] 5 4.998123088999907] + ;; [[1 1 0] 6 5.981028232754423] + ;; [[1 1 1] 7 7.001250628803362]) + ;; check if inputs match the outputs (let [n {:phi back-prop:run :d-phi (fn [_] 1) @@ -257,4 +272,4 @@ d {:desired 3}] (check [(list n1 n2) n2 (list n1 n1) (list n1 n1 n2 n2 n1)])) ; ({:at 1, :from 1, :to 2}) - ) + ) \ No newline at end of file -- 2.11.4.GIT