mean and standard deviation functions in util
[neural-net.git] / src / neural_net / util.clj
blobd5e7249b993bfac931ac55255ec92fef65b1b5cb
1 (ns neural-net.util
2   (:use neural-net.core)
3   (:use clojure.contrib.math))
5 (defn pick
6   ([lst] (nth lst (rand (count lst))))
7   ([lst num]
8      (if (> num 0)
9        (let [i (rand (count lst))]
10          (cons (nth lst i)
11                (pick (concat (take (dec i) lst) (drop i lst)) (dec num))))
12        nil)))
14 (defn rand-weight [] (- (rand 2) 1))
16 (defn mean [lst] (if (empty? lst) 0 (/ (reduce + lst) (count lst))))
18 (defn stddev [lst]
19   (let [m (mean lst)]
20     (sqrt (/ (reduce + (map (fn [el] (expt (- m el) 2)) lst))
21              (dec (count lst))))))
23 (defn rms-error "Collect the error from an epic"
24   ([net epic] (rms-error net epic identity))
25   ([net epic pull]
26      (sqrt (/ (reduce + (map (fn [[x d]] (expt (- (pull (run net x)) d) 2)) epic))
27               (* (count epic) (if (coll? (second (first epic)))
28                                 (count (second (first epic)))
29                                 1))))))