Skip to content

Commit

Permalink
Finish updating autoencoder to use loss function
Browse files Browse the repository at this point in the history
  • Loading branch information
voidvoxel committed Jun 18, 2024
1 parent 52edc88 commit 8f8f455
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/autoencoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AutoencoderGPU from './autoencoder';
import { INeuralNetworkTrainOptions } from './neural-network';

const trainingData = [
[0, 0, 0],
Expand All @@ -9,15 +10,22 @@ const trainingData = [

const xornet = new AutoencoderGPU<number[], number[]>({
inputSize: 3,
hiddenLayers: [5, 2, 5],
hiddenLayers: [4, 2, 4],
outputSize: 3,
});

const errorThresh = 0.011;
const errorThresh = 0.0011;

const result = xornet.train(trainingData, {
iterations: 100000,
const trainOptions: Partial<INeuralNetworkTrainOptions> = {
errorThresh,
});
iterations: 250000,
learningRate: 0.1,
log: (details) => console.log(details),
// logPeriod: 500,
logPeriod: 500,
};

const result = xornet.train(trainingData, trainOptions);

test('denoise a data sample', async () => {
expect(result.error).toBeLessThanOrEqual(errorThresh);
Expand Down Expand Up @@ -58,7 +66,7 @@ test('test a data sample for anomalies', async () => {
expect(result.error).toBeLessThanOrEqual(errorThresh);

function includesAnomalies(...args: number[]) {
expect(xornet.likelyIncludesAnomalies(args)).toBe(false);
expect(xornet.likelyIncludesAnomalies(args, 0.5)).toBe(false);
}

includesAnomalies(0, 0, 0);
Expand Down
11 changes: 8 additions & 3 deletions src/autoencoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function loss(
) {
let error = expected - actual;

// if ( o ≈ i0 ) then return 10% of the loss value.
// Otherwise, return 1000% of the full loss value.
// if ( o ≈ i0 ) then return 3.125% of the loss value.
// Otherwise, return 3200% of the full loss value.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (Math.round(actual) !== Math.round(inputs[this.thread.x])) error *= 32;
Expand Down Expand Up @@ -127,7 +127,10 @@ export class AutoencoderGPU<
* @param {DecodedData} input
* @returns {boolean}
*/
likelyIncludesAnomalies(input: DecodedData, anomalyThreshold = 0.2): boolean {
likelyIncludesAnomalies(
input: DecodedData,
anomalyThreshold: number
): boolean {
// Create the anomaly vector.
const anomalies: number[] = [];

Expand All @@ -149,6 +152,8 @@ export class AutoencoderGPU<
// 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

0 comments on commit 8f8f455

Please sign in to comment.