Skip to content

Commit a762a48

Browse files
committed
Backup: Another thunderstorm, power outage risk
1 parent 2a7840a commit a762a48

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

src/neural-network-gpu.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ function loss(
3636

3737
function updateMemory(
3838
this: IKernelFunctionThis,
39-
actual: number,
40-
expected: number,
41-
inputs: LossFunctionInputs,
4239
memory: NeuralNetworkMemory,
4340
memorySize: number,
44-
loss: number
41+
loss: number,
42+
outputs: number[][],
43+
sizes: number[]
4544
) {
4645
const layer = this.thread.z;
4746
const neuron = this.thread.y;
@@ -291,10 +290,19 @@ export class NeuralNetworkGPU<
291290
// @ts-expect-error
292291
biases: KernelOutput[] = [];
293292

293+
294+
losses: KernelOutput[] = [];
295+
ram: KernelOutput = ;
296+
294297
constructor(options: Partial<INeuralNetworkGPUOptions> = {}) {
295298
super(options);
296299
this.errorCheckInterval = 100;
297300
this.gpu = new GPU({ mode: options.mode });
301+
this._memoryFunction = options.memory ?? DEFAULT_MEMORY_FUNCTION;
302+
}
303+
304+
get loss(): number {
305+
return this._loss;
298306
}
299307

300308
get lossFunction(): LossFunction {
@@ -391,6 +399,14 @@ export class NeuralNetworkGPU<
391399
});
392400
}
393401

