Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define-library (neural-network matrix-algebra)
- (import (scheme base) (srfi 1)) (export transpose hadamard sum dot-product)
- (begin (define map map-in-order) (define (transpose x) (apply zip x))
- (define (hadamard x y) (map (lambda (x y) (map * x y)) x y))
- (define (sum xy) (map (lambda (x) (apply + x)) xy))
- (define (dot-product x xy) (sum (hadamard (make-list 3 x) (transpose xy))))))
- (define-library (neural-network random)
- (import (scheme base) (srfi 1) (srfi 27)) (export rand weights)
- (begin (define source default-random-source) (random-source-randomize! source)
- (define rand (random-source-make-reals source))
- (define weights
- (list-tabulate 3 (lambda (i) (list-tabulate 3 (lambda (i) (list-tabulate 3 (lambda (i) (/ (rand) 2))))))))))
- (define-library (neural-network feedforwarding)
- (import (scheme base) (scheme inexact) (srfi 1) (neural-network matrix-algebra) (neural-network random))
- (export sigmoid feedforward)
- (begin (define (sigmoid x) (/ 1 (+ 1.0 (exp (- 0 x)))))
- (define (feedforward input ws)
- (let f ([in input] [ws ws] [ya (list)] [ys (list)])
- (if (null? ws) (list in (reverse ya) (reverse ys))
- (let* ([y (dot-product in (car ws))] [in (map sigmoid y)])
- (f in (cdr ws) (cons in ya) (cons y ys))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement