@@ -12,7 +12,7 @@ import { max } from './utilities/max';
12
12
import { mse } from './utilities/mse' ;
13
13
import { randos } from './utilities/randos' ;
14
14
import { zeros } from './utilities/zeros' ;
15
- import { LossFunction , LossFunctionInputs , LossFunctionState } from './utilities/loss' ;
15
+ import { LossFunction , LossFunctionInputs , MemoryFunction , NeuralNetworkMemory } from './utilities/loss' ;
16
16
type NeuralNetworkFormatter =
17
17
| ( ( v : INumberHash ) => Float32Array )
18
18
| ( ( v : number [ ] ) => Float32Array ) ;
@@ -44,7 +44,7 @@ function loss(
44
44
actual : number ,
45
45
expected : number ,
46
46
inputs : LossFunctionInputs ,
47
- state : LossFunctionState
47
+ state : NeuralNetworkMemory
48
48
) {
49
49
return expected - actual ;
50
50
}
@@ -58,6 +58,7 @@ export type NeuralNetworkActivation =
58
58
export interface IJSONLayer {
59
59
biases : number [ ] ;
60
60
weights : number [ ] [ ] ;
61
+ memory : number [ ] [ ] ;
61
62
}
62
63
63
64
export interface INeuralNetworkJSON {
@@ -77,15 +78,15 @@ export interface INeuralNetworkOptions {
77
78
outputSize : number ;
78
79
binaryThresh : number ;
79
80
hiddenLayers ?: number [ ] ;
80
- lossStateSize : number ;
81
+ memorySize : number ;
81
82
}
82
83
83
84
export function defaults ( ) : INeuralNetworkOptions {
84
85
return {
85
86
inputSize : 0 ,
86
87
outputSize : 0 ,
87
88
binaryThresh : 0.5 ,
88
- lossStateSize : 1
89
+ memorySize : 1
89
90
} ;
90
91
}
91
92
@@ -119,8 +120,8 @@ export interface INeuralNetworkTrainOptions {
119
120
log : boolean | ( ( status : INeuralNetworkState ) => void ) ;
120
121
logPeriod : number ;
121
122
loss ?: LossFunction ;
122
- lossState ?: LossFunctionState ;
123
- lossStateSize : number ;
123
+ memory ?: MemoryFunction ;
124
+ memorySize : number ;
124
125
leakyReluAlpha : number ;
125
126
learningRate : number ;
126
127
momentum : number ;
@@ -141,7 +142,7 @@ export function trainDefaults(): INeuralNetworkTrainOptions {
141
142
log : false , // true to use console.log, when a function is supplied it is used
142
143
logPeriod : 10 , // iterations between logging out
143
144
loss,
144
- lossStateSize : 1 ,
145
+ memorySize : 1 ,
145
146
leakyReluAlpha : 0.01 ,
146
147
learningRate : 0.3 , // multiply's against the input and the delta then adds to momentum
147
148
momentum : 0.1 , // multiply's against the specified "change" then adds to learning rate for change
@@ -192,7 +193,7 @@ export class NeuralNetwork<
192
193
_formatInput : NeuralNetworkFormatter | null = null ;
193
194
_formatOutput : NeuralNetworkFormatter | null = null ;
194
195
195
- _lossState : LossFunctionState ;
196
+ _memory : NeuralNetworkMemory ;
196
197
197
198
runInput : ( input : Float32Array ) => Float32Array = ( input : Float32Array ) => {
198
199
this . setActivation ( ) ;
@@ -208,6 +209,7 @@ export class NeuralNetwork<
208
209
} ;
209
210
210
211
_lossFunction ?: LossFunction ;
212
+ _memoryFunction ?: MemoryFunction ;
211
213
212
214
// adam
213
215
biasChangesLow : Float32Array [ ] = [ ] ;
@@ -227,8 +229,9 @@ export class NeuralNetwork<
227
229
this . sizes = [ inputSize ] . concat ( hiddenLayers ?? [ ] ) . concat ( [ outputSize ] ) ;
228
230
}
229
231
230
- const { lossStateSize } = this . options ?? 0 ;
231
- this . _lossState = this . trainOpts . lossState ?? this . replaceLossState ( lossStateSize ) ;
232
+ // Initialize memory matrix
233
+ const { memorySize } = this . options ?? 0 ;
234
+ this . _memory = this . replaceMemory ( memorySize ) ;
232
235
}
233
236
234
237
/**
@@ -305,8 +308,8 @@ export class NeuralNetwork<
305
308
return this . sizes . length > 0 ;
306
309
}
307
310
308
- public get lossState ( ) : LossFunctionState {
309
- return this . _lossState ;
311
+ public get memory ( ) : NeuralNetworkMemory {
312
+ return this . _memory ;
310
313
}
311
314
312
315
run ( input : Partial < InputType > ) : OutputType {
@@ -772,7 +775,7 @@ export class NeuralNetwork<
772
775
const kernelFunctionThis = { thread : { x : node , y : layer , z : 0 } } ;
773
776
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
774
777
// @ts -ignore
775
- error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . lossState ) ;
778
+ error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . memory ) ;
776
779
}
777
780
else error = target [ node ] - output ;
778
781
} else {
@@ -805,7 +808,7 @@ export class NeuralNetwork<
805
808
const kernelFunctionThis = { thread : { x : node , y : layer , z : 0 } } ;
806
809
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
807
810
// @ts -ignore
808
- error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . lossState ) ;
811
+ error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . memory ) ;
809
812
}
810
813
else error = target [ node ] - output ;
811
814
} else {
@@ -838,7 +841,7 @@ export class NeuralNetwork<
838
841
const kernelFunctionThis = { thread : { x : node , y : layer , z : 0 } } ;
839
842
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
840
843
// @ts -ignore
841
- error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . lossState ) ;
844
+ error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . memory ) ;
842
845
}
843
846
else error = target [ node ] - output ;
844
847
} else {
@@ -870,7 +873,7 @@ export class NeuralNetwork<
870
873
const kernelFunctionThis = { thread : { x : node , y : layer , z : 0 } } ;
871
874
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
872
875
// @ts -ignore
873
- error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . lossState ) ;
876
+ error = this . _lossFunction . call ( kernelFunctionThis , output , target [ node ] , input , this . memory ) ;
874
877
}
875
878
else error = target [ node ] - output ;
876
879
} else {
@@ -1231,12 +1234,18 @@ export class NeuralNetwork<
1231
1234
const jsonLayerBiases = this . biases . map ( ( layerBiases ) =>
1232
1235
Array . from ( layerBiases )
1233
1236
) ;
1237
+ const jsonLayerMemory = this . memory . map ( layerMemory =>
1238
+ layerMemory . map (
1239
+ nodeMemory => Array . from ( nodeMemory )
1240
+ )
1241
+ ) ;
1234
1242
const jsonLayers : IJSONLayer [ ] = [ ] ;
1235
1243
const outputLength = this . sizes . length - 1 ;
1236
1244
for ( let i = 0 ; i <= outputLength ; i ++ ) {
1237
1245
jsonLayers . push ( {
1238
1246
weights : jsonLayerWeights [ i ] ?? [ ] ,
1239
1247
biases : jsonLayerBiases [ i ] ?? [ ] ,
1248
+ memory : jsonLayerMemory [ i ] ?? [ ]
1240
1249
} ) ;
1241
1250
}
1242
1251
return {
@@ -1281,9 +1290,15 @@ export class NeuralNetwork<
1281
1290
const layerBiases = this . biases . map ( ( layerBiases , layerIndex ) =>
1282
1291
Float32Array . from ( jsonLayers [ layerIndex ] . biases )
1283
1292
) ;
1293
+ const layerMemory = this . memory . map ( ( memory , layerIndex ) =>
1294
+ Array . from ( jsonLayers [ layerIndex ] . memory ) . map ( nodeMemory =>
1295
+ Float32Array . from ( nodeMemory )
1296
+ )
1297
+ ) ;
1284
1298
for ( let i = 0 ; i <= this . outputLayer ; i ++ ) {
1285
1299
this . weights [ i ] = layerWeights [ i ] || [ ] ;
1286
1300
this . biases [ i ] = layerBiases [ i ] || [ ] ;
1301
+ this . memory [ i ] = layerMemory [ i ] || [ ] ;
1287
1302
}
1288
1303
return this ;
1289
1304
}
@@ -1387,23 +1402,23 @@ export class NeuralNetwork<
1387
1402
) => OutputType ;
1388
1403
}
1389
1404
1390
- private createLossState (
1391
- lossStateSize : number
1392
- ) : LossFunctionState {
1393
- const lossState : LossFunctionState = [ ] ;
1405
+ private createMemory (
1406
+ memorySize : number
1407
+ ) : NeuralNetworkMemory {
1408
+ const memory : NeuralNetworkMemory = [ ] ;
1394
1409
for ( let layer = 0 ; layer < this . sizes . length ; layer ++ ) {
1395
- lossState [ layer ] = [ ] ;
1410
+ memory [ layer ] = [ ] ;
1396
1411
for ( let neuron = 0 ; neuron < this . sizes . length ; neuron ++ ) {
1397
- lossState [ layer ] [ neuron ] = new Float32Array ( lossStateSize ) ;
1412
+ memory [ layer ] [ neuron ] = new Float32Array ( memorySize ) ;
1398
1413
}
1399
1414
}
1400
- return lossState ;
1415
+ return memory ;
1401
1416
}
1402
1417
1403
- private replaceLossState (
1404
- lossState : number | LossFunctionState
1405
- ) : LossFunctionState {
1406
- if ( typeof lossState === "number" ) return this . _lossState = this . createLossState ( lossState ) ;
1407
- return this . _lossState = lossState ;
1418
+ private replaceMemory (
1419
+ memory : number | NeuralNetworkMemory
1420
+ ) : NeuralNetworkMemory {
1421
+ if ( typeof memory === "number" ) return this . _memory = this . createMemory ( memory ) ;
1422
+ return this . _memory = memory ;
1408
1423
}
1409
1424
}
0 commit comments