; CSCI 4150 Introduction to Artificial Intelligence, Fall 2001 ; Assignment 7 support code, Version 1.0 ; Compute the output value of a perceptron ; ; The weights and input-vector lists should be the same length. ; Prepending a -1 to the beginning of the inputs to form the input ; vector must be done before this procedure is called. ; (define (perceptron weights input-vector) (define (dot-product a b) (apply + (map * a b))) (cond ((= (length weights) (length input-vector)) (if (> (dot-product weights input-vector) 0) 1 -1)) (else (error "Weights and input-vector must be of equal length in procedure perceptron!")))) ; Accessors for the training/testing data format, assuming it is ; structured as a list of training examples where each example is of ; the form: ; ; (output (input1 input2 ... inputN)) ; (define (td-answer te) (if (valid-example? te) (first te) (error "Invalid example given to procedure td-answer!"))) (define (td-inputs te) (if (valid-example? te) (second te) (error "Invalid example given to procedure td-inputs!"))) ; ; Tests a perceptron against a set of testing data. ; ; Returns the percentage of examples correctly classified correctly. ; (define (test-perceptron weights testing-data) ; the arguments to the helper procedure are the remaining training ; data (td) and the number of examples right and wrong so far... (define (tp td right) (if (null? td) (let ((fraction-correct (/ right (length testing-data)))) (printif "\nOut of " (length testing-data) " examples, " right " (" (round (* 100 fraction-correct)) " percent) were correctly classified.\n") (exact->inexact fraction-correct)) (let* ((correct-answer (td-answer (first td))) (input-vector (cons -1 (td-inputs (first td)))) (answer (perceptron weights input-vector)) (correct? (= answer correct-answer))) (tp (cdr td) (if correct? (+ 1 right) right))))) (if (and (valid-tdata? testing-data) (> (length testing-data) 0) (list? weights) (= (- (length weights) 1) (length (td-inputs (first testing-data))))) (tp testing-data 0) (error "Invalid input(s) given to procedure test-perceptron!"))) ; produce a list of N random numbers in the range of [-1, 1) ; (define (n-random#s N) (if (zero? N) '() (cons (- (random 2.0) 1) (n-random#s (- N 1))))) ; print (using display) all the arguments to this procedure ; (define (print . stuff) (if (not (null? stuff)) (begin (display (car stuff)) (apply print (cdr stuff))))) (define print-info #f) ; controls how much information is printed... (define (printif . stuff) (if print-info (apply print stuff))) ; some test for valid testing/training data/examples ; ; these don't check for everything but should catch simple mistakes... ; (define (valid-example? x) (and (list? x) (= (length x) 2) (number? (first x)) (list? (second x)))) (define (valid-tdata? td) (and (list? td) (all-equal? (map valid-example? td) #t))) ; are all the elements of Lst equal? to val ; (define (all-equal? Lst val) (define (ae remaining) (cond ((null? remaining) #t) ((equal? (car Lst) val) (ae (cdr remaining))) (else #f))) (ae Lst))