Advertisement
bsddeamon

feedforward neural network

Jun 9th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.26 KB | None | 0 0
  1. (define-library (neural-network matrix-algebra)
  2. (import (scheme base) (srfi 1)) (export transpose hadamard sum dot-product)
  3. (begin (define map map-in-order) (define (transpose x) (apply zip x))
  4.   (define (hadamard x y) (map (lambda (x y) (map * x y)) x y))
  5.   (define (sum xy) (map (lambda (x) (apply + x)) xy))
  6.   (define (dot-product x xy) (sum (hadamard (make-list 3 x) (transpose xy))))))
  7.  
  8. (define-library (neural-network random)
  9. (import (scheme base) (srfi 1) (srfi 27)) (export rand weights)
  10. (begin (define source default-random-source) (random-source-randomize! source)
  11.   (define rand (random-source-make-reals source))
  12.   (define weights
  13.     (list-tabulate 3 (lambda (i) (list-tabulate 3 (lambda (i) (list-tabulate 3 (lambda (i) (/ (rand) 2))))))))))
  14.  
  15. (define-library (neural-network feedforwarding)
  16. (import (scheme base) (scheme inexact) (srfi 1) (neural-network matrix-algebra) (neural-network random))
  17. (export sigmoid feedforward)
  18. (begin (define (sigmoid x) (/ 1 (+ 1.0 (exp (- 0 x)))))
  19.   (define (feedforward input ws)
  20.     (let f ([in input] [ws ws] [ya (list)] [ys (list)])
  21.       (if (null? ws) (list in (reverse ya) (reverse ys))
  22.           (let* ([y (dot-product in (car ws))] [in (map sigmoid y)])
  23.             (f in (cdr ws) (cons in ya) (cons y ys))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement