This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.lisp
132 lines (106 loc) · 3.76 KB
/
wrap.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;;; -*- mode:lisp; coding:utf-8 -*-
;;; This file is part of the BRAIN package
;;; Lisp wrap for brain.js library
;;; Copyright © 2017 Vladimir Mezentsev
(in-package :brain)
;;; make neural net
(export '(make-neural-net))
(defun make-neural-net (&optional nno)
(if nno
(make-new #j:brain:NeuralNetwork nno)
(make-new #j:brain:NeuralNetwork)))
;;; RNN
(export '(make-rnn))
(defun make-rnn (&optional nno)
(if nno (make-new #j:brain:recurrent:RNN nno)
(make-new #j:brain:recurrent:RNN)))
;;; GRU
(export '(make-gru))
(defun make-gru (&optional nno)
(if nno (make-new #j:brain:recurrent:GRU nno)
(make-new #j:brain:recurrent:GRU)))
;;; LSTM
(export '(make-lstm))
(defun make-lstm (&optional nno)
(if nno (make-new #j:brain:recurrent:LSTM nno)
(make-new #j:brain:recurrent:LSTM)))
;;; neural-net options - train & run
;;;
;;; (setq *nn (brain:make-neural-net
;;; (brain:nno :hiddensizes #(6 3)
;;; :callback (lambda (stats) (print (klib:jso-to-list stats)))
;;; :callbackperiod 100)))
;;; (brain:train *nn
;;; (train-data
;;; (tp (mkjso "r" 0.3 "g" 0.7) (mkjso "black" 1))
;;; (tp (mkjso "r" 0.16 "b" 0.2) (mkjso "white" 1))
;;; (tp (mkjso "r" 0.5 "g" 0.5 "b" 1.0) (mkjso "white" 1)))
;;; (brain:nno :iterations 200 :errorthres 0.02))
;;;
;;; (print (klib:jso-to-list (brain:run *nn (mkjso "r" 0 "g" 0.00 "b" 0.0))))
;;;
;;;
(export '(nno))
(defun nno (&key
activation
errorThresh
iterations
momentum
learningRate
callback callbackPeriod
log logPeriod
hiddenSizes
inputSize inputRange outputSize)
(let ((options (new)))
(flet ((mko (key val) (setf (oget options key) val)))
(if activation (mko "activation" activation))
(if errorThresh (mko "errorThresh" errorThresh))
(if iterations (mko "iterations" iterations))
(if momentum (mko "momentum" momentum))
(if learningrate (mko "learningRate" learningrate))
(if callback (mko "callback" callback))
(if callbackperiod (mko "callbackPeriod" callbackperiod))
(if log (mko "log" log))
(if logperiod (mko "logPeriod" logperiod))
(if hiddensizes (mko "hiddenSizes" hiddensizes))
(if inputsize (mko "inputSize" inputsize))
(if inputRange (mko "inputRange" inputRange))
(if outputSize (mko "outputSize" outputSize))
options)))
;;; train
(export '(train))
(defun train (bro &optional input nno)
(if nno
(funcall ((oget bro "train" "bind") bro input nno))
(if input
(funcall ((oget bro "train" "bind") bro input))
(funcall ((oget bro "train" "bind") bro)))))
;;; run
(export '(run))
(defun run (bro input &optional nno)
(if nno
(funcall ((oget bro "run" "bind") bro input nno))
(funcall ((oget bro "run" "bind") bro input ))))
;;; train data set utils
;;;
;;; (train-data
;;; (tp (mkjso "r" 0.3 "g" 0.7) (mkjso "black" 1))
;;; (tp (mkjso "r" 0.16 "b" 0.2) (mkjso "white" 1))
;;; (tp (mkjso "r" 0.5 "g" 0.5 "b" 1.0) (mkjso "white" 1)))
;;; train pattern
(export '(tp))
(defun tp (input output)
(mkjso "input" input "output" output))
;;; train-data
(export '(train-data))
(defun train-data (&rest patterns)
(apply #'vector patterns))
#|
(defun ncb (stats)
(format *so* "~a~%" (klib:jso-to-list stats)))
(setq *hidden (nno :hiddensizes #(4 5)))
(setq *topt (nno :callback #ncb :callbackPeriod 1000 :errorThresh 0.002 :iterations 100000))
(setq *topt (nno :callback #ncb))
|#
(in-package :cl-user)
;;; EOF