Skip to content

Instantly share code, notes, and snippets.

@eschulte
Created November 10, 2010 22:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eschulte/671636 to your computer and use it in GitHub Desktop.
Save eschulte/671636 to your computer and use it in GitHub Desktop.
Neural Network DSL
(ns neural-net.core) ; Copyright Eric Schulte, GPL V3
(defprotocol Neural
"Protocol implemented by any element of a neural network."
(run [this x] "Evaluates the net")
(train [this x y d] "Trains the net returning the updated net and deltas")
(collect [this key] "Collect key from the network")
(inputs [this] "Number of inputs")
(outputs [this] "Number of outputs")
(check [this] "Ensure that the number of inputs matches the outputs"))
(extend-protocol
Neural
;; a map initializes a single neural element
clojure.lang.IPersistentMap
(run [this x] ((this :phi) ((this :accum) x (this :weights))))
(train [this x y d] ((fn [delta] [delta ((this :train) this delta)])
((this :learn) this x (or y (run this x)) d)))
(collect [this key] (this key))
(inputs [this] (count (this :weights)))
(outputs [this] 1)
(check [this] nil)
;; a list of many ns in the same layer
clojure.lang.ISeq
(run [this x] (vec (map (fn [n] (run n x)) this)))
(train [this x y d]
(let [trained (map (fn [n d] (train n x (run n x) d)) this d)]
[(vec (apply map (comp vec list) (vec (map first trained))))
(map second trained)]))
(collect [this key] (map (fn [el] (collect el key)) this))
(inputs [this] (apply max (map inputs this)))
(outputs [this] (reduce + (map outputs this)))
(check [this]
(filter identity
(map-indexed (fn [i err] (when (not (empty? err)) {i err}))
(map check this))))
;; a vector of ns in different layers
clojure.lang.IPersistentVector
(run [this x]
(reduce (fn [x layer] (run layer (if (vector? x) x [x]))) x this))
(train [this x y d]
(let [xs (reverse (reduce
(fn [xs layer] (cons (run layer (first xs)) xs))
[x] this))
trained (reduce (fn [ds [x n y]]
(cons (train n x y (first (first ds))) ds))
[(cons d nil)]
(reverse (map list xs this (rest xs))))
deltas (map first trained)
net (map second trained)]
[(first (first trained)) (vec (map second (butlast trained)))]))
(collect [this key] (vec (map (fn [el] (collect el key)) this)))
(inputs [this] (inputs (first this)))
(outputs [this] (outputs (last this)))
(check [this]
(let [boundries (map (fn [el] [(inputs el) (outputs el)]) this)]
(filter identity
(map-indexed
(fn [i [a b]]
(when (not (= (second a) (first b)))
{:at i :from (second a) :to (first b)}))
(map (fn [a b] [a b]) boundries (rest boundries)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment