Created
May 15, 2010 15:51
-
-
Save pbadenski/402257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Refactor of http://www.fatvat.co.uk/2009/05/neural-networks-and-clojure.html | |
(defn create-network | |
[out] | |
(repeat out 0)) | |
(defn run-network | |
[input weights] | |
(if | |
(> (reduce + (map * input weights)) 0.001) | |
1 | |
0)) | |
(def learning-rate 0.05) | |
(defn- update-weights | |
[weights inputs error] | |
(map | |
(fn [weight input] (+ weight (* learning-rate error input))) | |
weights inputs)) | |
(defn train | |
([weights [sample expected]] | |
(let [actual (run-network sample weights) | |
error (- expected actual)] | |
(update-weights weights sample error)))) | |
(defn jiggle [data] | |
(map (fn [x] (+ x (- (rand 0.05) 0.025))) data)) | |
(defn gen-test-data [num-of-samples inputs-outputs] | |
(mapcat | |
(fn [[vector output]] | |
(map | |
list | |
(take num-of-samples (repeatedly #(jiggle vector))) | |
(repeat num-of-samples output))) | |
inputs-outputs)) | |
(defn ls-test-data [num-of-samples] | |
(gen-test-data num-of-samples | |
{[0 1 0] 0 | |
[1 0 0] 1})) | |
(defn xor-test-data [num-of-samples] | |
(gen-test-data num-of-samples | |
{[0 1] 1 | |
[1 0] 1 | |
[0 0] 0 | |
[1 1] 0})) | |
(let [nn (reduce train [0 0 0] (ls-test-data 100))] | |
[(run-network [0 1 0] nn) | |
(run-network [1 0 0] nn)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment