Skip to content

Instantly share code, notes, and snippets.

@adlai
Created December 22, 2014 06:36
Show Gist options
  • Save adlai/532de954e43e164a3014 to your computer and use it in GitHub Desktop.
Save adlai/532de954e43e164a3014 to your computer and use it in GitHub Desktop.
"Learning from Data" HW1
(defparameter *minimum* -1d0)
(defparameter *maximum* +1d0)
(defun random-point ()
(list (+ *minimum* (random (- *maximum* *minimum*)))
(+ *minimum* (random (- *maximum* *minimum*)))))
(defun random-line () (loop repeat 2 collect (random-point)))
(defun dot-product (a b) (reduce #'+ (mapcar #'* a b)))
(defun random-target-function ()
(let ((line (random-line))
(side (1- (* 2 (random 2)))))
(destructuring-bind (a b) line
(values (lambda (p)
(* side (signum (- (* (- (first b) (first a))
(- (second p) (second a)))
(* (- (second b) (second a))
(- (first p) (first a)))))))
line side))))
(defun random-dataset (function size)
(loop repeat size
for x = (random-point) collect (cons x (funcall function x))))
(defun eval-hypothesis (weights point)
(signum (dot-product weights (cons 1 point))))
(defun improve-weights (weights dataset)
(let ((misclassified (loop
for (x . y) in dataset
unless (= y (eval-hypothesis weights x))
collect (cons y (mapcar (lambda (xi) (* xi y)) x)))))
(if (null misclassified) weights
(let ((chosen (elt misclassified (random (length misclassified)))))
(mapcar #'+ weights chosen)))))
(defun perceptron (dataset &aux (weights (make-list 3 :initial-element 0)))
(loop for count from 0
do (let ((new (improve-weights weights dataset)))
(if (eq new weights) (return (values weights count))
(setf weights new)))))
(defun approximate-disagreement (function weights)
(/ (loop repeat 1000
for p = (random-point)
count (not (= (funcall function p)
(eval-hypothesis weights p))))
1000d0))
@adlai
Copy link
Author

adlai commented Dec 22, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment