-
Notifications
You must be signed in to change notification settings - Fork 112
/
index.js
246 lines (203 loc) · 5.5 KB
/
index.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const sigmoidPrime = require('sigmoid-prime')
const Emitter = require('emitter-component')
const htanPrime = require('htan-prime')
const Matrix = require('node-matrix')
const sigmoid = require('sigmoid')
const sample = require('samples')
const htan = require('htan')
class Mind extends Emitter {
/**
* Initialize a new `Mind`.
*
* @param {Object} options
* @return {Object} this
* @api public
*/
constructor (options) {
super()
options = options || {}
if (options.activator === 'sigmoid') {
this.activate = sigmoid
this.activatePrime = sigmoidPrime
} else {
this.activate = htan
this.activatePrime = htanPrime
}
// hyperparameters
this.learningRate = options.learningRate || 0.7
this.hiddenLayers = options.hiddenLayers || 1
this.hiddenUnits = options.hiddenUnits || 3
this.iterations = options.iterations || 10000
}
/**
* Learn.
*
* 1. Normalize examples
* 2. Setup weights
* 3. Forward propagate to generate a prediction
* 4. Back propagate to adjust weights
* 5. Repeat (3) and (4) `this.iterations` times
*
* These five steps enable our network to learn the relationship
* between inputs and outputs.
*
* @param {Array} examples
* @return {Object} this
* @api public
*/
learn (examples) {
examples = normalize(examples)
if (!this.weights) {
this.setup(examples)
}
for (let i = 0; i < this.iterations; i++) {
const results = this.forward(examples)
const errors = this.back(examples, results)
this.emit('data', i, errors, results)
}
return this
}
/**
* Setup the weights.
*
* @param {Object} examples
* @api private
*/
setup (examples) {
this.weights = []
// input > hidden
this.weights.push(
Matrix({
rows: examples.input[0].length,
columns: this.hiddenUnits,
values: sample
})
)
// hidden > hidden
for (let i = 1; i < this.hiddenLayers; i++) {
this.weights.push(
Matrix({
rows: this.hiddenUnits,
columns: this.hiddenUnits,
values: sample
})
)
}
// hidden > output
this.weights.push(
Matrix({
rows: this.hiddenUnits,
columns: examples.output[0].length,
values: sample
})
)
}
/**
* Forward propagate.
*
* @param {Object} examples
* @return {Array} results
* @api private
*/
forward (examples) {
const results = []
// input > hidden
results.push(this.sum(this.weights[0], examples.input))
// hidden > hidden
for (let i = 1; i < this.hiddenLayers; i++) {
results.push(this.sum(this.weights[i], results[i - 1].result))
}
// hidden > output
results.push(this.sum(this.weights[this.weights.length - 1], results[results.length - 1].result))
return results
}
/**
* Sum `weight` and `input`.
*
* @param {Matrix} weight
* @param {Array} input
* @return {Object}
* @api private
*/
sum (weight, input) {
const res = {}
res.sum = Matrix.multiply(weight, input)
res.result = res.sum.transform(this.activate)
return res
}
/**
* Back propagate.
*
* @param {Object} outputMatrix
* @api private
*/
back (examples, results) {
const activatePrime = this.activatePrime
const hiddenLayers = this.hiddenLayers
const learningRate = this.learningRate
const weights = this.weights
// output > hidden
const error = Matrix.subtract(examples.output, results[results.length - 1].result)
let delta = Matrix.multiplyElements(results[results.length - 1].sum.transform(activatePrime), error)
let changes = Matrix.multiplyScalar(Matrix.multiply(delta, results[hiddenLayers - 1].result.transpose()), learningRate)
weights[weights.length - 1] = Matrix.add(weights[weights.length - 1], changes)
// hidden > hidden
for (let i = 1; i < hiddenLayers; i++) {
delta = Matrix.multiplyElements(Matrix.multiply(weights[weights.length - i].transpose(), delta), results[results.length - (i + 1)].sum.transform(activatePrime))
changes = Matrix.multiplyScalar(Matrix.multiply(delta, results[results.length - (i + 1)].result.transpose()), learningRate)
weights[weights.length - (i + 1)] = Matrix.add(weights[weights.length - (i + 1)], changes)
}
// hidden > input
delta = Matrix.multiplyElements(Matrix.multiply(weights[1].transpose(), delta), results[0].sum.transform(activatePrime))
changes = Matrix.multiplyScalar(Matrix.multiply(delta, examples.input.transpose()), learningRate)
weights[0] = Matrix.add(weights[0], changes)
return error
}
/**
* Predict.
*
* @param {Array} input
* @api public
*/
predict (input) {
const results = this.forward({ input: Matrix([input]) })
return results[results.length - 1].result[0]
}
/**
* Upload weights.
*
* @param {Object} weights
* @return {Object} this
* @api public
*/
upload (weights) {
this.weights = weights
return this
}
/**
* Download weights.
*
* @return {Object} weights
* @api public
*/
download () {
return this.weights
}
}
module.exports = Mind
/**
* Normalize the data.
*
* @param {Array} data
* @return {Object} ret
*/
function normalize (data) {
const ret = { input: [], output: [] }
for (let i = 0; i < data.length; i++) {
ret.output.push(data[i].output)
ret.input.push(data[i].input)
}
ret.output = Matrix(ret.output)
ret.input = Matrix(ret.input)
return ret
}