|
| 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"; |
| 6 | + |
| 7 | +export interface IAEOptions { |
| 8 | + binaryThresh: number; |
| 9 | + decodedSize: number; |
| 10 | + hiddenLayers: number[]; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * An autoencoder learns to compress input data down to relevant features and reconstruct input data from its compressed representation. |
| 15 | + */ |
| 16 | +export class AE<DecodedData extends INeuralNetworkData, EncodedData extends INeuralNetworkData> { |
| 17 | + private decoder?: NeuralNetworkGPU<EncodedData, DecodedData>; |
| 18 | + private denoiser: NeuralNetworkGPU<DecodedData, DecodedData>; |
| 19 | + |
| 20 | + constructor ( |
| 21 | + options?: Partial<IAEOptions> |
| 22 | + ) { |
| 23 | + // Create default options for the autoencoder. |
| 24 | + options ??= {}; |
| 25 | + |
| 26 | + // Create default options for the autoencoder's denoiser subnet. |
| 27 | + const denoiserOptions: Partial<INeuralNetworkGPUOptions> = {}; |
| 28 | + |
| 29 | + // Inherit the binary threshold of the parent autoencoder. |
| 30 | + denoiserOptions.binaryThresh = options.binaryThresh; |
| 31 | + // Inherit the hidden layers of the parent autoencoder. |
| 32 | + denoiserOptions.hiddenLayers = options.hiddenLayers; |
| 33 | + |
| 34 | + // Define the denoiser subnet's input and output sizes. |
| 35 | + if (options.decodedSize) denoiserOptions.inputSize = denoiserOptions.outputSize = options.decodedSize; |
| 36 | + |
| 37 | + // Create the denoiser subnet of the autoencoder. |
| 38 | + this.denoiser = new NeuralNetworkGPU<DecodedData, DecodedData>(options); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Denoise input data, removing any anomalies from the data. |
| 43 | + * @param {DecodedData} input |
| 44 | + * @returns {DecodedData} |
| 45 | + */ |
| 46 | + denoise(input: DecodedData): DecodedData { |
| 47 | + // Run the input through the generic denoiser. |
| 48 | + // This isn't the best denoiser implementation, but it's efficient. |
| 49 | + // Efficiency is important here because training should focus on |
| 50 | + // optimizing for feature extraction as quickly as possible rather than |
| 51 | + // denoising and anomaly detection; there are other specialized topologies |
| 52 | + // better suited for these tasks anyways, many of which can be implemented |
| 53 | + // by using an autoencoder. |
| 54 | + return this.denoiser.run(input); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Decode `EncodedData` into an approximation of its original form. |
| 59 | + * |
| 60 | + * @param {EncodedData} input |
| 61 | + * @returns {DecodedData} |
| 62 | + */ |
| 63 | + decode(input: EncodedData): DecodedData { |
| 64 | + // If the decoder has not been trained yet, throw an error. |
| 65 | + if (!this.decoder) throw new UntrainedNeuralNetworkError(this); |
| 66 | + |
| 67 | + // Decode the encoded input. |
| 68 | + return this.decoder.run(input); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Encode data to extract features, reduce dimensionality, etc. |
| 73 | + * |
| 74 | + * @param {DecodedData} input |
| 75 | + * @returns {EncodedData} |
| 76 | + */ |
| 77 | + encode(input: DecodedData): EncodedData { |
| 78 | + // If the decoder has not been trained yet, throw an error. |
| 79 | + if (!this.denoiser) throw new UntrainedNeuralNetworkError(this); |
| 80 | + |
| 81 | + // Process the input. |
| 82 | + this.denoiser.run(input); |
| 83 | + |
| 84 | + // Get the auto-encoded input. |
| 85 | + let encodedInput: TextureArrayOutput = this.encodedLayer as TextureArrayOutput; |
| 86 | + |
| 87 | + // If the encoded input is a `Texture`, convert it into an `Array`. |
| 88 | + if (encodedInput instanceof Texture) encodedInput = encodedInput.toArray(); |
| 89 | + else encodedInput = encodedInput.slice(0); |
| 90 | + |
| 91 | + // Return the encoded input. |
| 92 | + return encodedInput as EncodedData; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Test whether or not a data sample likely contains anomalies. |
| 97 | + * If anomalies are likely present in the sample, returns `true`. |
| 98 | + * Otherwise, returns `false`. |
| 99 | + * |
| 100 | + * @param {DecodedData} input |
| 101 | + * @returns {boolean} |
| 102 | + */ |
| 103 | + likelyIncludesAnomalies(input: DecodedData, anomalyThreshold: number = 0.2): boolean { |
| 104 | + // Create the anomaly vector. |
| 105 | + const anomalies: number[] = []; |
| 106 | + |
| 107 | + // Attempt to denoise the input. |
| 108 | + const denoised = this.denoise(input); |
| 109 | + |
| 110 | + // Calculate the anomaly vector. |
| 111 | + for (let i = 0; i < (input.length ?? 0); i++) { |
| 112 | + anomalies[i] = Math.abs((input as number[])[i] - (denoised as number[])[i]); |
| 113 | + } |
| 114 | + |
| 115 | + // Calculate the sum of all anomalies within the vector. |
| 116 | + const sum = anomalies.reduce( |
| 117 | + (previousValue, value) => previousValue + value |
| 118 | + ); |
| 119 | + |
| 120 | + // Calculate the mean anomaly. |
| 121 | + const mean = sum / (input as number[]).length; |
| 122 | + |
| 123 | + // Return whether or not the mean anomaly rate is greater than the anomaly threshold. |
| 124 | + return mean > anomalyThreshold; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Train the auto encoder. |
| 129 | + * |
| 130 | + * @param {DecodedData[]} data |
| 131 | + * @param {Partial<INeuralNetworkTrainOptions>} options |
| 132 | + * @returns {INeuralNetworkState} |
| 133 | + */ |
| 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 } ); |
| 139 | + } |
| 140 | + |
| 141 | + const results = this.denoiser.train(preprocessedData, options); |
| 142 | + |
| 143 | + this.decoder = this.createDecoder(); |
| 144 | + |
| 145 | + return results; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Create a new decoder from the trained denoiser. |
| 150 | + * |
| 151 | + * @returns {NeuralNetworkGPU<EncodedData, DecodedData>} |
| 152 | + */ |
| 153 | + private createDecoder() { |
| 154 | + const json = this.denoiser.toJSON(); |
| 155 | + |
| 156 | + const layers: IJSONLayer[] = []; |
| 157 | + const sizes: number[] = []; |
| 158 | + |
| 159 | + for (let i = this.encodedLayerIndex; i < this.denoiser.sizes.length; i++) { |
| 160 | + layers.push(json.layers[i]); |
| 161 | + sizes.push(json.sizes[i]); |
| 162 | + } |
| 163 | + |
| 164 | + json.layers = layers; |
| 165 | + json.sizes = sizes; |
| 166 | + |
| 167 | + json.options.inputSize = json.sizes[0]; |
| 168 | + |
| 169 | + const decoder = new NeuralNetworkGPU().fromJSON(json); |
| 170 | + |
| 171 | + return decoder as unknown as NeuralNetworkGPU<EncodedData, DecodedData>; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get the layer containing the encoded representation. |
| 176 | + */ |
| 177 | + private get encodedLayer(): KernelOutput { |
| 178 | + return this.denoiser.outputs[this.encodedLayerIndex]; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Get the offset of the encoded layer. |
| 183 | + */ |
| 184 | + private get encodedLayerIndex(): number { |
| 185 | + return Math.round(this.denoiser.outputs.length * 0.5) - 1; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +export default AE; |
0 commit comments