forked from BrainJS/brain.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
282 lines (242 loc) · 7.6 KB
/
index.d.ts
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* NeuralNetwork section */
export interface INeuralNetworkOptions {
/**
* @default 0.5
*/
binaryThresh?: number;
/**
* array of int for the sizes of the hidden layers in the network
*
* @default [3]
*/
hiddenLayers?: number[];
/**
* supported activation types: ['sigmoid', 'relu', 'leaky-relu', 'tanh'],
*
* @default 'sigmoid'
*/
activation?: NeuralNetworkActivation;
/**
* supported for activation type 'leaky-relu'
*
* @default 0.01
*/
leakyReluAlpha?: number;
}
export type NeuralNetworkActivation = 'sigmoid' | 'relu' | 'leaky-relu' | 'tanh';
export interface INeuralNetworkTrainingOptions {
/**
* the maximum times to iterate the training data --> number greater than 0
* @default 20000
*/
iterations?: number;
/**
* the acceptable error percentage from training data --> number between 0 and 1
* @default 0.005
*/
errorThresh?: number;
/**
* true to use console.log, when a function is supplied it is used --> Either true or a function
* @default false
*/
log?: boolean | INeuralNetworkTrainingCallback;
/**
* iterations between logging out --> number greater than 0
* @default 10
*/
logPeriod?: number;
/**
* scales with delta to effect training rate --> number between 0 and 1
* @default 0.3
*/
learningRate?: number;
/**
* scales with next layer's change value --> number between 0 and 1
* @default 0.1
*/
momentum?: number;
/**
* a periodic call back that can be triggered while training --> null or function
* @default null
*/
callback?: INeuralNetworkTrainingCallback | number;
/**
* the number of iterations through the training data between callback calls --> number greater than 0
* @default 10
*/
callbackPeriod?: number;
/**
* the max number of milliseconds to train for --> number greater than 0
* @default Infinity
*/
timeout?: number;
praxis?: null | 'adam'
}
export interface INeuralNetworkTrainingCallback {
(state: INeuralNetworkState): void;
}
export interface INeuralNetworkState {
iterations: number;
error: number;
}
export interface INeuralNetworkJSON {
sizes: number[];
layers: object[];
outputLookup: any;
inputLookup: any;
activation: NeuralNetworkActivation,
trainOpts: INeuralNetworkTrainingOptions,
leakyReluAlpha?: number,
}
export interface INeuralNetworkTrainingData {
input: NeuralNetworkInput;
output: NeuralNetworkInput;
}
export type NeuralNetworkInput = number[];
export interface INeuralNetworkTestResult {
misclasses: any;
error: number;
total: number;
}
export interface INeuralNetworkBinaryTestResult extends INeuralNetworkTestResult {
trueNeg: number;
truePos: number;
falseNeg: number;
falsePos: number;
precision: number;
recall: number;
accuracy: number;
}
export class NeuralNetwork {
public constructor(options?: INeuralNetworkOptions);
public train(data: INeuralNetworkTrainingData[], options?: INeuralNetworkTrainingOptions): INeuralNetworkState;
public train<T>(data: T, options?: INeuralNetworkTrainingOptions): INeuralNetworkState;
public trainAsync(data: INeuralNetworkTrainingData, options?: INeuralNetworkTrainingOptions): Promise<INeuralNetworkState>;
public trainAsync<T>(data: T, options?: INeuralNetworkTrainingOptions): Promise<INeuralNetworkState>;
public test(data: INeuralNetworkTrainingData): INeuralNetworkTestResult | INeuralNetworkBinaryTestResult;
public run(data: NeuralNetworkInput): NeuralNetworkInput;
public run<T>(data: NeuralNetworkInput): T;
public run<TInput, TOutput>(data: TInput): TOutput;
public fromJSON(json: INeuralNetworkJSON): NeuralNetwork;
public toJSON(): INeuralNetworkJSON;
}
export class NeuralNetworkGPU extends NeuralNetwork {}
/* CrossValidate section */
export interface ICrossValidateJSON {
avgs: ICrossValidationTestPartitionResults;
stats: ICrossValidateStats;
sets: ICrossValidationTestPartitionResults[];
}
export interface ICrossValidateStats {
truePos: number;
trueNeg: number;
falsePos: number;
falseNeg: number;
total: number;
}
export interface ICrossValidationTestPartitionResults {
trainTime: number;
testTime: number;
iterations: number;
trainError: number;
learningRate: number;
hidden: number[];
network: NeuralNetwork;
}
export class CrossValidate {
public constructor(Classifier: typeof NeuralNetwork, options?: INeuralNetworkOptions);
public fromJSON(json: ICrossValidateJSON): NeuralNetwork;
public toJSON(): ICrossValidateJSON;
public train(
data: INeuralNetworkTrainingData[],
trainingOptions: INeuralNetworkTrainingOptions,
k?: number): ICrossValidateStats;
public train<T>(
data: T,
trainingOptions: INeuralNetworkTrainingOptions,
k?: number): ICrossValidateStats;
public testPartition(): ICrossValidationTestPartitionResults;
public toNeuralNetwork(): NeuralNetwork;
public toNeuralNetwork<T>(): T;
}
/* TrainStream section */
export interface ITrainStreamOptions {
neuralNetwork: NeuralNetwork,
neuralNetworkGPU: NeuralNetworkGPU,
floodCallback: () => void,
doneTrainingCallback: (state: INeuralNetworkState) => void
}
export class TrainStream {
public constructor(options: ITrainStreamOptions)
write(data: INeuralNetworkTrainingData): void;
write<T>(data: T): void;
endInputs(): void;
}
/* recurrent section */
export type RNNTrainingValue = string;
export interface IRNNTrainingData {
input: RNNTrainingValue,
output: RNNTrainingValue
}
export interface IRNNDefaultOptions extends INeuralNetworkOptions {
inputSize?: number;
outputSize?: number;
}
/* recurrent time step section */
export type RNNTimeStepInput = number[] | number[][] | object | object[] | object[][];
export type IRNNTimeStepTrainingDatum =
IRNNTimeStepTrainingNumbers
| IRNNTimeStepTrainingNumbers2D
| IRNNTimeStepTrainingObject
| IRNNTimeStepTrainingObjects
| IRNNTimeStepTrainingObject2D
| number[]
| number[][]
| object[]
| object[][];
export interface IRNNTimeStepTrainingNumbers {
input: number[],
output: number[]
}
export interface IRNNTimeStepTrainingNumbers2D {
input: number[][],
output: number[][]
}
export interface IRNNTimeStepTrainingObject {
input: object,
output: object
}
export interface IRNNTimeStepTrainingObjects {
input: object[],
output: object[]
}
export interface IRNNTimeStepTrainingObject2D {
input: object[][],
output: object[][]
}
export declare namespace recurrent {
class RNN extends NeuralNetwork {
constructor(options?: IRNNDefaultOptions)
run(data: RNNTrainingValue): RNNTrainingValue;
run<T>(data: RNNTrainingValue): T;
run<TInput, TOutput>(data: TInput): TOutput;
train(data: IRNNTrainingData[], options: INeuralNetworkTrainingOptions): INeuralNetworkState;
train<T>(data: T, options: INeuralNetworkTrainingOptions): INeuralNetworkState;
}
class LSTM extends recurrent.RNN {}
class GRU extends recurrent.RNN {}
class RNNTimeStep extends recurrent.RNN {
run(input: RNNTimeStepInput): RNNTimeStepInput;
run<T>(input: RNNTimeStepInput): T;
run<TInput, TOutput>(input: TInput): TOutput;
forecast(input: RNNTimeStepInput, count: number): RNNTimeStepInput;
forecast<T>(input: RNNTimeStepInput, count: number): T;
forecast<TInput, TOutput>(input: TInput, count: number): TOutput;
train(data: IRNNTimeStepTrainingDatum[], options: INeuralNetworkTrainingOptions): INeuralNetworkState;
train<T>(data: T, options: INeuralNetworkTrainingOptions): INeuralNetworkState;
}
class LSTMTimeStep extends recurrent.RNNTimeStep {}
class GRUTimeStep extends recurrent.RNNTimeStep {}
}
/* misc helper function section */
export function likely<T>(input: T, net: NeuralNetwork): any;