Created
November 2, 2018 00:28
-
-
Save no-defun-allowed/95b95469449124570f36bb05192b7ef8 to your computer and use it in GitHub Desktop.
a neural network with petalisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload :petalisp) | |
(use-package :petalisp) | |
(defvar x (as-matrix #2a((5.1 3.5 1.4 0.2) | |
(4.9 3.0 1.4 0.2) | |
(6.2 3.4 5.4 2.3) | |
(5.9 3.0 5.1 1.9)))) | |
(defvar y (as-matrix #(0 0 1 1))) | |
(defvar w (as-matrix #(0.5 0.5 0.5 0.5))) | |
(defun as-matrix (x) | |
(ecase (dimension x) | |
(0 (reshape x (τ () (0 0)))) | |
(1 (reshape x (τ (i) (i 0)))) | |
(2 x))) | |
(defun sigmoid-derivative (n) | |
(* n (- 1.0 n))) | |
(defun sigmoid (n) | |
(/ 1.0 (+ 1.0 (exp (- n))))) | |
(defun transpose (x) | |
(reshape | |
(as-matrix x) | |
(τ (m n) (n m)))) | |
(defun matmul (a b) | |
(β #'+ | |
(α #'* | |
(reshape (as-matrix a) (τ (m n) (n m 0))) | |
(reshape (as-matrix b) (τ (n k) (n 0 k)))))) | |
(defun dot (x y) | |
(reshape | |
(matmul | |
(transpose x) | |
(as-matrix y)) | |
(τ (0 0) ()))) | |
(defun predict (x w) | |
(α #'sigmoid (matmul x w))) | |
(defun train (x y w) | |
(let* ((prediction (predict x w)) | |
(prediction-error (α #'- y prediction)) | |
(prediction-delta (α #'* prediction-error | |
(α #'sigmoid-derivative prediction)))) | |
(compute (α #'+ (matmul (transpose x) prediction-delta) w)))) | |
(defun train* (x y first-w &optional (steps 50)) | |
(loop for w = first-w then (train x y w) | |
for step below steps | |
finally (return w))) | |
#| | |
CL-USER> (compute (predict x (train* x y w 1000))) | |
#2A((0.0048913625) (0.00955967) (0.99563795) (0.9923453)) | |
|# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment