memoizing the neighbors function over graphs
[neural-net.git] / src / neural_net / perceptron.clj
blob1781cb86979179c7bd103af52d820585a223f9cb
1 (ns neural-net.perceptron
2   (:use neural-net.core))
4 (defn perceptron:run "Perceptron execution" [this x]
5   (reduce + (map (fn [x w] (* x w)) x (this :weights))))
7 (defn perceptron:learn "Perceptron learning" [this x y d]
8   (let [dir ((fn [dif] (cond (> dif 0) 1 (< dif 0) -1 true 0)) (- d y))]
9     (vec (map (fn [x] (* dir (this :eta) x)) x))))
11 (comment         ; perceptron learning -- converting binary to decimal
12   (let [n {:phi perceptron:run
13            :weights [0 0]
14            :learn perceptron:learn
15            :train (fn [n delta]
16                     (assoc n :weights (vec (map + (n :weights) delta))))
17            :eta 0.01}
18         epic [[[0 0] 0]
19               [[0 1] 1]
20               [[1 0] 2]
21               [[1 1] 3]]
22         x [1 1]
23         d 1]
24     ((reduce (fn [m _]
25                (reduce
26                 (fn [n p]
27                   (second (train n (first p) (run n (first p)) (second p))))
28                 m epic)) n (range 100))
29      :weights))              ; [2.0000000000000013 1.0000000000000007]
30   )
32 (comment                 ; perceptron learning over an arbitrary graph
33   (let [n {:phi perceptron:run
34            :learn perceptron:learn
35            :eta 0.01
36            :train (fn [n delta]
37                     (assoc n :weights (vec (map + (n :weights) delta))))
38            :weights [2 2]}
39         g (Graph. {:a n :b n :c n} [[:a :b] [:a :c] [:b :c]])
40         x [1 1 1]
41         d [30]]
42     (list (run g x)
43           (run (second (train g x nil d)) x)))
44   )