forked from BrainJS/brain.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnn-time-step.js
169 lines (150 loc) · 4.65 KB
/
rnn-time-step.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
import Matrix from './matrix';
import RandomMatrix from './matrix/random-matrix';
import Equation from './matrix/equation';
import RNN from './rnn';
export default class RNNTimeStep extends RNN {
constructor(options) {
super(options);
}
createInputMatrix() {
this.model.input = new RandomMatrix(this.inputSize, 1, 0.08);
}
createOutputMatrix() {
let model = this.model;
let outputSize = this.outputSize;
let lastHiddenSize = this.hiddenSizes[this.hiddenSizes.length - 1];
//whd
model.outputConnector = new RandomMatrix(outputSize, lastHiddenSize, 0.08);
//bd
model.output = new Matrix(outputSize, 1);
}
bindEquation() {
let model = this.model;
let hiddenSizes = this.hiddenSizes;
let hiddenLayers = model.hiddenLayers;
let equation = new Equation();
let outputs = [];
let equationConnection = model.equationConnections.length > 0
? model.equationConnections[model.equationConnections.length - 1]
: this.initialLayerInputs
;
// 0 index
let output = this.getEquation(equation, equation.input(model.input), equationConnection[0], hiddenLayers[0]);
outputs.push(output);
// 1+ indices
for (let i = 1, max = hiddenSizes.length; i < max; i++) {
output = this.getEquation(equation, output, equationConnection[i], hiddenLayers[i]);
outputs.push(output);
}
model.equationConnections.push(outputs);
equation.add(equation.multiply(model.outputConnector, output), model.output);
model.equations.push(equation);
}
/**
*
* @param {Number[]} input
* @returns {number}
*/
runInput(input) {
this.runs++;
let model = this.model;
let errorSum = 0;
let equation;
while (model.equations.length < input.length - 1) {
this.bindEquation();
}
const outputs = [];
if (this.inputSize === 1) {
for (let inputIndex = 0, max = input.length - 1; inputIndex < max; inputIndex++) {
// start and end tokens are zeros
equation = model.equations[inputIndex];
const current = input[inputIndex];
const next = input[inputIndex + 1];
const output = equation.runInput([current]);
for (let i = 0; i < output.weights.length; i++) {
const error = output.weights[i] - next;
// set gradients into log probabilities
errorSum += Math.abs(error);
// write gradients into log probabilities
output.deltas[i] = error;
outputs.push(output.weights);
}
}
} else {
for (let inputIndex = 0, max = input.length - 1; inputIndex < max; inputIndex++) {
// start and end tokens are zeros
equation = model.equations[inputIndex];
const current = input[inputIndex];
const next = input[inputIndex + 1];
const output = equation.runInput(current);
for (let i = 0; i < output.weights.length; i++) {
const error = output.weights[i] - next[i];
// set gradients into log probabilities
errorSum += Math.abs(error);
// write gradients into log probabilities
output.deltas[i] = error;
outputs.push(output.weights);
}
}
}
//this.model.equations.length - 1;
this.totalCost = errorSum;
return errorSum;
}
runBackpropagate() {
for (let i = this.model.equations.length - 1; i > -1; i--) {
this.model.equations[i].runBackpropagate();
}
}
/**
*
* @param {Number[]|Number} [input]
* @param {Number} [maxPredictionLength]
* @param {Boolean} [isSampleI]
* @param {Number} temperature
* @returns {Number[]|Number}
*/
run(input = [], maxPredictionLength = 1, isSampleI = false, temperature = 1) {
if (!this.isRunnable) return null;
const model = this.model;
while (model.equations.length < maxPredictionLength) {
this.bindEquation();
}
let lastOutput;
if (this.inputSize === 1) {
for (let i = 0; i < input.length; i++) {
let outputMatrix = model.equations[i].runInput([input[i]]);
lastOutput = outputMatrix.weights;
}
} else {
for (let i = 0; i < input.length; i++) {
let outputMatrix = model.equations[i].runInput(input[i]);
lastOutput = outputMatrix.weights;
}
}
if (this.outputSize === 1) {
return lastOutput[0]
}
return lastOutput;
}
/**
*
* @returns {Function}
*/
toFunction() {
throw new Error('not implemented');
}
}
RNNTimeStep.defaults = {
inputSize: 1,
hiddenSizes:[20],
outputSize: 1,
learningRate: 0.01,
decayRate: 0.999,
smoothEps: 1e-8,
regc: 0.000001,
clipval: 5,
json: null,
dataFormatter: null
};
RNNTimeStep.trainDefaults = RNN.trainDefaults;