Created
October 6, 2010 17:36
-
-
Save dakrone/613740 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
;; This is proof-of-concept, it's not idiomatic and needs a lot of refactoring, take it with a block of salt. | |
(ns gradient-descent.core) | |
;; Thetas with starting values | |
(def theta0 (atom 1)) | |
(def theta1 (atom 1)) | |
(def theta2 (atom 1)) | |
;; learning step rate | |
(def alpha 1e-9) | |
;; Dataoints gathered as example data | |
;; points = x0, x1, x2, y | |
;; x0 - constant, always 1 | |
;; x1 - square footage of house | |
;; x2 - # of bedrooms | |
;; y - price of the house | |
(def points [ | |
[1 2104 3 400000] | |
[1 1416 2 232000] | |
[1 1534 3 315000] | |
[1 852 2 178000] | |
[1 1940 4 240000] | |
]) | |
(defn h | |
"Given a datapoint, return the suspected price of the house" | |
[[x0 x1 x2 y]] | |
(+ | |
(* @theta0 x0) | |
(* @theta1 x1) | |
(* @theta2 x2))) | |
(defn h? | |
"Attempt to calculate a price for a house using arbitrary x0, x1 and x2 values" | |
[x0 x1 x2] | |
(+ | |
(* @theta0 x0) | |
(* @theta1 x1) | |
(* @theta2 x2))) | |
(defn single-j-theta | |
"Calculate a single J(theta) (without the summing)" | |
[i theta-index] | |
(* | |
(- (h (butlast i)) | |
(last i)) | |
(get i theta-index))) | |
(defn J-theta | |
"Given a theta-index to resolve, calculate J(theta)" | |
[theta-index] | |
(reduce + (map (fn [p] (single-j-theta p theta-index)) points))) | |
(defn new-theta | |
"Given a theta and the theta-index, return a new theta value" | |
[theta theta-index] | |
(- theta (* alpha (J-theta theta-index)))) | |
(defn set-thetas | |
"Update all thetas to new values" | |
[] | |
(let [newt0 (new-theta @theta0 0) | |
newt1 (new-theta @theta1 1) | |
newt2 (new-theta @theta2 2)] | |
#_(println "New thetas are:") | |
#_(println "t0:" newt0) | |
#_(println "t1:" newt1) | |
#_(println "t2:" newt2) | |
(reset! theta0 newt0) | |
(reset! theta1 newt1) | |
(reset! theta2 newt2))) | |
;;; Using this: | |
; user=> (use 'gradient-descent.core) | |
; nil | |
; user=> (dotimes [_ 100000] (set-thetas)) | |
; nil | |
;; A 1400 sq. foot house with 2 bedrooms should be priced what? | |
; user=> (h? 1 1400 2) | |
; 239468.77647838247 |
@aria42: I am! Aspiring to be able to understand your blog posts :)
Awesome best of luck! Make sure to also take CS228 with Daphne, that class taught me all I know.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Someone is taking CS229 Machine Learning with Andrew Ng! Recognize the example from years ago when I took it.