402+
// Determine whether `options.updateMemory` was passed to the constructor.
403+
if (this._memoryFunction) {
404+
// Get the defined memory function.
405+
const updateMemory = this._memoryKernel;
406+
// Update the neural network's memory matrix after each feed-forward pass.
407+
updateMemory(this.outputs, this.memory, this.sizes, this.memorySize, this.loss);
408+
}
409+
394410
this.texturizeInputData = this.gpu.createKernel(
395411
function (value: number[]): number {
396412
return value[this.thread.x];
@@ -442,7 +458,6 @@ export class NeuralNetworkGPU<
442458
}
443459

444460
const loss: LossFunction = this._lossFunction ?? DEFAULT_LOSS_FUNCTION;
445-
const updateMemory: MemoryFunction = this._memoryFunction ?? DEFAULT_MEMORY_FUNCTION;
446461

447462
calcDeltas = alias(
448463
utils.getMinifySafeName(() => calcDeltas),
@@ -523,6 +538,7 @@ export class NeuralNetworkGPU<
523538

524539
let output;
525540
if (layer === this.outputLayer) {
541+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
526542
// @ts-ignore
527543
output = this.backwardPropagate[layer](this.outputs[layer], target, this.outputs[0], this.memory);
528544
} else {

src/neural-network.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export type NeuralNetworkActivation =
5858
export interface IJSONLayer {
5959
biases: number[];
6060
weights: number[][];
61-
memory: number[][];
61+
memory?: number[][];
6262
}
6363

6464
export interface INeuralNetworkJSON {
@@ -78,6 +78,7 @@ export interface INeuralNetworkOptions {
7878
outputSize: number;
7979
binaryThresh: number;
8080
hiddenLayers?: number[];
81+
memory?: MemoryFunction;
8182
memorySize: number;
8283
}
8384

@@ -193,7 +194,7 @@ export class NeuralNetwork<
193194
_formatInput: NeuralNetworkFormatter | null = null;
194195
_formatOutput: NeuralNetworkFormatter | null = null;
195196

196-
_memory: NeuralNetworkMemory;
197+
_memory?: NeuralNetworkMemory;
197198

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

233+
this._memoryFunction = options.memory;
232234
// Initialize memory matrix
233235
const { memorySize } = this.options ?? 0;
234-
this._memory = this.replaceMemory(memorySize);
236+
if (memorySize) this._memory = this.replaceMemory(memorySize);
237+
}
238+
239+
public get memory(): NeuralNetworkMemory | undefined {
240+
return this._memory;
241+
}
242+
243+
public get memoryFunction(): MemoryFunction | undefined {
244+
return this._memoryFunction;
245+
}
246+
247+
public get memorySize(): number {
248+
if (!this._memory || !this._memory[0] || !this._memory[0][0]) return 0;
249+
return this._memory[0][0].length;
235250
}
236251

237252
/**
@@ -308,10 +323,6 @@ export class NeuralNetwork<
308323
return this.sizes.length > 0;
309324
}
310325

311-
public get memory(): NeuralNetworkMemory {
312-
return this._memory;
313-
}
314-
315326
run(input: Partial<InputType>): OutputType {
316327
if (!this.isRunnable) {
317328
throw new Error('network not runnable');
@@ -1234,19 +1245,23 @@ export class NeuralNetwork<
12341245
const jsonLayerBiases = this.biases.map((layerBiases) =>
12351246
Array.from(layerBiases)
12361247
);
1237-
const jsonLayerMemory = this.memory.map(layerMemory =>
1238-
layerMemory.map(
1239-
nodeMemory => Array.from(nodeMemory)
1240-
)
1241-
);
1248+
let jsonLayerMemory;
1249+
if (this.memory) {
1250+
jsonLayerMemory = this.memory.map(layerMemory =>
1251+
layerMemory.map(
1252+
nodeMemory => Array.from(nodeMemory)
1253+
)
1254+
);
1255+
}
12421256
const jsonLayers: IJSONLayer[] = [];
12431257
const outputLength = this.sizes.length - 1;
12441258
for (let i = 0; i <= outputLength; i++) {
1245-
jsonLayers.push({
1259+
const jsonLayer: IJSONLayer = {
12461260
weights: jsonLayerWeights[i] ?? [],
1247-
biases: jsonLayerBiases[i] ?? [],
1248-
memory: jsonLayerMemory[i] ?? []
1249-
});
1261+
biases: jsonLayerBiases[i] ?? []
1262+
};
1263+
if (jsonLayerMemory) jsonLayer.memory = jsonLayerMemory[i] ?? [];
1264+
jsonLayers.push(jsonLayer);
12501265
}
12511266
return {
12521267
type: 'NeuralNetwork',
@@ -1290,15 +1305,18 @@ export class NeuralNetwork<
12901305
const layerBiases = this.biases.map((layerBiases, layerIndex) =>
12911306
Float32Array.from(jsonLayers[layerIndex].biases)
12921307
);
1293-
const layerMemory = this.memory.map((memory, layerIndex) =>
1294-
Array.from(jsonLayers[layerIndex].memory).map(nodeMemory =>
1295-
Float32Array.from(nodeMemory)
1296-
)
1297-
);
1308+
let layerMemory;
1309+
if (this.memory) {
1310+
layerMemory = this.memory.map((memory, layerIndex) =>
1311+
Array.from(jsonLayers[layerIndex].memory ?? []).map(nodeMemory =>
1312+
Float32Array.from(nodeMemory)
1313+
)
1314+
);
1315+
}
12981316
for (let i = 0; i <= this.outputLayer; i++) {
12991317
this.weights[i] = layerWeights[i] || [];
13001318
this.biases[i] = layerBiases[i] || [];
1301-
this.memory[i] = layerMemory[i] || [];
1319+
if (layerMemory && this.memory) this.memory[i] = (layerMemory && layerMemory[i]) ?? [];
13021320
}
13031321
return this;
13041322
}

src/utilities/loss.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ export type LossFunction = (
1414

1515
export type MemoryFunction = (
1616
this: IKernelFunctionThis,
17-
actual: number,
18-
expected: number,
19-
inputs: LossFunctionInputs,
2017
memory: NeuralNetworkMemory,
2118
memorySize: number,
22-
loss: number
19+
loss: number,
20+
outputs: number[][],
21+
sizes: number[]
2322
) => number;

0 commit comments

Comments
 (0)