Skip to content

Commit

Permalink
Fix CI task errors
Browse files Browse the repository at this point in the history
  • Loading branch information
voidvoxel committed Jun 17, 2024
1 parent 3d392f1 commit ce98bf1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/errors/untrained-neural-network-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { INeuralNetworkData, NeuralNetwork } from "../neural-network";
import { NeuralNetworkGPU } from "../neural-network-gpu";
import { NeuralNetwork, NeuralNetworkIO } from "../neural-network";

export class UntrainedNeuralNetworkError extends Error {
export class UntrainedNeuralNetworkError<NeuralNetworkType extends NeuralNetwork<NeuralNetworkIO, NeuralNetworkIO>> extends Error {

Check failure on line 3 in src/errors/untrained-neural-network-error.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Type 'NeuralNetworkIO' does not satisfy the constraint 'INeuralNetworkData'.

Check failure on line 3 in src/errors/untrained-neural-network-error.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and windows-latest

Type 'NeuralNetworkIO' does not satisfy the constraint 'INeuralNetworkData'.

Check failure on line 3 in src/errors/untrained-neural-network-error.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Type 'NeuralNetworkIO' does not satisfy the constraint 'INeuralNetworkData'.

Check failure on line 3 in src/errors/untrained-neural-network-error.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and windows-latest

Type 'NeuralNetworkIO' does not satisfy the constraint 'INeuralNetworkData'.

Check failure on line 3 in src/errors/untrained-neural-network-error.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

Type 'NeuralNetworkIO' does not satisfy the constraint 'INeuralNetworkData'.
constructor (
neuralNetwork: object
) {
Expand Down
4 changes: 4 additions & 0 deletions src/neural-network-gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ export class NeuralNetworkGPU<
super.lossFunction = value;
}

public get ramFunction(): RAMFunction | undefined {
return super.ramFunction;
}

public set ramFunction(
value: RAMFunction | undefined
) {
Expand Down
45 changes: 40 additions & 5 deletions src/neural-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,33 @@ import { max } from './utilities/max';
import { mse } from './utilities/mse';
import { randos } from './utilities/randos';
import { zeros } from './utilities/zeros';
import { IKernelFunctionThis, KernelOutput } from 'gpu.js';
import { IKernelFunctionThis } from 'gpu.js';

/**
* An input or output layer of a neural network.
* This data type exists to allow kernel functions to operate on individual layers.
* This functionality is essential to custom loss functions.
*/
export type NeuralNetworkIO = number[] | number[][] | number[][][];

/**
* A read-write state matrix designed to hold metadata for use by `loss` functions.
* This data is read-only to the `loss` function.
* To the `updateRAM` kernel function, this data is read-write.
* To the neural network consumer,
* the `ram` property is made public to allow for modifications to be made in addition to reading.
*/
export type NeuralNetworkRAM = number[][][];

/**
* A loss function determines how fit a neural network currently is.
* The higher the value returned by this function,
* the less accurate the network is.
* The lower the value returned by this function is,
* the more accurate the network is.
*
* Here, `ram` is read-only.
*/
export type LossFunction = (
this: IKernelFunctionThis,
actual: number,
Expand All @@ -26,6 +47,13 @@ export type LossFunction = (
ram: NeuralNetworkRAM
) => number;

/**
* A RAM function updates the RAM matrix of the neural network.
*
* Here, `ram` is read-write.
* The actual matrix passed to the function is read-only.
* However, the return value of the function directly corresponds to a value within the RAM matrix.
*/
export type RAMFunction = (
this: IKernelFunctionThis,
ram: NeuralNetworkRAM,
Expand All @@ -36,6 +64,10 @@ export type RAMFunction = (
lossDelta: number
) => number;

/**
* Each time the loss is calculated,
* a snapshot is taken of various loss analytics.
*/
export interface ILossAnalyticsSnapshot {
mean: number;
median: number;
Expand All @@ -50,10 +82,13 @@ const EMPTY_LOSS_SNAPSHOT: ILossAnalyticsSnapshot = {

Object.freeze(EMPTY_LOSS_SNAPSHOT);

function createLossAnalyticsSnapshot() {
function createLossAnalyticsSnapshot(): ILossAnalyticsSnapshot {
return JSON.parse(JSON.stringify(EMPTY_LOSS_SNAPSHOT));
}

/**
* A collection of analytics pertaining to the results of the loss function.
*/
export interface ILossAnalytics {
current: ILossAnalyticsSnapshot;
max: ILossAnalyticsSnapshot;
Expand All @@ -72,7 +107,7 @@ const EMPTY_LOSS: ILossAnalytics = {

Object.freeze(EMPTY_LOSS);

export function createLossAnalytics() {
export function createLossAnalytics(): ILossAnalytics {
return JSON.parse(JSON.stringify(EMPTY_LOSS));
}

Expand Down Expand Up @@ -109,7 +144,7 @@ function loss(
expected: number,
inputs: NeuralNetworkIO,
ram: NeuralNetworkRAM
) {
): number {
return expected - actual;
}

Expand Down Expand Up @@ -438,7 +473,7 @@ export class NeuralNetwork<
return (output as unknown) as OutputType;
}

protected _updateRAM() {
protected _updateRAM(): void {
if (this.ram) {
const updateRAM: RAMFunction | undefined = this.ramFunction;

Expand Down

0 comments on commit ce98bf1

Please sign in to comment.