Skip to content

Instantly share code, notes, and snippets.

@sritchie
Created January 17, 2012 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sritchie/1624010 to your computer and use it in GitHub Desktop.
Save sritchie/1624010 to your computer and use it in GitHub Desktop.
(defn feature-vec
[n]
(map (partial cons 1)
(for [x (range n)]
(take 22 (repeatedly rand)))))
(defn label-vec
[n]
(for [x (range n)] (if (> (rand) 0.5) 1 0)))
(defn scaled-vector
[scalar coll]
(map #(* scalar %) coll))
(defn logistic-fn
[x]
(let [exp-x (Math/exp x)]
(/ exp-x (inc exp-x))))
(defn dot-product
[x y]
(reduce + (map * x y)))
(defn logistic-prob
[beta-seq x]
(logistic-fn (dot-product beta-seq x)))
(defn log-likelihood
[beta-seq label x]
(let [prob (logistic-prob beta-seq x)]
(+ (* label (Math/log prob))
(* (- 1 label) (Math/log (- 1 prob))))))
(defn total-log-likelihood
"returns the total log likelihood for a group of pixels; input
labels and features for the group of pixels, aligned correctly so
that the first label and feature correspond to the first pixel."
[iterations batch-size beta-seq labels features]
(loop [iter iterations ls labels fs features]
(when (pos? iter)
(time (dorun (map (partial log-likelihood beta-seq)
(take batch-size ls)
(take batch-size fs))))
(recur (- iter batch-size)
(drop batch-size ls)
(drop batch-size fs)))))
(def X-rand (feature-vec 10000000))
(def y-rand (label-vec 10000000))
(def beta (repeat 22 0))
(total-log-likelihood 100000 25000 beta y-rand X-rand)
"Elapsed time: 343.759 msecs"
"Elapsed time: 408.516 msecs"
"Elapsed time: 333.686 msecs"
"Elapsed time: 371.483 msecs"
user> (total-log-likelihood 200000 25000 beta y-rand X-rand)
"Elapsed time: 372.326 msecs"
"Elapsed time: 356.37 msecs"
"Elapsed time: 362.232 msecs"
"Elapsed time: 342.627 msecs"
"Elapsed time: 1010.623 msecs"
"Elapsed time: 1181.387 msecs"
"Elapsed time: 1388.509 msecs"
"Elapsed time: 1322.818 msecs"
user> (total-log-likelihood 500000 25000 beta y-rand X-rand)
"Elapsed time: 370.097 msecs"
"Elapsed time: 389.351 msecs"
"Elapsed time: 422.98 msecs"
"Elapsed time: 429.956 msecs"
"Elapsed time: 453.33 msecs"
"Elapsed time: 419.977 msecs"
"Elapsed time: 374.716 msecs"
"Elapsed time: 416.268 msecs"
"Elapsed time: 1494.244 msecs"
"Elapsed time: 2634.889 msecs"
"Elapsed time: 4269.544 msecs"
"Elapsed time: 4088.356 msecs"
"Elapsed time: 8938.812 msecs"
"Elapsed time: 21680.063 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment