1
- import { KernelOutput , Texture , TextureArrayOutput } from "gpu.js" ;
2
- import { IJSONLayer , INeuralNetworkData , INeuralNetworkDatum , INeuralNetworkTrainOptions } from "./neural-network" ;
3
- import { INeuralNetworkGPUOptions , NeuralNetworkGPU } from "./neural-network-gpu" ;
4
- import { INeuralNetworkState } from "./neural-network-types" ;
5
- import { UntrainedNeuralNetworkError } from "./errors/untrained-neural-network-error" ;
1
+ import { KernelOutput , Texture , TextureArrayOutput } from 'gpu.js' ;
2
+ import {
3
+ IJSONLayer ,
4
+ INeuralNetworkData ,
5
+ INeuralNetworkDatum ,
6
+ INeuralNetworkTrainOptions ,
7
+ } from './neural-network' ;
8
+ import {
9
+ INeuralNetworkGPUOptions ,
10
+ NeuralNetworkGPU ,
11
+ } from './neural-network-gpu' ;
12
+ import { INeuralNetworkState } from './neural-network-types' ;
13
+ import { UntrainedNeuralNetworkError } from './errors/untrained-neural-network-error' ;
6
14
7
15
export interface IAEOptions {
8
16
binaryThresh : number ;
@@ -13,13 +21,14 @@ export interface IAEOptions {
13
21
/**
14
22
* An autoencoder learns to compress input data down to relevant features and reconstruct input data from its compressed representation.
15
23
*/
16
- export class AE < DecodedData extends INeuralNetworkData , EncodedData extends INeuralNetworkData > {
24
+ export class AE <
25
+ DecodedData extends INeuralNetworkData ,
26
+ EncodedData extends INeuralNetworkData
27
+ > {
17
28
private decoder ?: NeuralNetworkGPU < EncodedData , DecodedData > ;
18
- private denoiser : NeuralNetworkGPU < DecodedData , DecodedData > ;
29
+ private readonly denoiser : NeuralNetworkGPU < DecodedData , DecodedData > ;
19
30
20
- constructor (
21
- options ?: Partial < IAEOptions >
22
- ) {
31
+ constructor ( options ?: Partial < IAEOptions > ) {
23
32
// Create default options for the autoencoder.
24
33
options ??= { } ;
25
34
@@ -32,7 +41,9 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
32
41
denoiserOptions . hiddenLayers = options . hiddenLayers ;
33
42
34
43
// Define the denoiser subnet's input and output sizes.
35
- if ( options . decodedSize ) denoiserOptions . inputSize = denoiserOptions . outputSize = options . decodedSize ;
44
+ if ( options . decodedSize )
45
+ denoiserOptions . inputSize = denoiserOptions . outputSize =
46
+ options . decodedSize ;
36
47
37
48
// Create the denoiser subnet of the autoencoder.
38
49
this . denoiser = new NeuralNetworkGPU < DecodedData , DecodedData > ( options ) ;
@@ -82,7 +93,8 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
82
93
this . denoiser . run ( input ) ;
83
94
84
95
// Get the auto-encoded input.
85
- let encodedInput : TextureArrayOutput = this . encodedLayer as TextureArrayOutput ;
96
+ let encodedInput : TextureArrayOutput = this
97
+ . encodedLayer as TextureArrayOutput ;
86
98
87
99
// If the encoded input is a `Texture`, convert it into an `Array`.
88
100
if ( encodedInput instanceof Texture ) encodedInput = encodedInput . toArray ( ) ;
@@ -100,7 +112,7 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
100
112
* @param {DecodedData } input
101
113
* @returns {boolean }
102
114
*/
103
- likelyIncludesAnomalies ( input : DecodedData , anomalyThreshold : number = 0.2 ) : boolean {
115
+ likelyIncludesAnomalies ( input : DecodedData , anomalyThreshold = 0.2 ) : boolean {
104
116
// Create the anomaly vector.
105
117
const anomalies : number [ ] = [ ] ;
106
118
@@ -109,7 +121,9 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
109
121
110
122
// Calculate the anomaly vector.
111
123
for ( let i = 0 ; i < ( input . length ?? 0 ) ; i ++ ) {
112
- anomalies [ i ] = Math . abs ( ( input as number [ ] ) [ i ] - ( denoised as number [ ] ) [ i ] ) ;
124
+ anomalies [ i ] = Math . abs (
125
+ ( input as number [ ] ) [ i ] - ( denoised as number [ ] ) [ i ]
126
+ ) ;
113
127
}
114
128
115
129
// Calculate the sum of all anomalies within the vector.
@@ -131,11 +145,17 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
131
145
* @param {Partial<INeuralNetworkTrainOptions> } options
132
146
* @returns {INeuralNetworkState }
133
147
*/
134
- train ( data : DecodedData [ ] , options ?: Partial < INeuralNetworkTrainOptions > ) : INeuralNetworkState {
135
- const preprocessedData : INeuralNetworkDatum < Partial < DecodedData > , Partial < DecodedData > > [ ] = [ ] ;
136
-
137
- for ( let datum of data ) {
138
- preprocessedData . push ( { input : datum , output : datum } ) ;
148
+ train (
149
+ data : DecodedData [ ] ,
150
+ options ?: Partial < INeuralNetworkTrainOptions >
151
+ ) : INeuralNetworkState {
152
+ const preprocessedData : Array < INeuralNetworkDatum <
153
+ Partial < DecodedData > ,
154
+ Partial < DecodedData >
155
+ > > = [ ] ;
156
+
157
+ for ( const datum of data ) {
158
+ preprocessedData . push ( { input : datum , output : datum } ) ;
139
159
}
140
160
141
161
const results = this . denoiser . train ( preprocessedData , options ) ;
@@ -168,7 +188,7 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
168
188
169
189
const decoder = new NeuralNetworkGPU ( ) . fromJSON ( json ) ;
170
190
171
- return decoder as unknown as NeuralNetworkGPU < EncodedData , DecodedData > ;
191
+ return ( decoder as unknown ) as NeuralNetworkGPU < EncodedData , DecodedData > ;
172
192
}
173
193
174
194
/**
0 commit comments