perceptron.lisp 959 B

12345678910111213141516171819202122232425262728293031323334
  1. (load "test-framework")
  2. (defstruct perceptron theta weights)
  3. (defun compute-class (perceptron inputs)
  4. (> (+ (perceptron-theta perceptron)
  5. (dot-product (perceptron-weights perceptron)
  6. inputs))
  7. 0.0))
  8. (defun dot-product (vector-1 vector-2)
  9. (apply #'+ (mapcar #'* vector-1 vector-2)))
  10. (defun bad-dot-product (vector-1 vector-2)
  11. (apply #'* (mapcar #'+ vector-1 vector-2)))
  12. (def-unit-tests
  13. dp1 ()
  14. (test (= 11 (dot-product '(1 2) '(3 4))) "test dp 1")
  15. (assert= 3 (dot-product '(1 0) '(3 4)) "test dp 2")
  16. (test (= 11 (bad-dot-product '(1 2) '(3 4))) "bad-dp"))
  17. (def-process-tests
  18. perceptron1 ()
  19. (let ((p (make-perceptron :theta 0.5 :weights '(0.2 0.7))))
  20. (assert-true (compute-class p '(3 4)))))
  21. (def-canonical-result-tests
  22. cr1 ()
  23. (let ((p (make-perceptron :theta 0.5 :weights '(0.2 0.7))))
  24. (assert-true (and (compute-class p '(0 1))
  25. (not (compute-class p '(0 -1)))))))