Skip to content

Commit

Permalink
Backup: Another thunderstorm, power outage risk
Browse files Browse the repository at this point in the history
  • Loading branch information
voidvoxel committed Jun 16, 2024
1 parent 2a7840a commit a762a48
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
26 changes: 21 additions & 5 deletions src/neural-network-gpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ function loss(

function updateMemory(
this: IKernelFunctionThis,
actual: number,
expected: number,
inputs: LossFunctionInputs,
memory: NeuralNetworkMemory,
memorySize: number,
loss: number
loss: number,
outputs: number[][],
sizes: number[]
) {
const layer = this.thread.z;
const neuron = this.thread.y;
Expand Down Expand Up @@ -291,10 +290,19 @@ export class NeuralNetworkGPU<
// @ts-expect-error
biases: KernelOutput[] = [];


losses: KernelOutput[] = [];
ram: KernelOutput = ;

constructor(options: Partial<INeuralNetworkGPUOptions> = {}) {
super(options);
this.errorCheckInterval = 100;
this.gpu = new GPU({ mode: options.mode });
this._memoryFunction = options.memory ?? DEFAULT_MEMORY_FUNCTION;
}

get loss(): number {
return this._loss;
}

get lossFunction(): LossFunction {
Expand Down Expand Up @@ -391,6 +399,14 @@ export class NeuralNetworkGPU<
});
}

// Determine whether `options.updateMemory` was passed to the constructor.
if (this._memoryFunction) {
// Get the defined memory function.
const updateMemory = this._memoryKernel;
// Update the neural network's memory matrix after each feed-forward pass.
updateMemory(this.outputs, this.memory, this.sizes, this.memorySize, this.loss);
}

this.texturizeInputData = this.gpu.createKernel(
function (value: number[]): number {
return value[this.thread.x];
Expand Down Expand Up @@ -442,7 +458,6 @@ export class NeuralNetworkGPU<
}

const loss: LossFunction = this._lossFunction ?? DEFAULT_LOSS_FUNCTION;
const updateMemory: MemoryFunction = this._memoryFunction ?? DEFAULT_MEMORY_FUNCTION;

calcDeltas = alias(
utils.getMinifySafeName(() => calcDeltas),
Expand Down Expand Up @@ -523,6 +538,7 @@ export class NeuralNetworkGPU<

let output;
if (layer === this.outputLayer) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
output = this.backwardPropagate[layer](this.outputs[layer], target, this.outputs[0], this.memory);
} else {
Expand Down
62 changes: 40 additions & 22 deletions src/neural-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type NeuralNetworkActivation =
export interface IJSONLayer {
biases: number[];
weights: number[][];
memory: number[][];
memory?: number[][];
}

export interface INeuralNetworkJSON {
Expand All @@ -78,6 +78,7 @@ export interface INeuralNetworkOptions {
outputSize: number;
binaryThresh: number;
hiddenLayers?: number[];
memory?: MemoryFunction;
memorySize: number;
}

Expand Down Expand Up @@ -193,7 +194,7 @@ export class NeuralNetwork<
_formatInput: NeuralNetworkFormatter | null = null;
_formatOutput: NeuralNetworkFormatter | null = null;

_memory: NeuralNetworkMemory;
_memory?: NeuralNetworkMemory;

runInput: (input: Float32Array) => Float32Array = (input: Float32Array) => {
this.setActivation();
Expand Down Expand Up @@ -229,9 +230,23 @@ export class NeuralNetwork<
this.sizes = [inputSize].concat(hiddenLayers ?? []).concat([outputSize]);
}

this._memoryFunction = options.memory;
// Initialize memory matrix
const { memorySize } = this.options ?? 0;
this._memory = this.replaceMemory(memorySize);
if (memorySize) this._memory = this.replaceMemory(memorySize);
}

public get memory(): NeuralNetworkMemory | undefined {
return this._memory;
}

public get memoryFunction(): MemoryFunction | undefined {
return this._memoryFunction;
}

public get memorySize(): number {
if (!this._memory || !this._memory[0] || !this._memory[0][0]) return 0;
return this._memory[0][0].length;
}

/**
Expand Down Expand Up @@ -308,10 +323,6 @@ export class NeuralNetwork<
return this.sizes.length > 0;
}

public get memory(): NeuralNetworkMemory {
return this._memory;
}

run(input: Partial<InputType>): OutputType {
if (!this.isRunnable) {
throw new Error('network not runnable');
Expand Down Expand Up @@ -1234,19 +1245,23 @@ export class NeuralNetwork<
const jsonLayerBiases = this.biases.map((layerBiases) =>
Array.from(layerBiases)
);
const jsonLayerMemory = this.memory.map(layerMemory =>
layerMemory.map(
nodeMemory => Array.from(nodeMemory)
)
);
let jsonLayerMemory;
if (this.memory) {
jsonLayerMemory = this.memory.map(layerMemory =>
layerMemory.map(
nodeMemory => Array.from(nodeMemory)
)
);
}
const jsonLayers: IJSONLayer[] = [];
const outputLength = this.sizes.length - 1;
for (let i = 0; i <= outputLength; i++) {
jsonLayers.push({
const jsonLayer: IJSONLayer = {
weights: jsonLayerWeights[i] ?? [],
biases: jsonLayerBiases[i] ?? [],
memory: jsonLayerMemory[i] ?? []
});
biases: jsonLayerBiases[i] ?? []
};
if (jsonLayerMemory) jsonLayer.memory = jsonLayerMemory[i] ?? [];
jsonLayers.push(jsonLayer);
}
return {
type: 'NeuralNetwork',
Expand Down Expand Up @@ -1290,15 +1305,18 @@ export class NeuralNetwork<
const layerBiases = this.biases.map((layerBiases, layerIndex) =>
Float32Array.from(jsonLayers[layerIndex].biases)
);
const layerMemory = this.memory.map((memory, layerIndex) =>
Array.from(jsonLayers[layerIndex].memory).map(nodeMemory =>
Float32Array.from(nodeMemory)
)
);
let layerMemory;
if (this.memory) {
layerMemory = this.memory.map((memory, layerIndex) =>
Array.from(jsonLayers[layerIndex].memory ?? []).map(nodeMemory =>
Float32Array.from(nodeMemory)
)
);
}
for (let i = 0; i <= this.outputLayer; i++) {
this.weights[i] = layerWeights[i] || [];
this.biases[i] = layerBiases[i] || [];
this.memory[i] = layerMemory[i] || [];
if (layerMemory && this.memory) this.memory[i] = (layerMemory && layerMemory[i]) ?? [];
}
return this;
}
Expand Down
7 changes: 3 additions & 4 deletions src/utilities/loss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export type LossFunction = (

export type MemoryFunction = (
this: IKernelFunctionThis,
actual: number,
expected: number,
inputs: LossFunctionInputs,
memory: NeuralNetworkMemory,
memorySize: number,
loss: number
loss: number,
outputs: number[][],
sizes: number[]
) => number;

0 comments on commit a762a48

Please sign in to comment.