Guest User

Untitled

a guest
Jul 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 9.50 KB | None | 0 0
  1. (defun fitness (weights)
  2.  
  3.  
  4.     (defparameter *NINPUTS* 2)
  5.     (defparameter *NHIDDEN* 1)
  6.     (defparameter *NOUTPUTS* 1)
  7.     (defparameter *NMAX* 3)
  8.  
  9.     (defparameter *NEXAMPLES* 4)
  10.     (defparameter *BETA* 1.0)
  11.     (defparameter *ETA-START* 0.1)
  12.     (defparameter *ETA-RUN-THRESH* 5)
  13.  
  14.     (defvar *w*)
  15.     (setf *w* (make-array (list 2 *nmax* *nmax*)
  16.                         :element-type 'float) )
  17.     ;;; Each weight is associated with the level of its tail endpoint,
  18.     ;;; and is thus referenced with with an expression of them form
  19.     ;;; (AREF *W* level from-node to-node).
  20.  
  21.     (defvar *increment*)
  22.     (setf *increment*
  23.           (make-array (list 2 *nmax* *nmax*)
  24.                       :element-type 'float) )
  25.     ;;; Used to accumulate the corrections to weights during an epoch.
  26.  
  27.     (defvar *activation*)
  28.     (setf *activation*
  29.           (make-array (list 3 *nmax*)
  30.                       :element-type 'float) )
  31.     ;;; Unit activations ( = g(h) ).
  32.  
  33.     (defvar *h*)
  34.     (setf *h* (make-array (list 3 *nmax*)
  35.                           :element-type 'float) )
  36.     ;;; Used to store the summed inputs to each unit.
  37.  
  38.     (defvar *delta*)
  39.     (setf *delta*
  40.           (make-array (list 3 *nmax*)
  41.                       :element-type 'float) )
  42.     ;;; Used during backpropagation.
  43.  
  44.     (defvar *gp*)
  45.     (setf *gp* (make-array (list 3 *nmax*)
  46.                            :element-type 'float) )
  47.     ;;; Used to store the derivative of G, that is, G prime,
  48.     ;;; evaluated on the H value of each node.
  49.  
  50.     (defvar *levelsize*)
  51.     (setf *levelsize*
  52.           (make-array '(3) :element-type 'integer) )
  53.  
  54.     (defvar *simlevelsize*)
  55.     (setf *simlevelsize*
  56.           (make-array '(3) :element-type 'integer) )
  57.  
  58.     (defvar *eta*)
  59.  
  60.     ;;; Here is the training set.
  61.     ;;; Each input vector is given on a separate line.
  62.  
  63.     (defun make-input-example (lst)
  64.       "Returns its arguments in an array."
  65.       (make-array (list *ninputs*)
  66.         :element-type 'float :initial-contents lst) )
  67.  
  68.     (defparameter *example-inputs*
  69.        (make-array (list *nexamples*)
  70.          :initial-contents (mapcar #'make-input-example '(
  71.  
  72.     ;;; xor inputs...
  73.       (0 0)
  74.       (0 1)
  75.       (1 0)
  76.       (1 1)
  77.      ) ) ) )
  78.  
  79.     (defun make-output-example (lst)
  80.       "Returns its arguments in an array."
  81.       (make-array (list *noutputs*)
  82.         :element-type 'float :initial-contents lst) )
  83.  
  84.     (defparameter *example-outputs*
  85.        (make-array (list *nexamples*)
  86.          :initial-contents (mapcar #'make-output-example '(
  87.         ;;; xor expected outputs
  88.       (0)
  89.       (1)
  90.       (1)
  91.       (0)
  92.        ) ) ) )
  93.  
  94.  
  95.  
  96.     (defun init ()
  97.       "Initializes the *LEVELSIZE* arrays and the weights."
  98.    
  99.       (setf (aref *levelsize* 0) (1+ *ninputs*))
  100.       (setf (aref *levelsize* 1) (1+ *nhidden*))
  101.       (setf (aref *levelsize* 2) *noutputs*)
  102.       ;;; Note that the input layer and the hidden layer each have an
  103.       ;;; extra unit, whose activation is always set to 1, and whose
  104.       ;;; outgoing weights serve as thresholds for other units.
  105.       (setf (aref *activation* 0 *ninputs*) 1.0)
  106.       (setf (aref *activation* 1 *nhidden*) 1.0)
  107.       ;;; The simulated numbers of units are kept, also:
  108.       (setf (aref *simlevelsize* 0) *ninputs*)
  109.       (setf (aref *simlevelsize* 1) *nhidden*)
  110.       (setf (aref *simlevelsize* 2) *noutputs*)
  111.     ;;; Level 1
  112.       (setf (aref *w* 0 0 0) (aref *weights* 0 0 0)) ;; 1st weight
  113.       (setf (aref *w* 0 1 0) (aref *weights* 0 1 0)) ;; 2nd weight
  114.       (setf (aref *w* 0 2 0) (aref *weights* 0 2 0)) ;; 3rd weight
  115.     ;;; Level 2
  116.       (setf (aref *w* 1 0 0) (aref *weights* 1 0 0)) ;; 1st weight
  117.       (setf (aref *w* 1 1 0) (aref *weights* 1 1 0)) ;; 2nd weight
  118.       ;;; Initialize the step size for weight adjustment:
  119.       (setf *eta* *eta-start*)
  120.       )
  121.  
  122.  
  123. (defun g (h)
  124.   "Returns the value of the sigmoid function at H."
  125.   (/ (1+ (exp (* -2.0 *beta* h)))) )
  126.  
  127. (defun feedforward (input-example)
  128.   "Determines unit activations for INPUT-EXAMPLE."
  129.   (let (sum gval (p input-example))
  130.     ;;; Copy input activations:
  131.     (dotimes (i *ninputs*)
  132.       (setf (aref *activation* 0 i) (aref p i)) )
  133.     ;;; Compute activations at next 2 levels:
  134.     (dotimes (level 2)
  135.       (dotimes (j (aref *simlevelsize* (1+ level)))
  136.         (setf sum 0.0)
  137.         (dotimes (i (aref *levelsize* level))
  138.           (incf sum (* (aref *activation* level i)
  139.                        (aref *w* level i j) )) )
  140.         (setf (aref *h* (1+ level) j) sum)
  141.         (setf gval (g sum))
  142.         (setf (aref *activation* (1+ level) j) gval)
  143.         (setf (aref *gp* (1+ level) j)
  144.               (* 2.0 *beta* gval (- 1.0 gval)) )
  145.         ) ) ) )
  146.  
  147. ;;; The following procedure takes one input/output pair,
  148. ;;; determines the error at each output using the current
  149. ;;; weights, and uses backpropagation to compute the changes
  150. ;;; that should be made to the weights.  These changes are
  151. ;;; added to the pre-existing collection of changes, maintained
  152. ;;; in the array *INCREMENT*.
  153. (defun backprop-one-example (input-example desired-output)
  154.   "Uses one I/O example to adjust the weights."
  155.   (let (sum example-error temp)
  156.     (feedforward input-example)
  157.     ;;; Compute *DELTA* values for output layer:
  158.     (dotimes (i *noutputs*)
  159.       (setf (aref *delta* 2 i)
  160.         (* (aref *gp* 2 i)
  161.            (- (aref desired-output i)
  162.               (aref *activation* 2 i) )) ) )
  163.     (let ((level 1))
  164.       ;;; Compute *INCREMENT* values for arcs coming
  165.       ;;; into output layer:
  166.       (dotimes (i (aref *levelsize* level))
  167.         (setf sum 0.0)
  168.         (dotimes (j (aref *simlevelsize* (1+ level)))
  169.           (incf sum (* (aref *w* level i j)
  170.                        (aref *delta* (1+ level) j) ))
  171.           (incf (aref *increment* level i j)
  172.             (* *eta*
  173.                (aref *delta* (1+ level) j)
  174.                (aref *activation* level i) ) ) )
  175.          ;;; Compute *DELTA* values for hidden layer:
  176.          (if (not (= i *nhidden*))
  177.            (setf (aref *delta* level i)
  178.                  (* (aref *gp* level i) sum) ) ) )
  179.       (setf level 0)
  180.       ;;; Compute *INCREMENT* values for hidden layer's
  181.       ;;; incoming arcs:
  182.       (dotimes (i (aref *levelsize* level))
  183.         (setf sum 0.0)
  184.         (dotimes (j (aref *simlevelsize* (1+ level)))
  185.           (incf sum (* (aref *w* level i j)
  186.                        (aref *delta* (1+ level) j) ))
  187.           (incf (aref *increment* level i j)
  188.             (* *eta*
  189.                (aref *delta* (1+ level) j)
  190.                (aref *activation* level i) ) ) ) ) )
  191.  
  192.     ;;; Compute the sum-squared error for this example:
  193.     (setf example-error 0.0)
  194.     (dotimes (i *noutputs* example-error)
  195.       (setf temp (- (aref desired-output i)
  196.                     (aref *activation* 2 i) ))
  197.       (incf example-error (* temp temp)) ) ) )
  198.  
  199. (defun show-weights ()
  200.   "Displays the current weights."
  201.   (dotimes (level 2)
  202.     (format t "~%Incoming weights for level ~2D:" (1+ level))
  203.     (dotimes (j (aref *simlevelsize* (1+ level)))
  204.       (format t "~%  For unit (~2d,~2d):~%"
  205.         (1+ level) j)
  206.       (dotimes (i (aref *levelsize* level))
  207.         (format t "~8,4F, " (aref *w* level i j)) )
  208.     ) ) )
  209.  
  210. (defun show-activations ()
  211.   "Displays the current unit activation levels."
  212.   (dotimes (level 2)
  213.     (dotimes (j (aref *simlevelsize* (1+ level)))
  214.       (format t "Activation for node (~2d,~2d) = ~6,3F.~%"
  215.         (1+ level) j (aref *activation* (1+ level) j) ) ) )
  216.   ;;; Report the input and output values:
  217.   (format t "The input vector is: ")
  218.   (dotimes (i *ninputs*)
  219.     (format t "~6,3F, " (aref *activation* 0 i)) )
  220.   (format t "~%The output vector is: ")
  221.   (dotimes (i *noutputs*)
  222.     (format t " ~6,3F, " (aref *activation* 2 i)) )
  223.  )
  224.  
  225. (defun clear-increments ()
  226.   "Sets the increments to zero for the beginning
  227.   of a new epoch."
  228.   (dotimes (level 2)
  229.     (dotimes (i *nmax*)
  230.       (dotimes (j *nmax*)
  231.         (setf (aref *increment* level i j) 0.0) ) ) ) )
  232.  
  233. (defun apply-increments ()
  234.   "Changes the weights according to the increments computed
  235.   during the epoch."
  236.   (dotimes (level 2)
  237.     (dotimes (i (aref *levelsize* level))
  238.       (dotimes (j (aref *simlevelsize* (1+ level)))
  239.         (incf (aref *w* level i j)
  240.               (aref *increment* level i j) ) ) ) ) )
  241.  
  242.  
  243.  
  244.     (defun training-epoch ()
  245.       "Makes a pass through all the training examples,
  246.        accumulating the increments to the weights until the end,
  247.        and finally adjusts the weights.
  248.        Returns the average system error for the examples."
  249.       (clear-increments)
  250.       (let ((sum 0.0))
  251.         (dotimes (i *nexamples*)
  252.           (incf sum
  253.                 (backprop-one-example
  254.                   (aref *example-inputs* i)
  255.                   (aref *example-outputs* i) ) ) )
  256.         (apply-increments)
  257.         (/ sum *nexamples*) ) )
  258.  
  259.  
  260.     (defun backprop ()
  261.       "Performs training of the neural network for many epochs."
  262.       (format t "Beginning neural net training with backpropagation.~%")
  263.       (init)
  264.       (show-weights)
  265.       (let (
  266.             (last-error 1.0)    ; an arbitrary high value.
  267.             system-error )
  268.           (setf system-error (training-epoch))
  269.           (setf last-error system-error)
  270.         (setf return last-error) ) )
  271.  
  272.  
  273.     (setf error (backprop))
  274. )
  275. (defvar *weights*)
  276. (setf *weights* (make-array (list 3 3 1) ;; 3 lists 3x3
  277.                     :element-type 'float) )
  278.     ;;; Level 1
  279.       (setf (aref *weights* 0 0 0) 0.1) ;; 1st weight
  280.       (setf (aref *weights* 0 1 0) 0.2) ;; 2nd weight
  281.       (setf (aref *weights* 0 2 0) 0.3) ;; 3rd weight
  282.     ;;; Level 2
  283.       (setf (aref *weights* 1 0 0) 0.4) ;; 1st weight
  284.       (setf (aref *weights* 1 1 0) 0.5) ;; 2nd weight
  285.     ;;; Fitness
  286.       (setf (aref *weights* 2 0 0) 1.0) ;; Start with fitness of 1
  287.    
  288.     (setf (aref *weights* 2 0 0) (* 500 (- 1 (fitness *weights*))))
  289. (format t "~%Fitness: ~9,6F" (aref *weights* 2 0 0))
Add Comment
Please, Sign In to add comment