Skip to content

Commit

Permalink
Polish autoencoders and remove debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
voidvoxel committed Jun 18, 2024
1 parent 00b8515 commit e4e6906
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/autoencoder-gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './neural-network-gpu';
import { INeuralNetworkState } from './neural-network-types';
import { UntrainedNeuralNetworkError } from './errors/untrained-neural-network-error';
import { DEFAULT_ANOMALY_THRESHOLD } from './autoencoder';

function loss(
this: IKernelFunctionThis,
Expand Down Expand Up @@ -131,6 +132,7 @@ export class AutoencoderGPU<
input: DecodedData,
anomalyThreshold: number
): boolean {
anomalyThreshold ??= DEFAULT_ANOMALY_THRESHOLD;
// Create the anomaly vector.
const anomalies: number[] = [];

Expand Down
36 changes: 19 additions & 17 deletions src/autoencoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
import { INeuralNetworkState } from './neural-network-types';
import { UntrainedNeuralNetworkError } from './errors/untrained-neural-network-error';

export const DEFAULT_ANOMALY_THRESHOLD = 0.5;

function loss(
this: IKernelFunctionThis,
actual: number,
Expand Down Expand Up @@ -63,6 +65,20 @@ export class Autoencoder<
super(options);
}

/**
* Get the layer containing the encoded representation.
*/
private get encodedLayer(): KernelOutput {
return this.outputs[this.encodedLayerIndex];
}

/**
* Get the offset of the encoded layer.
*/
private get encodedLayerIndex(): number {
return Math.round(this.outputs.length * 0.5) - 1;
}

/**
* Denoise input data, removing any anomalies from the data.
* @param {DecodedData} input
Expand Down Expand Up @@ -130,6 +146,8 @@ export class Autoencoder<
input: DecodedData,
anomalyThreshold: number
): boolean {
anomalyThreshold ??= DEFAULT_ANOMALY_THRESHOLD;

// Create the anomaly vector.
const anomalies: number[] = [];

Expand All @@ -151,8 +169,6 @@ export class Autoencoder<
// Calculate the mean anomaly.
const mean = sum / (input as number[]).length;

console.log(sum, mean, anomalyThreshold);

// Return whether or not the mean anomaly rate is greater than the anomaly threshold.
return mean > anomalyThreshold;
}
Expand Down Expand Up @@ -195,7 +211,7 @@ export class Autoencoder<
*
* @returns {NeuralNetwork<EncodedData, DecodedData>}
*/
private createDecoder() {
private createDecoder(): NeuralNetwork<EncodedData, DecodedData> {
const json = this.toJSON();

const layers: IJSONLayer[] = [];
Expand All @@ -215,20 +231,6 @@ export class Autoencoder<

return (decoder as unknown) as NeuralNetwork<EncodedData, DecodedData>;
}

/**
* Get the layer containing the encoded representation.
*/
private get encodedLayer(): KernelOutput {
return this.outputs[this.encodedLayerIndex];
}

/**
* Get the offset of the encoded layer.
*/
private get encodedLayerIndex(): number {
return Math.round(this.outputs.length * 0.5) - 1;
}
}

export default Autoencoder;

0 comments on commit e4e6906

Please sign in to comment.