-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.ts
55 lines (48 loc) · 1.65 KB
/
layer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Neuron from "./neuron"
import { ActFunc } from './types'
export default class Layer {
neurons: Neuron[]
inputBuffer: number[]
inputBufferSize: number
constructor(activationFunction: ActFunc, neuronCount: number, inputBufferSize: number) {
this.neurons = this.initializeNeurons(
activationFunction,
neuronCount,
inputBufferSize
)
this.inputBuffer = []
this.inputBufferSize = inputBufferSize
}
applyInputs(inputs: number[]): void {
if (inputs.length !== this.inputBufferSize) {
throw new Error("Layer received incorrect number of inputs")
}
this.inputBuffer = inputs
}
private initializeNeurons(
activationFunction: ActFunc,
neuronCount: number,
inputBufferSize: number
): Neuron[] {
let arrayOfNeurons: Neuron[] = []
for (let i = 0; i < neuronCount; i++) {
let arrayOfWeights: number[] = []
for (let j = 0; j < inputBufferSize; j++) {
arrayOfWeights[j] = this.generateSmallNonzeroValue()
}
arrayOfNeurons[i] = new Neuron(
activationFunction,
arrayOfWeights,
this.generateSmallNonzeroValue()
)
}
return arrayOfNeurons
}
// TODO Comment about what/why
private generateSmallNonzeroValue() {
const numberNearZero = 2 * Math.random() - 1
const nonZeroNumber = numberNearZero === 0 ? 1 : numberNearZero
const numberEvenNearerToZero = 3 * (nonZeroNumber ** 3 + nonZeroNumber / 3) / 4
return numberEvenNearerToZero
}
}