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' ;
614
715export interface IAEOptions {
816 binaryThresh : number ;
@@ -13,13 +21,14 @@ export interface IAEOptions {
1321/**
1422 * An autoencoder learns to compress input data down to relevant features and reconstruct input data from its compressed representation.
1523 */
16- export class AE < DecodedData extends INeuralNetworkData , EncodedData extends INeuralNetworkData > {
24+ export class AE <
25+ DecodedData extends INeuralNetworkData ,
26+ EncodedData extends INeuralNetworkData
27+ > {
1728 private decoder ?: NeuralNetworkGPU < EncodedData , DecodedData > ;
18- private denoiser : NeuralNetworkGPU < DecodedData , DecodedData > ;
29+ private readonly denoiser : NeuralNetworkGPU < DecodedData , DecodedData > ;
1930
20- constructor (
21- options ?: Partial < IAEOptions >
22- ) {
31+ constructor ( options ?: Partial < IAEOptions > ) {
2332 // Create default options for the autoencoder.
2433 options ??= { } ;
2534
@@ -32,7 +41,9 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
3241 denoiserOptions . hiddenLayers = options . hiddenLayers ;
3342
3443 // 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 ;
3647
3748 // Create the denoiser subnet of the autoencoder.
3849 this . denoiser = new NeuralNetworkGPU < DecodedData , DecodedData > ( options ) ;
@@ -82,7 +93,8 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
8293 this . denoiser . run ( input ) ;
8394
8495 // Get the auto-encoded input.
85- let encodedInput : TextureArrayOutput = this . encodedLayer as TextureArrayOutput ;
96+ let encodedInput : TextureArrayOutput = this
97+ . encodedLayer as TextureArrayOutput ;
8698
8799 // If the encoded input is a `Texture`, convert it into an `Array`.
88100 if ( encodedInput instanceof Texture ) encodedInput = encodedInput . toArray ( ) ;
@@ -100,7 +112,7 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
100112 * @param {DecodedData } input
101113 * @returns {boolean }
102114 */
103- likelyIncludesAnomalies ( input : DecodedData , anomalyThreshold : number = 0.2 ) : boolean {
115+ likelyIncludesAnomalies ( input : DecodedData , anomalyThreshold = 0.2 ) : boolean {
104116 // Create the anomaly vector.
105117 const anomalies : number [ ] = [ ] ;
106118
@@ -109,7 +121,9 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
109121
110122 // Calculate the anomaly vector.
111123 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+ ) ;
113127 }
114128
115129 // Calculate the sum of all anomalies within the vector.
@@ -131,11 +145,17 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
131145 * @param {Partial<INeuralNetworkTrainOptions> } options
132146 * @returns {INeuralNetworkState }
133147 */
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 } ) ;
139159 }
140160
141161 const results = this . denoiser . train ( preprocessedData , options ) ;
@@ -168,7 +188,7 @@ export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeu
168188
169189 const decoder = new NeuralNetworkGPU ( ) . fromJSON ( json ) ;
170190
171- return decoder as unknown as NeuralNetworkGPU < EncodedData , DecodedData > ;
191+ return ( decoder as unknown ) as NeuralNetworkGPU < EncodedData , DecodedData > ;
172192 }
173193
174194 /**
0 commit comments