]> git.vouivredigital.com Git - vouivre.git/commitdiff
Remove unused array types
authoradmin <admin@vouivredigital.com>
Mon, 27 Nov 2023 15:16:09 +0000 (00:16 +0900)
committeradmin <admin@vouivredigital.com>
Mon, 27 Nov 2023 15:16:09 +0000 (00:16 +0900)
examples/example.scm
tests/autodiff.scm
vouivre/autodiff.scm

index bf694c4c126dfcaef00b90ab9130ab425e31f4a7..b817ca6dd64f947453a0bb2a92ed8dc260baef2f 100644 (file)
       (iter f (f a) (1- n))))
 
 (define (make-randn-array . dims)
-  (apply v:produce-typed-array
+  "Return an array of the given dimensions with normally distributed elements."
+  (apply v:produce-array
          (lambda _ (random:normal))
-        v:*atype*
          dims))
 
-(define (relu x)
-  (v:max 0 x))
-
-(define (logsumexp x)
-  (let ((c (v:maximum x)))
-    (v:+ c (v:log (v:sum (v:exp (v:- x c)))))))
-
 (define (random-batch batch-size array . more)
   "Return random batches of the given size, one for each array, as multiple
 values. The indices of elements of a batch, in their array of origin,
@@ -86,6 +79,9 @@ etc."
      x)
     mi))
 
+(define (normalize x)
+  (v:+ -0.5 (v:/ x 255)))
+
 ;;;; parameters
 
 (define bs 2)                          ; batch size
@@ -98,25 +94,14 @@ etc."
 ;;;; data
 
 (define data (v:load-mnist 10 #t))
-(define (cast-array x)
-  (apply
-   v:produce-typed-array
-   (lambda indices
-     (apply array-ref x indices))
-   v:*atype*
-   (array-dimensions x)))
-(define (normalize x)
-  (v:- ((v:extend /) x 255)
-       0.5))
 
-(set! h (list-ref (array-dimensions (car data))
-                 1))
-(set! w (list-ref (array-dimensions (car data))
-                 2))
+(let ((dims (array-dimensions (car data))))
+  (set! h (second dims))
+  (set! w (third dims)))
 
-(define x (normalize (cast-array (car data)))) ; training samples
-(define y (cdr data))                         ; training labels
-(define a (list (make-randn-array hl h w)      ; the model parameters
+(define x (normalize (car data)))        ; training samples
+(define y (cdr data))                    ; training labels
+(define a (list (make-randn-array hl h w) ; the model parameters
                (make-randn-array hl hl)
                 (make-randn-array es hl)))
 
@@ -126,13 +111,13 @@ etc."
   "The model which, given a sample and some parameters, produces a distribution.
 The log-likelihood of each label, here."
   (fold (lambda (a prev)
-         (v:adot a (relu prev) 1))
+         (v:adot a (v:relu prev) 1))
        (v:adot (car a) x 2)
        (cdr a)))
 
 (define (L y p)
   "The loss as a function of the ground truth label and the model's output."
-  (v:- (logsumexp p)
+  (v:- (v:logsumexp p)
        (v:array-ref p y)))
 
 (define (<L> x y)
@@ -166,9 +151,12 @@ gradient (without overwriting the original parameters)."
   (newline))
 
 (define (categorize x)
+  "The category of the given sample infered by the model."
   (argmax (f x a)))
 
-(define (run nb-epochs steps-per-epochs)
+(define (train nb-epochs steps-per-epochs)
+  "Train the model for a total of `(* nb-epochs steps-per-epochs)' steps,
+reporting the loss once before training, and then every epoch."
   (report-loss a)
   (do ((i 0 (1+ i)))
       ((= i nb-epochs))
index ef638ff80c95d4ae3700dbc0538a952058a9d33f..87ec79e8e1a19617d65722f7376591b81f9ae225 100644 (file)
@@ -87,9 +87,9 @@ being the number of arguments to each procedure."
       (random-array-shape)))
 
 (define* (random-array #:optional shape)
-  (apply produce-typed-array
+  (apply produce-array
         (lambda _ (random:uniform))
-        v:*atype* (or shape (random-array-shape))))
+        (or shape (random-array-shape))))
 
 (define (random-non-empty-array)
   "Random array of at least one element."
@@ -207,12 +207,11 @@ and, when it's an array, at the given index."
        ((and (number? fxs)
             (array? x))
        (apply
-        produce-typed-array
+        produce-array
         (lambda indices
           (/ (- (apply f (apply axis-add xs step indices))
                 (apply f (apply axis-add xs (- step) indices)))
              (* 2 step)))
-        v:*atype*
         (array-dimensions x)))
        ((and (array? fxs)
             (number? x))
@@ -224,7 +223,7 @@ and, when it's an array, at the given index."
        ((and (array? fxs)
             (array? x))
        (let ((a (apply
-                 make-typed-array v:*atype* *unspecified*
+                 make-array *unspecified*
                  (append (array-dimensions fxs)
                          (array-dimensions x)))))
          (for-indices-in-range
index d243107e98fedfb81d7c32a2690bf34853981187..4720c9fa39502de30f632eade127a19ffb2155ef 100644 (file)
@@ -22,8 +22,7 @@
   #:use-module (vouivre misc)
   #:use-module (vouivre promises)
   #:export
-  (*atype*
-   adot
+  (adot
    amap2
    contract-arrays
    differentiable-wrapper
    ewise2
    extend
    fdiff
-   rdiff
+   logsumexp
    make-batch
    make-internal
    maximum
    mean
    rank-of
+   rdiff
+   relu
    sum)
   #:replace
   ((i:sqrt . sqrt)
@@ -92,8 +93,8 @@
         (n-free-dims-b (apply * free-dims-b))
         (n-bound-dims (apply * bound-dims))
         (s 0)
-        (r (apply make-typed-array *atype* *unspecified* (append free-dims-a
-                                                                 free-dims-b)))
+        (r (apply make-array *unspecified*
+                  (append free-dims-a free-dims-b)))
         (ac (array-contents a))
         (bc (array-contents b))
         (rc (array-contents r)))
@@ -128,10 +129,9 @@ element-wise. All arrays must have the same dimension."
   (lambda xs
     (if-let (x (find array? xs))
            (apply
-            produce-typed-array
+            produce-array
             (lambda is
               (apply-elemwise f is xs))
-            *atype*
             (array-dimensions x))
            (apply f xs))))
 
@@ -160,8 +160,6 @@ element-wise. All arrays must have the same dimension."
   (forward internal-forward)
   (jacobian internal-jacobian))
 
-;;(define *atype* 'f32)
-(define *atype* #t)
 (define *differentiation-mode* (make-parameter #f))
 (define *n-y-dims* (make-parameter #f))
 (define *j* (make-parameter #f))
@@ -791,8 +789,7 @@ adding the result to the destination buffer."
                )))))
      (list
       (lambda (elem . more)
-       (let ((a (apply make-typed-array *atype* *unspecified* batch-size
-                       (dims-of elem))))
+       (let ((a (apply make-array *unspecified* batch-size (dims-of elem))))
          (for-each
           (lambda (x b)
             (array-cell-set! a x b))
@@ -874,3 +871,10 @@ adding the result to the destination buffer."
                        (lambda (b)
                          (f (i:array-cell-ref x b)
                             (i:array-cell-ref y b))))))
+
+(define (relu x)
+  (i:max 0 x))
+
+(define (logsumexp x)
+  (let ((c (maximum x)))
+    (i:+ c (i:log (sum (i:exp (i:- x c)))))))