(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,
x)
mi))
+(define (normalize x)
+ (v:+ -0.5 (v:/ x 255)))
+
;;;; parameters
(define bs 2) ; batch size
;;;; 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)))
"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)
(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))
(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."
((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))
((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
#: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)
(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)))
(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))))
(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))
)))))
(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))
(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)))))))