-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializers.lisp
37 lines (28 loc) · 1.58 KB
/
initializers.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(in-package #:lispnet)
(defun init-weights(&key shape (mode #'glorot-uniform) (fan-in 0) (fan-out 0) (element-type 'single-float))
(funcall mode :shape shape :fan-in fan-in :fan-out fan-out :element-type element-type))
(defun uniform (&key shape (fan-in 0) (fan-out 0) (element-type 'single-float) )
(let ((array (make-array (shape-dimensions shape) :element-type element-type)))
(loop for index below (array-total-size array) do
(setf (row-major-aref array index)
(random (coerce 1 element-type))))
array))
(defun glorot-uniform (&key shape fan-in fan-out (element-type 'single-float) )
(let ((array (make-array (shape-dimensions shape) :element-type element-type))
(limit (sqrt (/ 6.0 (+ fan-in fan-out)))))
(loop for index below (array-total-size array) do
(setf (row-major-aref array index)
(coerce (- limit (random (* 2 limit))) element-type)))
array))
(defun zeros (&key shape (fan-in 0) (fan-out 0) (element-type 'single-float))
(let ((array (make-array (shape-dimensions shape) :element-type element-type)))
(loop for index below (array-total-size array) do
(setf (row-major-aref array index)
(coerce 0.0 element-type)))
array))
(defun ones (&key shape (fan-in 0) (fan-out 0) (element-type 'single-float))
(let ((array (make-array (shape-dimensions shape) :element-type element-type)))
(loop for index below (array-total-size array) do
(setf (row-major-aref array index)
(coerce 1.0 element-type)))
array))