lisp – 如何为多变量方程推广此代码?

我是LISP的新手.我正在跟随Andrew Ng在Coursera的机器学习课程(第一周仍然).我想尝试在LISP中进行线性回归.

我编写了单变量线性回归的代码.代码似乎工作正常.我想对多变量线性函数进行推广.我想知道如何开始这样做.我想最终得到类似的东西:

(defun run-linear-regression alpha iterations training-set number-of-variables (...))

这反过来会创建一个假设生成函数,其中包含变量的输入数,这些假设的偏导函数等.

以下是我到目前为止的代码.我不需要任何人为我编写代码,但是如何去做我想要的一些指导将不胜感激.此外,我欢迎任何关于如何改进代码(性能,风格等)的一般性评论.

(defun make-hypothesis (theta1 theta2)
  (lambda (x) 
    (+ theta1 (* x theta2))))

(defun make-cost-function (hypothesis)
  (lambda (training-data)
    (let* ((x (car training-data)) (y (cadr training-data))
       (val (- (funcall hypothesis x) y)))
      (* val val))))

(defun make-J-1 (cost-function)
  (lambda (training-set) (float 
              (/ 
               (reduce #'+ (mapcar cost-function training-set)) 
               (* 2 (length training-set))))))


(defun make-J (theta1 theta2)
  (make-J-1 (make-cost-function (make-hypothesis theta1 theta2))))

(defun make-part-deriv-1 (hypothesis)
  (lambda (test-set)
    (let ((m (length test-set)))
      (float (/
          (reduce #'+ (mapcar (lambda(elem)(- (funcall hypothesis (car elem)) (cadr elem))) test-set))
          m)))))

(defun make-part-deriv-2 (hypothesis)
  (lambda (test-set)
    (let ((m (length test-set)))
      (float (/
          (reduce #'+ (mapcar (lambda(elem)(* (- (funcall hypothesis (car elem)) (cadr elem)) (funcall hypothesis (car elem)))) test-set))
          m)))))

(defun make-learn-fn (alpha theta1 theta2 make-part-deriv)
  (lambda (test-set) 
    (let* ((hypothesis (make-hypothesis theta1 theta2)) (pdv (funcall make-part-deriv hypothesis)))
      (* alpha (funcall pdv test-set)))))

(defun make-learners (alpha)
  (list 
   (lambda (theta1 theta2 test-set) (- theta1 (funcall (make-learn-fn alpha theta1 theta2 #'make-part-deriv-1) test-set)))
   (lambda (theta1 theta2 test-set) (- theta2 (funcall (make-learn-fn alpha theta1 theta2 #'make-part-deriv-2) test-set)))))

(defun run-linear-regression (alpha iterations training-set &optional (theta1 0) (theta2 0) (printer nil))
  (let ((t1 theta1) (t2 theta2))
    (dotimes (i iterations)
      (if (not (null printer))
      (funcall printer t1 t2))
      (let* ((funcs (make-learners alpha))
         (nt1 (funcall (car funcs) t1 t2 training-set))
         (nt2 (funcall (cadr funcs) t1 t2 training-set)))
    (setq t1 nt1)
    (setq t2 nt2)))
    (list t1 t2)))

最后,我会这样称呼它:

(defvar *training-set* '((15 20) (700 6) (23 15) (19 19) (204 15) (60 150) (87 98) (17 35) (523 29)))
(run-linear-regression 0.0001 1000000 *training-set*)

最佳答案 我不熟悉这里的数学,但由于没有其他人写过更好的答案,这里有一些一般的建议.

您应该更改RUN-LINEAR-REGRESSION以获取变量列表以及学习者函数列表.例如:

(defun run-linear-regression (iterations training-set
                              variables learners)
  (let ((vars variables))
    (dotimes (i iterations)
      (setf vars (mapcar (lambda (function)
                           (funcall function vars training-set))
                         learners)))
    vars))

这需要学习者作为一个参数,而不是让他们在函数中.你的原始代码使学习者进入一个循环,这似乎没有必要,因为MAKE-LEARNERS只将ALPHA作为一个参数,并且不会改变,所以最终的学习者将始终是相同的.

我们还需要更改MAKE-LEARNERS,以便lambda函数将获取变量列表:

(defun make-learners (alpha)
  (list (lambda (variables test-set)
          (destructuring-bind (theta1 theta2) variables
            (- theta1 (funcall (make-learn-fn alpha theta1 theta2
                                              #'make-part-deriv-1)
                               test-set))))
        (lambda (variables test-set)
          (destructuring-bind (theta1 theta2) variables
            (- theta2 (funcall (make-learn-fn alpha theta1 theta2
                                              #'make-part-deriv-2)
                               test-set))))))

这几乎与你的相同,但它使用DESTRUCTURING-BIND从列表VARIABLES中提取THETA1和THETA2.现在我们可以调用RUN-LINEAR-REGRESSION,如:

(run-linear-regression 1000000 *training-set* '(0 0) (make-learners 0.0001))
;=> (42.93504 2.5061023e-4)

要添加更多变量,您可以编写合适的MAKE-LEARNERS版本.由于我不知道数学,我真的不能为此做一个例子.

点赞