User:Kaufman/LISP Perceptron

From Noisebridge
Revision as of 22:33, 12 March 2009 by Kaufman (talk | contribs) (New page: 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: (perceptron) <code>...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to 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:

(perceptron)

(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)
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))
weight))

(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)
weight))))

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


(defun pushvalue (input weight direction)
(if (first input)
(progn
(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  )))