11
Sep-22-15—1:43 PM 1 University of Florida EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo EEL5840: Elements of Machine Intelligence Rocko Announcements 2 University of Florida EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo EEL5840: Elements of Machine Intelligence Rocko Todays Menu Finish Introduction to ANNs (original material by Dr. Mike Nechyba) > Slides 58-60 > Adjusting the Learning Rate Loose Ends in LISP > Lambda Expressions Review > I/O Functions > PROG > LOOPing

EEL5840: Elements of Machine Intelligence Today s Menu · Sep-22-15—1:43 PM 3 University of Florida EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo EEL5840: Elements

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Sep-22-15—1:43 PM

1 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Announcements

2 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Today’s Menu

•  Finish Introduction to ANNs (original material by Dr. Mike Nechyba)

>  Slides 58-60 >  Adjusting the Learning Rate

•  Loose Ends in LISP >  Lambda Expressions Review >  I/O Functions >  PROG >  LOOPing

Sep-22-15—1:43 PM

3 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Finish Anns •  See Class 11 Notes Slides 34-36•  ANN Example

4 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  LAMBDA Expressions allow us to define anonymous procedures The lambda expression is the actual mechanism that the LISP interpreter uses to "evaluate" dummy arguments in a procedure. This is what XLISP returns when you use FUNCTION-LAMBDA-EXPRESSION. Suppose we want to put quotes around all the elements of an input list. (defun put-quotes(lis) (mapcar #'qhelp lis)) (defun qhelp(sex) (list (quote quote) sex)) > (put-quotes '(this is a test))

((QUOTE THIS) (QUOTE IS) (QUOTE A) (QUOTE TEST)) But if you begin to run out of names for your "helper" functions (especially if they are only used once in a program) a more elegant solution is given by: (defun put-quotes(lis) (mapcar #'(lambda(sex) (list (quote quote) sex)) lis ))

Sep-22-15—1:43 PM

5 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Lambda Expressions

 The actual mechanism that runs user-defined functions is the built-in lambda expression. For example, let us define a function to increment, say, inc.

>(defun inc(x) (+ 1 x)) INC >(inc 1) 2 >(function-lambda-expression #'inc) (lambda(x) (+ 1 x))  What is stored in the system when you use function inc is the symbolic expression (lambda(x) (+ 1 x)). That is, inc and (lambda(x) (+ 1 x)) are one in the same. To prove it, replace inc with (lambda(x) (+ 1 x)) in the expression (inc 1).

> ((lambda(x) (+ 1 x)) 1) 2

6 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Lambda Expressions

 Lambda expressions can be used as “anonymous functions” anywhere you would otherwise use a named function. For example, using inc we can increment a list of integers using the built-in function mapcar. The syntax for mapcar is (mapcar function-f list-of-arguments-for-f ). The result is (list (f (first of list)) (f (second of list)) (f (third of list)) etc.)

> (mapcar #’oddp '(1 2 3)) (t nil t) > (mapcar #'inc '(1 2 3)) (2 3 4) > (mapcar #'(lambda(x) (+ 1 x)) '(1 2 3)) (2 3 4)

This example shows that we did not need to do a defun to define inc and could just use the code for inc (its corresponding lambda expression) in the same manner as we used inc. This is more efficient because we save the space and OH of having to define the function inc in order to use it inside mapcar.

Sep-22-15—1:43 PM

7 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Lambda Expressions

 In some cases, to solve scoping problems we must use the lambda (anonymous function) instead of defun. For example, to do the intersection of two sets we need a helper function that checks to see if each element of s1 is in s2. If yes, it belongs in the intersection, if not, we do not keep it. Let me call the helper function finter. finter returns the element from s1 that is also inside s2 as a list or returns nil. > (defun inter(s1 s2) (mapcan #’finter s1))

> (defun finter (sex) (cond ( (member sex s2) (list sex)) ( t nil) )) For example if s1 is ‘(a b) and s2 is ‘(a c), (finter ‘a) would return ‘(a) and (finter ‘b) would return (). If I append these two answers, presto, I get the result of (inter ‘(a b) ‘(a c)) = (a)

8 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko Lambda Expressions

 But there is a problem. What is the s2 inside finter? You could say that s2 is “global” to finter. Using that line of reasoning we could write (inter s1 s2) as follows:

> (defun inter(s1 s2) (mapcan #’finter s1)) > (inter ‘(a b) ‘(a c)) ***ERROR***  What is the problem? When you call finter inside inter, s2 is known to inter but not to finter. The scope of s2 is inside the definition of inter and it is not made global to LISP. But as we said earlier the s2 in finter is global to finter. The LISP interpreter cannot resolve this problem. But if you use the anonymous version of finter inside inter, all is well.

> (defun inter(s1 s2) (mapcan #'(lambda (sex) (cond ( (member sex s2) (list sex)) ( t nil) )) s1)) > (inter ‘(a b) ‘(a c)) (A)

Sep-22-15—1:43 PM

9 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  General MAPing Functions MAPCAR and related mapping functions allow us to easily transform lists. MAPCAR takes two arguments, the first is a funarg and the second a list. The answer is a list of the results of applying the functional argument to each successive car of the 2nd argument. Thus, (mapcar #'f '(x1 x2 x3)) ==> (list f(x1) f(x2) f(x3)) template: (mapcar #'<fname> <a list of SEXes for fname>)

> (mapcar #’oddp '(1 2 3)) ==> (T NIL T)

We can define mapcar as follows: (defun mymapcar (f lis) (cond ( (null lis) nil ) ( t (cons (funcall f (car lis)) (mymapcar f (cdr lis))) ) )) > (mymapcar #'oddp '(1 2 3)) ==> (T NIL T)

10 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  User-Defined MAPing Functions Using our definition of MAPCAR we can define MAPPENDCAR, (MAPCAN) which applies f (.) to successive cdrs as follows:

(defun mappendcar (f lis) (cond ( (null lis) nil ) ( t (append (funcall f (car lis)) (mappendcar f (cdr lis))) ) ))

(defun inter (s1 s2) (mappendcar #'ihelp s1)) (defun ihelp (s1el) (cond ( (member s1el s2) (list s1el)) ( t nil) )) But this will not work either. Why? (Same as in Slide 7}

Sep-22-15—1:43 PM

11 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

> (inter '(a b) '(b c)) Error: The variable S2 is unbound. Happened in: #<Closure-IHELP: #76dc8c

The reason is obvious(?), s2 is only defined inside INTER but is undefined inside ihelp. We MUST use lambda:

> (defun inter (s1 s2) (mappendcar #'(lambda (s1el) (cond ( (member s1el s2) (list s1el))

( t nil))) s1)) INTER > (inter '(a b) '(b c)) (B)

12 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko

•  MAPing Functions Example Use a mapping function to transpose an N×N matrix

represented as N lists of N integers each, e.g., – Lisp→(setf mat '((1 2 3) (4 5 6) (7 8 9)))

( (1 2 3) (4 5 6) (7 8 9) ) –  Lisp→ (transpose mat )

( (1 4 7) (2 5 8) (3 6 9) ) –  Lisp→ (transpose '( (1 2) (3 4) (5 6) ) )

( (1 3 5) (2 4 6) )

> (mapcar #'car mat) (1 4 7)

> (mapcar #'cdr mat) ( (2 3) (5 6) (8 9) )

– Lisp→(defun transp (mat) (if (null (car mat)) nil (cons (mapcar #'car mat) (transp (mapcar #'cdr mat)))))

– Lisp→ (transp mat) ( (1 4 7) (2 5 8) (3 6 9) )

LISP

Sep-22-15—1:43 PM

13 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  Substop replaces a sex in a list at the top-level. (defun substop (this that lis) (cond ((null lis) nil) ((equal that (car lis)) (cons this (substop this that (cdr lis)))) ('else (cons (car lis) (substop this that (cdr lis)))) ))

•  Let’s try to write substop using a mapping function. (defun replacesex (sex3) (if (equal sex1 sex3) sex2 sex3)) /* sex1,2 are global! */ (defun substop (sex1 sex2 lis) (mapcar #'replacesex lis)) /* The following code will not run. Why? */ > (substop 'coke 'pepsi '(coke is it)) Error: The variable SEX1 is unbound.

Happened in: #<Closure-REPLACESEX: #794b20> •  A Lambda Expression let us fix this nicely

> (defun substop (sex1 sex2 lis) (mapcar #'(lambda (sex3) (if (equal sex1 sex3) sex2 sex3)) lis)) > (substop 'coke 'pepsi '(coke is it)) (PEPSI IS IT)

14 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP I/O

I/O Functions in LISP The following represents a sampling of the available I/O functions in LISP. For a complete list see the LISP Reference Manual. If the file pointers outf or inf are omitted, the system defaults to the terminal.

(setf outf (open "fname" :direction :output)) Opens disk file fname for output. Sets outf, i.e., a file pointer for further use. (print Sex outf) Returns the value of the Sex and sends a copy to outf. (dribble "fname") Echoes all CRT I/O to the disk file (prin1 Sex outf) Same as print except for the trailing cr/lf (terpri outf) Issues a cr/lf (read inf) Inputs the next Sex from inf (or CRT if inf omitted) (read-line inf) Read 1 line from inf (read-char inf) Read 1 character from inf (close outf) Close file outf (or inf)

Sep-22-15—1:43 PM

15 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

PROG The PROG construct allows for an indefinite number of forms to

be evaluated with local variables. In modern LISP it has been replaced by LET and LOOP constructs, but it is still useful.

(prog (var1 var2 ... varn) <forms>) 1. The number of forms in prog is indeterminate. 2. PROG initializes all its local variables to nil (var1 var2 ... varn). 3. <Forms> are evaluated in order except:

a. Atomic forms are not evaluated. They are treated as labels. b. (GO label) means continue from the form that follows label. c. (return expr) exits a prog evaluation & returns (eval expr).

4. If a prog runs out of forms, it returns nil. 5. If a cond runs out of stuff inside a prog it is treated as a NOOP.

16 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

Example: (defun fact (n) (prog (i j) (if (< n 0) (return nil) ) (setf i 1 j 1) ( if (< n 2) (return j) ) LOOP (setf i (+ i 1) j (* j i) ) (if (= i n) (return j)) (go LOOP) ))

Sep-22-15—1:43 PM

17 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

LOOPING Common LISP includes most(all) the most popular looping constructs

template: (dotimes (<count atm> <upperbound form> <result form>) <body>) > (defun power (m n) (let ((result 1)) ( dotimes (count n result) (setf result (* m result)) ) ))

Also template: (dolist (<element> <list form> <result form>) <body>)

> (dolist (n '(1 2 3) 'end) (prin1 (list 'n= n 'n+1= (+ 1 n))) (prin1 '!)) (N= 1 N+1= 2)!(N= 2 N+1= 3)!(N= 3 N+1= 4)!END

> (let ((n 0))(dolist (grade '((john a) (mary b) (james c) (dick a)) (list n 'non-a-grades)) (if (equal (cadr grade) 'a) (print (list 'a (car grade))) (setf n (+ 1 n))) ) )

* Prints all the A grades in the input list & counts the non-A grades * EVALUATIONS

(eval sex) hands sex to the interpreter if (setf x '(+ 1 2) ) then (eval x) returns 3

18 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

PROPERTY LISTS {Review} The ability to associate with an atom in LISP a lisp form (pvalue, the

property value) under a given atomic designation (pname, the property name). template: (putprop symb pvalue pname) {Requires 3 arguments} symb - must be atomic, i.e., the atom to receive the property. pvalue - the property value, which can be any sex. pname - must also be atomic, i.e., the property name. > (putprop 'cain 'adam 'father) ADAM > (putprop 'eve '(cain abel) 'mother-of ) (CAIN ABEL) > (setf eve 'first-woman) FIRST-WOMAN > eve FIRST-WOMAN

Sep-22-15—1:43 PM

19 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  Putprop is not always included in some LISP dialects. If not use:

> (setf (get 'cain 'father) 'adam) > (setf (get 'eve 'mother-of) '(cain abel)) > (setf (get 'eve 'grandmother) '(tubalcain jubal))

(defun putprop (atm pvalue pname) (setf (get atm pname) pvalue))

In memory, property lists are stored as association lists or dotted pairs associated with the atom, e.g.,

( eve (mother (cain abel)) (grandmother (tubalcain jubal)) )

20 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  To retrieve properties use the function (get atm pname). It returns the property value if one has been defined or nil. Therefore, the system cannot distinguish between undefined properties and those whose value has been set to nil. A good rule to follow is to always set pvalue to something non-nil > (get 'cain 'father) ADAM > (get 'eve 'mother-of) (CAIN ABEL) > (get 'cain 'grandfather) NIL

•  To remove a property use (remprop symb pname). Returns nil. > (remprop 'cain 'father) NIL > (get 'cain 'father) NIL

Sep-22-15—1:43 PM

21 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko LISP

•  Another way to view this is as an associated list of pairs (a-list for short). Associated pairs are lists of sublists where the first element of each sublist is a "key or indicator" and the cdr of the sublist is the value. For example: > (setq eve '( (mother (cain abel)) (grandmother (tubalcain jubal)) )) ((MOTHER (CAIN ABEL)) (GRANDMOTHER (TUBALCAIN JUBAL))) > eve ((MOTHER (CAIN ABEL)) (GRANDMOTHER (TUBALCAIN JUBAL))) The function (assoc key a-list) returns the corresponding pair that has a key value matching argument.

> (assoc 'mother ‘eve) (MOTHER (CAIN ABEL))

(defun myassoc (key alist) (cond ( (null alist) nil ) ( (equal key (caar alist)) (car alist) ) ( t (myassoc key (cdr alist)) ) ))

22 University of Florida

EEL 5840 – Class #13 – Fall 2015 © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence Rocko

The End!