memoizing the neighbors function over graphs
[neural-net.git] / src / neural_net / radial_basis.clj
blob6aa78f5995c2d0cc2d496021a876763d7c207202
1 (ns neural-net.radial-basis
2   (:use neural-net.core)
3   (:use neural-net.util)
4   (:use clojure.contrib.math))
6 (defn radial:run "Run a Gaussian radial basis neuron" [this x]
7   (expt Math/E (/ (sqrt (reduce + (map (comp #(* % %) -) (this :t) x)))
8                   (* 2 (sqrt (this :std))))))
10 (comment
11   ;; sample data
12   (def training                           
13        (mapcat
14         (fn [[c o]]
15           (take 100 (repeatedly (fn []
16                                   [(vec (map + c (take 2 (repeatedly rand))))
17                                    0]))))
18         [[[    0     0] 1]
19          [[-0.75     0] 2]
20          [[    0 -0.75] 3]
21          [[-0.75 -0.75] 4]]))
23   ;; run a radial basis neuron
24   (let [x1s      (map (fn [[[x1 x2] o]] x1) training)
25         x2s      (map (fn [[[x1 x2] o]] x2) training)
26         std      (sqrt (/ (+ (expt (- (apply max x1s) (apply min x1s)) 2)
27                              (expt (- (apply max x2s) (apply min x2s)) 2))
28                           (sqrt 2)))
29         rb       {:std std
30                   :phi radial:run
31                   :weights [1 1]}
32         p        {:phi perceptron:run
33                   :weights (vec (take 3 (repeatedly rand-weight)))
34                   :learn perceptron:learn
35                   :train (fn [n delta]
36                            (assoc n :weights (vec (map + (n :weights) delta))))
37                   :eta 0.01}]
38     (run [(list (assoc rb :t [(pick x1s) (pick x2s)])
39                 (assoc rb :t [(pick x1s) (pick x2s)])
40                 (assoc rb :t [(pick x1s) (pick x2s)])) p]
41          (first (first training))))
43   ;; training is working... sort of
44   (let [training (map (fn [p] [[(nth p 2) (nth p 3)] (nth p 0)]) training)
45         testing (map (fn [p] [[(nth p 2) (nth p 3)] (nth p 0)]) testing)
46         x1s (map (fn [[[x1 x2] o]] x1) training)
47         x2s (map (fn [[[x1 x2] o]] x2) training)
48         std (sqrt (/ (+ (expt (- (apply max x1s) (apply min x1s)) 2)
49                         (expt (- (apply max x2s) (apply min x2s)) 2))
50                      (sqrt 2)))
51         rb {:std std
52             :phi radial:run
53             :weights [1 1]
54             :learn (fn [this x y d] (map (fn [_] 0) (this :weights)))
55             :train (fn [n delta] n)}
56         p {:phi perceptron:run
57            :weights (vec (take 3 (repeatedly rand-weight)))
58            :learn perceptron:learn
59            :train (fn [n delta]
60                     (assoc n :weights (vec (map + (n :weights) delta))))
61            :eta 0.00001}
62         net [(list (assoc rb :t [(pick x1s) (pick x2s)])
63                    (assoc rb :t [(pick x1s) (pick x2s)])
64                    (assoc rb :t [(pick x1s) (pick x2s)])) p]]
65     (reduce
66      (fn [m index]
67        (println (format "%S\t%S\t%S" index
68                         (rms-error m training) (epic-error m testing)))
69        (let [n (reduce
70                 (fn [n p]
71                   (second (train n (first p) (run n (first p)) (second p))))
72                 m training)]
73          n)) net (range 30)))
74   )