From 91067caf66442cad082ee4ae446a33452eea9e2c Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Wed, 17 Nov 2010 11:38:34 -0700 Subject: [PATCH] initial implementation of self organizing maps - running is working - learning is working - training is working --- src/neural_net/self_organizing_maps.clj | 47 +++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/neural_net/self_organizing_maps.clj b/src/neural_net/self_organizing_maps.clj index 71936b1..7b72f6b 100644 --- a/src/neural_net/self_organizing_maps.clj +++ b/src/neural_net/self_organizing_maps.clj @@ -1,4 +1,47 @@ (ns neural-net.self-organizing-maps ; Kohonen maps - (:use neural-net.core)) + (:use neural-net.core) + (:use clojure.set)) -;; TODO -- implement +(defn self-organizing-map:run + "Return a map of the dot product of each neurons weight against x" [this x] + (reduce (fn [a [k v]] (cons [k (reduce + (map * x (v :weights)))] a)) + [] (get (this :map) :vertices))) + +(defn self-organizing-map:learn + "Return a map of learning spread through the map." [this x y d] + (let [verts (map first (reverse (sort-by second y)))] + (loop [ns [(first verts)] rem (set (rest verts)) dist 0 out {}] + (if (empty? ns) + out + (let [m (filter (partial contains? rem) (mapcat (partial neighbors (this :map)) ns))] + (recur m (difference rem (set m)) (inc dist) + (reduce (fn [a n] (assoc a n ((this :update) this dist n x))) + out ns))))))) + +(defn self-organizing-map:train [this delta] + (assoc this + :map (neural-net.core.Graph. + (reduce (fn [a [k d]] (assoc a k (map + d ((get a k) :weights)))) + (get (this :map) :vertices) delta) + (get (this :map) :edges)))) + +(comment ; train a self organizing map + (let [som {:phi self-organizing-map:run + :learn self-organizing-map:learn + :train self-organizing-map:train + :eta 1 + :update (fn [this dist n x] + (map (fn [x] (* (/ x (inc dist)) (this :eta))) + (map - + x (((get (this :map) :vertices) n) :weights)))) + :map (neural-net.core.Graph. + {:a {:weights [ 1 0]} + :b {:weights [ 0 1]} + :c {:weights [-1 0]} + :d {:weights [ 0 -1]}} + [[:a :b] [:b :c] [:c :d]])}] + (first (train som [0.8 0.2] nil nil))) + ;; => + ;; {:d (0.2 0.3), :c (0.6 0.06666666666666667), + ;; :b (0.4 -0.4), :a (-0.19999999999999996 0.2)} + ) -- 2.11.4.GIT