2 (:use [neural-net.util])
3 (:use [clojure.contrib math]))
5 ;;; Value-based DSL definition
6 (defprotocol NeuralValue
7 (spec [this] "Returns the specification of the net.")
8 (func [this] "Returns a function implementing the neural net.")
9 (train [this f] "Trains the net using f returning the updated version."))
11 (defrecord Neural [spec]
13 (func [this] (func spec))
17 "Create and return a Neural network based on the specification."
22 ;; a map holds the values of a neuron
23 clojure.lang.IPersistentMap
27 ;; ordered lists holding collections of neurons
29 clojure.lang.IPersistentVector
30 (spec [this] (vec (map spec this)))
33 ;; TODO need to notice matrices of numbers as neurons specifications
36 ;; can't resolve weight assignment for unordered collections
37 ;; clojure.lang.PersistentHashSet
40 ;;; activation functions
42 ^{:doc "Derivative of the step function."}
43 (fn [v] (if (= v 0) (/ 1 0) 0)))
46 ^{:doc "The sigmoid function."
48 (fn [v] (if (pos? v) 1 -1)))
51 ^{:doc "Derivative of the sigmoid function."}
52 (fn [v] (/ (expt Math/E (- 0 v)) (expt (+ (expt Math/E (- 0 v)) 1) 2))))
55 ^{:doc "The sigmoid function."
57 (fn [v] (/ 1 (+ 1 (expt Math/E (- 0 v))))))