From 04cf2bccfb7bf039936360e68401cc6b33b00d6a Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 28 Nov 2023 00:16:09 +0900 Subject: [PATCH] Remove unused array types --- examples/example.scm | 46 ++++++++++++++++---------------------------- tests/autodiff.scm | 9 ++++----- vouivre/autodiff.scm | 26 ++++++++++++++----------- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/examples/example.scm b/examples/example.scm index bf694c4..b817ca6 100644 --- a/examples/example.scm +++ b/examples/example.scm @@ -30,18 +30,11 @@ (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 ( 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)) diff --git a/tests/autodiff.scm b/tests/autodiff.scm index ef638ff..87ec79e 100644 --- a/tests/autodiff.scm +++ b/tests/autodiff.scm @@ -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 diff --git a/vouivre/autodiff.scm b/vouivre/autodiff.scm index d243107..4720c9f 100644 --- a/vouivre/autodiff.scm +++ b/vouivre/autodiff.scm @@ -22,8 +22,7 @@ #:use-module (vouivre misc) #:use-module (vouivre promises) #:export - (*atype* - adot + (adot amap2 contract-arrays differentiable-wrapper @@ -33,12 +32,14 @@ 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))))))) -- 2.39.2