User:Kaufman/LISP Perceptron

From Noisebridge
Jump to: navigation, search

Over 3 days I taught myself just enough LISP to implement this.  :)

Copy the code block into your LISP interpreter (and hit enter, usually), and launch it by typing:


(defun perceptron ()
(setq inputs (list (list 1 0 0) (list 1 0 1) (list 1 1 0) (list 1 1 1) ))
(format t "inputs : ~a~%" inputs)
(setq answers (list 1 1 1 0))
(format t "answers : ~a~%" answers)
(setq weight (list 0.0 0.0 0.0))
(format t "starting weight : ~a~%" weight)
(mainfunc inputs answers weight))

(defun mainfunc (inputs answer weight)
(setq checked (check-all inputs weight answer))
(if (equal checked weight)
(mainfunc inputs answer checked))) 

(defun check-all (inputs weight answer)
(if (first inputs)
(check-all (rest inputs) (check-product (first inputs) weight (first answer)) (rest answer))

(defun check-product (inputs weight answer)
(if (> (dot-product inputs weight) 0.5)
(setq error (- answer 1))
(setq error (- answer 0)))
(if error 
(if (< error 0)
(pushvalue inputs weight -0.1)
(if (> error 0)
(pushvalue inputs weight 0.1)

(defun dot-product (input weight)
(setq product (map 'vector #'* input weight))
(setq sum (reduce #'+ product))

(defun pushvalue (input weight direction)
(if (first input)
(if (> (first input) 0) 
(setq newweight (+ (first weight) direction))
(setq newweight (first weight)))
(setq returnweight (append (list newweight) (pushvalue (rest input) (rest weight) direction)))
returnweight  )))