-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNeuralNetwork.js
143 lines (127 loc) · 3.92 KB
/
NeuralNetwork.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const activationFns = {
relu: z => z > 0 ? z : 0,
RELU: z => z > 0 ? z : 0,
leakyRELU: z => z > 0 ? z : 0.01*z,
sigmoid: z => 1 / (1 + Math.exp(-z)),
linear: z => z,
tanh: z => Math.tanh(z)
}
class NeuralNetwork {
constructor(numInputs, {seed = Math.random(), activationFn = "sigmoid"} = {}) {
this.randomseeded = SeedRandom(seed)
this.numInputs = numInputs
this.layers = [] // will later be: [4, 2] or similar
this.weights = []
this.biases = []
this.activations = []
this.neuronsBeforeLayer = [0]
this.activationFn = activationFn
}
addLayer(numNeurons) {
const layerIndex = this.layers.length
let lastLayerLength = this.prevSize(layerIndex)
this.neuronsBeforeLayer.push(this.neuronsBeforeLayer[layerIndex] + numNeurons)
this.layers.push(numNeurons)
for(let n = 0; n < numNeurons; n++) {
this.biases.push(this.rand(-1, 1))
this.activations.push(0)
for(let a = 0; a < lastLayerLength; a++) {
this.weights.push(this.rand(-1, 1))
}
}
}
fireNeuron(layer, neuron) {
let pos = this.neuronsBeforeLayer[layer] + neuron
let sum = 0
for(let a = 0; a < this.prevSize(layer); a++) {
sum += this.pickWeight(layer, neuron, a) * this.pickInput(layer, a)
}
sum += this.biases[pos]
this.activations[pos] = this.output(sum)
}
feedForward(inputActivations) {
console.assert(inputActivations.length === this.numInputs)
this.inputActivations = inputActivations
for(let layer = 0; layer < this.layers.length; layer++) {
for(let n = 0; n < this.layers[layer]; n++) {
this.fireNeuron(layer, n, inputActivations)
}
}
return this.activations.slice(this.neuronsBeforeLayer[this.layers.length-1])
}
output(z) {
return activationFns[this.activationFn](z)
}
pickWeight(layer, neuron, inputNr) {
let count = 0
for(let l = 0; l < layer; l++) {
count += this.layers[l] * this.prevSize(l)
}
count += neuron * this.prevSize(layer)
return this.weights[count + inputNr]
}
pickInput(layer, inputNr) {
if(layer === 0) return this.inputActivations[inputNr]
return this.activations[this.neuronsBeforeLayer[layer-1] + inputNr]
}
prevSize(layer) {
return layer === 0 ? this.numInputs : this.layers[layer-1]
}
exportState() {
let {weights, biases} = this
return JSON.stringify({weights, biases})
}
importState(state) {
let {weights, biases} = JSON.parse(state)
Object.assign(this, {weights, biases})
}
clone() {
let nn = new NeuralNetwork(this.numInputs, {seed: this.randomseeded(1e9)*1e-9, activationFn: this.activationFn})
this.layers.forEach(l=>nn.addLayer(l))
nn.weights = [...this.weights]
nn.biases = [...this.biases]
return nn
}
randomAdjust(amount) {
this.weights.forEach((_, i) => this.weights[i] += gauss(amount, this.randomseeded))
this.biases.forEach((_, i) => this.biases[i] += gauss(amount, this.randomseeded))
}
rand(low, high) {
return low + (high - low) * Math.round(this.randomseeded(1e9) / 1e7) / 100
}
}
function gauss(variance, randomseeded = Math.random) {
var u = 0, v = 0;
while(u === 0) u = randomseeded(1e9)*1e-9; //Converting [0,1) to (0,1)
while(v === 0) v = randomseeded(1e9)*1e-9;
return variance * Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}
function SeedRandom(state1,state2){
var mod1=4294967087
var mul1=65539
var mod2=4294965887
var mul2=65537
if(typeof state1!="number"){
state1=+new Date()
}
if(typeof state2!="number"){
state2=state1
}
state1=state1%(mod1-1)+1
state2=state2%(mod2-1)+1
function random(limit){
state1=(state1*mul1)%mod1
state2=(state2*mul2)%mod2
if(state1<limit && state2<limit && state1<mod1%limit && state2<mod2%limit){
return random(limit)
}
return (state1+state2)%limit
}
return random
}
/*
let nn = new NeuralNetwork(6)
nn.addLayer(4)
nn.addLayer(2)
console.log(nn.feedForward([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]));
*/