(deftest learning
  ((setq p *restaurant-boolean-problem*))
  ((setq e (learning-problem-examples p) 
         g (learning-problem-goals p)))
  ((setq h (decision-list-learning 4 p)))
  ((accuracy h #'dlpredict e g)
   (= * 1.0))
  ((setq p *restaurant-multivalued-problem*))
  ((setq e (learning-problem-examples p) 
         g (learning-problem-goals p)))
  ((setq h (decision-tree-learning p)))
  ((accuracy h #'dtpredict e g)
   (= * 1.0))
  ((setq p *majority-boolean-problem*))
  ((setq e (learning-problem-examples p) 
         a (learning-problem-attributes p) 
         g (learning-problem-goals p)))
  ((setq h (nn-learning p (make-perceptron 11 1) #'perceptron-update)))
  ((accuracy h #'(lambda (e1 h1) (nn-output e1 h1 a g)) e g)
   (> * 0.8))
  ((setq p *restaurant-real12-problem*))
  ((setq e (learning-problem-examples p) 
         a (learning-problem-attributes p) 
         g (learning-problem-goals p)))
  ((setq h (nn-learning p (make-connected-nn '(10 4 1)) #'backprop-update)))
  ((accuracy h #'(lambda (e1 h1) (nn-output e1 h1 a g)) e g #'rms-error)
   (> * 0.8))
  ((setq a (make-mdp-agent
	     :name 'LM :program (make-passive-lms-learner))))
  ((setq e (make-mdp-environment :mdp *4x3-passive-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs
  ((setq a (make-mdp-agent
	     :name 'TD :program (make-passive-td-learner))))
  ((setq e (make-mdp-environment :mdp *4x3-passive-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs
  ((setq a (make-mdp-agent :name 'MA 
	     :program (make-maximizing-adp-learner '(left right up down)))))
  ((setq e (make-mdp-environment :mdp *4x3-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs
  ((setq a (make-mdp-agent :name 'QI
	     :program (make-maximizing-qi-learner '(left right up down)))))
  ((setq e (make-mdp-environment :mdp *4x3-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs
  ((setq a (make-mdp-agent :name 'EA
	     :program (make-exploring-adp-learner '(left right up down)))))
  ((setq e (make-mdp-environment :mdp *4x3-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs
  ((setq a (make-mdp-agent :name 'TQ
	     :program (make-exploring-tdq-learner '(left right up down)))))
  ((setq e (make-mdp-environment :mdp *4x3-mdp* :agents (list a)
				 :epochs-left 2)))
  ((run-environment e))  ;;; for now just make sure it runs

  "Nonparametric models: k-nearest neighbors on the iris data."
  ((setq iris-h (nearest-neighbors-learning *iris-problem* :k 3)))
  ((knn-predict iris-h '((sepal-length . 5) (sepal-width . 3)
                         (petal-length . 1) (petal-width . 0.1)))
   '(setosa))
  ((knn-predict iris-h '((sepal-length . 6) (sepal-width . 3)
                         (petal-length . 4) (petal-width . 1.1)))
   '(versicolor))
  ((knn-predict iris-h '((sepal-length . 7.5) (sepal-width . 4)
                         (petal-length . 6) (petal-width . 2)))
   '(virginica))
  "Naive Bayes: Gaussian on the iris data, discrete on the restaurant data."
  ((setq iris-h (naive-bayes-learning *iris-problem*)))
  ((nb-predict iris-h '((sepal-length . 5) (sepal-width . 3)
                        (petal-length . 1) (petal-width . 0.1)))
   '(setosa))
  ((nb-predict iris-h '((sepal-length . 6) (sepal-width . 5)
                        (petal-length . 3) (petal-width . 1.5)))
   '(versicolor))
  ((nb-predict iris-h '((sepal-length . 7) (sepal-width . 3)
                        (petal-length . 6.5) (petal-width . 2)))
   '(virginica))
  ((accuracy (naive-bayes-learning *restaurant-multivalued-problem*) #'nb-predict
             (learning-problem-examples *restaurant-multivalued-problem*)
             (learning-problem-goals *restaurant-multivalued-problem*))
   (approximately-equal * 5/6))
  "Linear regression: in closed form, and by gradient descent on the points"
  "of the plane y = 3 + 2a - b."
  ((multiple-value-list (univariate-linear-regression '(1 2 3 4) '(3 5 7 9)))
   '(1 2))
  ((linear-regression-learning
    (make-learning-problem
     :attributes '((a) (b)) :goals '((y))
     :examples (loop for a from 0 to 2
                     nconc (loop for b from 0 to 2
                                 collect (list (cons 'y (+ 3 (* 2 a) (- b)))
                                               (cons 'a a) (cons 'b b))))))
   (approximately-equal * '(3 2 -1) 0.01))
  "Model selection (Figure 19.8) of the k of k-NN by cross-validation."
  ((setq knn-learner
         #'(lambda (size examples)
             (nearest-neighbors-learning
              (make-learning-problem
               :examples examples
               :attributes (learning-problem-attributes *iris-problem*)
               :goals (learning-problem-goals *iris-problem*))
              :k size))))
  ((cross-validation knn-learner 3 (learning-problem-examples *iris-problem*) 5
                     #'knn-predict (learning-problem-goals *iris-problem*))
   (< * 0.15))
  ((multiple-value-list
    (model-selection knn-learner (learning-problem-examples *iris-problem*) 5
                     #'knn-predict (learning-problem-goals *iris-problem*)))
   (destructuring-bind (h err size) *
     (and (knn-hypothesis-p h) (< err 0.2) (>= size 1))))
  "Ensembles: a random forest, and AdaBoost (Figure 19.25) with decision"
  "stumps, on the majority function of 11 Boolean inputs."
  ((accuracy (random-forest-learning *majority-boolean-problem* :n-trees 25)
             #'random-forest-predict
             (learning-problem-examples *majority-boolean-problem*)
             (learning-problem-goals *majority-boolean-problem*))
   (> * 0.7))
  ((accuracy (adaboost *majority-boolean-problem* #'stump-learning #'stump-predict 20)
             #'boosted-predict
             (learning-problem-examples *majority-boolean-problem*)
             (learning-problem-goals *majority-boolean-problem*))
   (> * 0.9))
  "The weighted-majority algorithm, with one expert per input, makes at"
  "most (m* log(1/beta) + log K) / log(2/(1 + beta)) mistakes."
  ((multiple-value-list
    (weighted-majority
     (mapcar #'(lambda (a) #'(lambda (e) (attribute-value a e)))
             (learning-problem-attributes *majority-boolean-problem*))
     (learning-problem-examples *majority-boolean-problem*)
     (first (learning-problem-goals *majority-boolean-problem*))))
   (destructuring-bind (mistakes weights expert-mistakes) *
     (declare (ignore weights))
     (<= mistakes (/ (+ (* (reduce #'min expert-mistakes) (log 2)) (log 11))
                     (log (/ 2 1.5))))))
  "Clustering two blobs of points around (0 0) and (10 10)."
  ((setq blobs (loop for (cx cy) in '((0 0) (10 10))
                     nconc (loop for i from -2 to 2
                                 nconc (loop for j from -2 to 2
                                             collect (list (+ cx (* 0.4 i))
                                                           (+ cy (* 0.4 j))))))))
  ((multiple-value-list (k-means blobs 2))
   (destructuring-bind (centers assignment) *
     (and (approximately-equal (sort (copy-list centers) #'< :key #'first)
                               '((0 0) (10 10)))
          (= 1 (length (remove-duplicates (subseq assignment 0 25))))
          (= 1 (length (remove-duplicates (subseq assignment 25))))
          (/= (first assignment) (car (last assignment))))))
  ((multiple-value-list (em-gaussian-mixture blobs 2))
   (destructuring-bind (weights means variances responsibilities) *
     (declare (ignore variances responsibilities))
     (and (approximately-equal weights '(0.5 0.5) 0.01)
          (approximately-equal (sort (copy-list means) #'< :key #'first)
                               '((0 0) (10 10)) 0.01))))
  "Current-best-hypothesis search on the restaurant examples, starting from"
  "Alternate = Yes, and version spaces."
  ((current-best-learning (learning-problem-examples *restaurant-multivalued-problem*)
                          '(((alternate . yes)))
                          (learning-problem-attributes *restaurant-multivalued-problem*)
                          (first (learning-problem-goals *restaurant-multivalued-problem*)))
   (hypothesis-consistent? * (learning-problem-examples *restaurant-multivalued-problem*)
                           (first (learning-problem-goals *restaurant-multivalued-problem*))))
  ((version-space-learning (learning-problem-examples *party-problem*)
                           (learning-problem-attributes *party-problem*)
                           (first (learning-problem-goals *party-problem*)))
   '(((pizza . yes))))
  ((version-space-learning (learning-problem-examples *animals-umbrellas-problem*)
                           (learning-problem-attributes *animals-umbrellas-problem*)
                           (first (learning-problem-goals *animals-umbrellas-problem*)))
   'nil)
  "Minimal consistent determinations."
  ((minimal-consistent-det (learning-problem-examples *party-problem*) '(pizza soda)
                           (first (learning-problem-goals *party-problem*)))
   '(pizza))
  ((minimal-consistent-det (subseq (learning-problem-examples *party-problem*) 0 2)
                           '(pizza soda) (first (learning-problem-goals *party-problem*)))
   'nil)
  ((minimal-consistent-det (learning-problem-examples *animals-umbrellas-problem*)
                           '(species rain coat)
                           (first (learning-problem-goals *animals-umbrellas-problem*)))
   '(species rain coat))
  ((minimal-consistent-det (learning-problem-examples *conductance-problem*)
                           '(mass temperature material size)
                           (first (learning-problem-goals *conductance-problem*)))
   '(temperature material))
  ((minimal-consistent-det (learning-problem-examples *conductance-problem*)
                           '(mass temperature size)
                           (first (learning-problem-goals *conductance-problem*)))
   '(mass temperature size))
  "FOIL learns Parent from Father and Mother, and then Grandparent."
  ((setq parent-clauses
         (foil (xy-bindings '((elizabeth anne) (elizabeth andrew) (philip anne)
                              (philip andrew) (anne peter) (anne zara) (mark peter)
                              (mark zara) (andrew beatrice) (andrew eugenie)
                              (sarah beatrice) (sarah eugenie)))
               (xy-bindings '((anne eugenie) (beatrice eugenie) (mark elizabeth)
                              (beatrice philip)))
               '(parent ?x ?y) *family-facts*)))
  ((mapcar #'rest parent-clauses)
   (and (= (length *) 2)
        (subsetp * '(((father ?x ?y)) ((mother ?x ?y))) :test #'equal)))
  ((foil (xy-bindings '((elizabeth peter) (elizabeth zara) (elizabeth beatrice)
                        (elizabeth eugenie) (philip peter) (philip zara)
                        (philip beatrice) (philip eugenie)))
         (xy-bindings '((anne eugenie) (beatrice eugenie) (elizabeth andrew)
                        (elizabeth anne) (elizabeth mark) (elizabeth sarah)
                        (philip anne) (philip andrew) (anne peter) (anne zara)
                        (mark peter) (mark zara) (andrew beatrice) (andrew eugenie)
                        (sarah beatrice) (mark elizabeth) (beatrice philip)
                        (peter andrew) (zara mark) (peter anne) (zara eugenie)))
         '(grandparent ?x ?y)
         (append *family-facts*
                 (mapcan #'(lambda (clause) (clause-facts clause *family-facts*))
                         parent-clauses)))
   '(((grandparent ?x ?y) (parent ?x ?v3) (parent ?v3 ?y))))
  "A SARSA agent in the 4x3 world: the Q-values stay below the best return."
  ((setq sarsa-q (make-hash-table :test #'equal)))
  ((setq a (make-mdp-agent :name 'SA
                           :program (make-exploring-sarsa-learner '(left right up down)
                                                                  sarsa-q))))
  ((setq e (make-mdp-environment :mdp *4x3-mdp* :agents (list a) :epochs-left 5)))
  ((run-environment e))
  ((gethash '(1 1) sarsa-q)
   (and * (every #'(lambda (pair) (<= (cdr pair) 1.0001)) *)))
  "The data sets read from aima-data: 150 iris flowers and 12 restaurants."
  ((length (learning-problem-examples *iris-problem*)) 150)
  ((first (learning-problem-examples *iris-problem*))
   '((class . setosa) (sepal-length . 5.1) (sepal-width . 3.5)
     (petal-length . 1.4) (petal-width . 0.2)))
  ((first (learning-problem-examples *restaurant-multivalued-problem*))
   '((willwait . yes) (alternate . yes) (bar . no) (fri/sat . no) (hungry . yes)
     (patrons . some) (price . ddd) (raining . no) (reservation . yes)
     (type . french) (waitestimate . 0)))
  ((second (learning-problem-examples *restaurant-real12-problem*))
   '((willwait . 0) (alternate . 1) (bar . 0) (fri/sat . 0) (hungry . 1)
     (patrons . 1) (price . 0) (raining . 0) (reservation . 0) (type . 0.33)
     (waitestimate . 0.5)))
  "A learning problem (a data set): examples, attributes and goals."
  ((setq ds (make-learning-problem :examples '(((y . 1) (a . 0)) ((y . 0) (a . 1)))
                                   :attributes '((a 0 1)) :goals '((y 0 1)))))
  ((mapcar #'(lambda (e1) (attribute-value '(a) e1)) (learning-problem-examples ds)) '(0 1))
  ((attribute-values (first (learning-problem-goals ds))) '(0 1))
  ((format nil "~A" ds) "#<LEARNING-PROBLEM with 2 examples and 1 attributes>")
  "Learning curves: a learner that knows the goal is right at every size;"
  "decision trees on the restaurant data, at 4 and 8 training examples."
  ((setq p *restaurant-multivalued-problem*))
  ((learning-curve #'(lambda (examples attributes goals)
                       (declare (ignore examples attributes goals))
                       'oracle)
                   #'(lambda (h1 e1) (declare (ignore h1)) (list (attribute-value '(willwait) e1)))
                   (learning-problem-examples p) (learning-problem-attributes p)
                   (learning-problem-goals p) 2 2)
   '((2 . 1.0) (4 . 1.0) (6 . 1.0) (8 . 1.0) (10 . 1.0)))
  ((learning-curve #'(lambda (examples attributes goals) (dtl examples attributes (first goals)))
                   #'dtpredict (learning-problem-examples p) (learning-problem-attributes p)
                   (learning-problem-goals p) 2 4)
   (and (equal (mapcar #'car *) '(4 8))
        (every #'(lambda (point) (<= 0 (cdr point) 1)) *)))
  "Perceptron learning finds the separable AND, but not the XOR."
  ((setq p *and-boolean-problem* e (learning-problem-examples p)
         a (learning-problem-attributes p) g (learning-problem-goals p)))
  ((accuracy (perceptron-learning p) #'(lambda (h1 e1) (nn-output h1 e1 a g)) e g) 1.0)
  ((accuracy (perceptron-learning *xor-boolean-problem*)
             #'(lambda (h1 e1) (nn-output h1 e1 a g))
             (learning-problem-examples *xor-boolean-problem*) g)
   (< * 1.0))
  "Back-propagation learning of AND by a network with a hidden layer."
  ((setq h (backprop-learning p)))
  ((length h) 2)
  ((accuracy h #'(lambda (h1 e1) (nn-output h1 e1 a g)) e g #'rms-error) (> * 0.8))
  "Logistic regression separates AND: rounded, its probabilities are AND."
  ((setq w (logistic-regression-learning p)))
  ((mapcar #'(lambda (e1) (round (logistic-predict w e1 a))) e) '(0 0 0 1))
  "Support vector machines: the maximum-margin separator of two points, and of"
  "two triangles (w = (2/3, 2/3), b = -5/3); XOR with a Gaussian kernel."
  ((setq svm (svm-learning '((1 1) (-1 -1)) '(1 -1) :c 10.0)))
  ((list (svm-weights svm) (svm-b svm)) (approximately-equal * '((0.5 0.5) 0) 1e-6))
  ((setq svm (svm-learning '((2 2) (2 3) (3 2) (0 0) (1 0) (0 1)) '(1 1 1 -1 -1 -1)
                           :c 100.0)))
  ((list (svm-weights svm) (svm-b svm)) (approximately-equal * '((2/3 2/3) -5/3) 1e-4))
  ((support-vectors svm) '((2 2) (1 0) (0 1)))
  ((mapcar #'(lambda (x) (svm-predict svm x)) '((3 3) (0 0) (1 2) (1 1))) '(1 -1 1 -1))
  ((setq svm (svm-learning '((0 0) (0 1) (1 0) (1 1)) '(-1 1 1 -1)
                           :kernel (make-rbf-kernel 1.0) :c 10.0)))
  ((mapcar #'(lambda (x) (svm-predict svm x)) '((0 0) (0 1) (1 0) (1 1))) '(-1 1 1 -1))
  "Support vector regression of y = 2x + 1: every error within epsilon."
  ((setq svr (svr-learning '((0) (1) (2) (3) (4)) '(1 3 5 7 9) :c 10 :epsilon 0.1)))
  ((mapcar #'(lambda (x) (svr-predict svr x)) '((0) (1) (2) (3) (4)))
   (approximately-equal * '(1 3 5 7 9) 0.11))
  ((- (svr-predict svr '(5)) (svr-predict svr '(0))) (approximately-equal * 10 0.01))
  "Ensembles of different learners vote: two decision trees outvote naive"
  "Bayes, and two naive Bayes models outvote a tree."
  ((setq p *restaurant-multivalued-problem* e (learning-problem-examples p)
         g (learning-problem-goals p)))
  ((accuracy (ensemble-learning p (list (cons #'decision-tree-learning #'dtpredict)
                                        (cons #'naive-bayes-learning #'nb-predict)
                                        (cons #'decision-tree-learning #'dtpredict)))
             #'ensemble-predict e g)
   1.0)
  ((accuracy (ensemble-learning p (list (cons #'naive-bayes-learning #'nb-predict)
                                        (cons #'decision-tree-learning #'dtpredict)
                                        (cons #'naive-bayes-learning #'nb-predict)))
             #'ensemble-predict e g)
   (approximately-equal * 5/6))
  ((weighted-replicate '(a b c) '(1 2 1) 4) '(a b b c))
  ((weighted-replicate '(a b) '(1 1) 3)
   (and (= (length *) 3) (member 'a *) (member 'b *)))
  "Gaussian naive Bayes treats the values as numbers, even when listed."
  ((setq gnb (gaussian-naive-bayes-learning
              (make-learning-problem
               :attributes '((x 1 2 3 7 8 9)) :goals '((c a b))
               :examples '(((c . a) (x . 1)) ((c . a) (x . 2)) ((c . a) (x . 3))
                           ((c . b) (x . 7)) ((c . b) (x . 8)) ((c . b) (x . 9)))))))
  ((mapcar #'(lambda (x) (nb-predict gnb (list (cons 'x x)))) '(4 6 10)) '((a) (b) (b)))
  ((accuracy (gaussian-naive-bayes-learning *iris-problem*) #'nb-predict
             (learning-problem-examples *iris-problem*) (learning-problem-goals *iris-problem*))
   (> * 0.9))
  "A probability distribution estimated by counting, without and with"
  "add-one smoothing."
  ((setq counts (make-counting-distribution '(a b a c a))))
  ((counting-probability counts 'a) 3/5)
  ((counting-distribution-top counts 2) '((3 . a) (1 . b)))
  ((setq counts (make-counting-distribution '(a b a) 1)))
  ((counting-probability counts 'a) 3/5)
  ((counting-probability counts 'z) 1/6)
  ((counting-distribution-sample counts) (member * '(a b z)))
  "EM on the 1000 candies of Section 20.3.2 (flavor, wrapper, hole) from the"
  "initial guess of the book: the parameters after one iteration."
  ((setq candies (loop for (x n) in '(((1 1 1) 273) ((1 1 0) 93) ((1 0 1) 104) ((1 0 0) 90)
                                      ((0 1 1) 79) ((0 1 0) 100) ((0 0 1) 94) ((0 0 0) 167))
                       nconc (make-list n :initial-element x))))
  ((multiple-value-list (naive-bayes-em candies 2 :weights '(0.6 0.4)
                                        :theta '((0.6 0.6 0.6) (0.4 0.4 0.4))
                                        :max-iterations 1))
   (approximately-equal (subseq * 0 2)
                        '((0.6124 0.3876) ((0.6684 0.6483 0.6558) (0.3887 0.3817 0.3827)))
                        1e-3))
  "Baum-Welch: an alternating umbrella sequence is learned as a deterministic"
  "alternation, and one iteration increases the likelihood of the data."
  ((setq umbrella (make-hmm :transition '((0.7d0 0.3d0) (0.3d0 0.7d0))
                            :sensor '((t 0.9d0 0.2d0) (nil 0.1d0 0.8d0))
                            :prior '(0.5d0 0.5d0))))
  ((setq alternating (loop repeat 10 append (list t nil))))
  ((setq learned (baum-welch umbrella alternating :iterations 20)))
  ((hmm-transition learned) (approximately-equal * '((0 1) (1 0)) 0.01))
  ((hmm-log-likelihood learned alternating) (> * -0.01))
  ((setq umbrella-days '(t t nil t t nil nil t t t nil nil nil t t)))
  ((hmm-log-likelihood (baum-welch umbrella umbrella-days :iterations 1) umbrella-days)
   (> * (hmm-log-likelihood umbrella umbrella-days)))
  "A passive ADP agent learns the utilities of the random walk of the 4x3"
  "world: U(4,3) = 1, and U(1,1) = -0.2911 for the true model."
  ((setq adp-u (make-hash-table :test #'equal)))
  ((setq a (make-mdp-agent :name 'ADP :program (make-passive-adp-learner adp-u))))
  ((setq e (make-mdp-environment :mdp *4x3-passive-mdp* :agents (list a) :epochs-left 100)))
  ((run-environment e))
  ((gethash '(4 3) adp-u) 1)
  ((gethash '(1 1) adp-u) (approximately-equal * -0.2911 0.15))
  "Q-iteration on the 4x3 world: the best Q-value of (1 1) is U(1,1) = 0.705,"
  "for the action Up; an active QI learner chooses one of its actions."
  ((setq q (q-iteration *4x3-mdp*)))
  ((cdr (the-biggest #'cdr (gethash '(1 1) q))) (approximately-equal * 0.7053 0.002))
  ((q-dmax-choice '(1 1) q) 'up)
  ((funcall (make-active-qi-learner '(left right up down) #'q-dmax-choice)
            (make-mdp-percept :state '(1 1) :reward -0.04 :terminalp nil))
   (member * '(left right up down)))
  "Convolution (Figure 21.4: kernel (+1 -1 +1), stride 2), pooling and batch"
  "normalization."
  ((convolution-1d '(5 6 6 2 5 6 5) '(1 -1 1) :stride 2) '(5 9 4))
  ((convolution-1d '(1 2 3 4) '(1 1)) '(3 5 7))
  ((max-pooling-1d '(1 3 2 5 4) 2) '(3 3 5 5))
  ((average-pooling-1d '(1 3 2 6) 2 :stride 2) '(2 4))
  ((batch-normalize '(1 2 3) :epsilon 0) (approximately-equal * '(-1.2247 0 1.2247) 1e-4))
  ((batch-normalize '(1 2 3) :gamma 2 :beta 1 :epsilon 0)
   (approximately-equal * '(-1.4495 1 3.4495) 1e-4))
  "Stochastic gradient descent and Adam fit y = 3 + 2x."
  ((setq line-gradient
         #'(lambda (w batch)
             (let ((g (list 0 0)))
               (dolist (xy batch g)
                 (let ((err (- (+ (first w) (* (second w) (first xy))) (second xy))))
                   (setf g (list (+ (first g) err) (+ (second g) (* err (first xy)))))))))))
  ((setq line (loop for x from 0 to 4 collect (list x (+ 3 (* 2 x))))))
  ((stochastic-gradient-descent line-gradient '(0 0) line :learning-rate 0.05 :epochs 500)
   (approximately-equal * '(3 2) 1e-3))
  ((stochastic-gradient-descent line-gradient '(0 0) line :learning-rate 0.02 :epochs 1000
                                :batch-size 2)
   (approximately-equal * '(3 2) 1e-3))
  ((adam line-gradient '(0 0) line :learning-rate 0.1 :epochs 500)
   (approximately-equal * '(3 2) 1e-3))
  "A recurrent network learns to output its previous input, by"
  "back-propagation through time on all the binary sequences of length 5."
  ((setq delay-data
         (loop for k from 0 below 32
               collect (let ((bits (loop for i from 0 below 5 collect (ldb (byte 1 i) k))))
                         (list (mapcar #'list bits)
                               (mapcar #'list (cons 0 (butlast bits))))))))
  ((setq rnn (make-simple-rnn 1 3 1)))
  ((setq rnn-initial-loss (rnn-loss rnn delay-data)))
  ((rnn-learning rnn delay-data :learning-rate 0.05 :epochs 200))
  ((rnn-loss rnn delay-data) (< * (* 0.01 rnn-initial-loss)))
  ((every #'(lambda (example)
              (equal (mapcar #'(lambda (y) (round (first y))) (rnn-outputs rnn (first example)))
                     (mapcar #'first (second example))))
          delay-data)
   *)
  "Autoencoders of points on a plane in 4 dimensions: 2 linear hidden units"
  "reconstruct them, 1 cannot."
  ((setq plane (loop for a1 in '(-1 0 1)
                     nconc (loop for b1 in '(-1 0 1)
                                 collect (list (/ a1 2) (/ b1 2) (/ (+ a1 b1) 2) (/ (- a1 b1) 2))))))
  ((autoencoder-reconstruction-error
    (autoencoder-learning plane 2 :g #'identity :dg #'(lambda (x) (declare (ignore x)) 1))
    plane)
   (< * 1e-6))
  ((autoencoder-reconstruction-error
    (autoencoder-learning plane 1 :g #'identity :dg #'(lambda (x) (declare (ignore x)) 1))
    plane)
   (> * 1))
  ((autoencoder-reconstruction-error (autoencoder-learning plane 2) plane) (< * 1e-3))
  )
