-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.js
65 lines (56 loc) · 1.49 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
'use strict'
console.log('NeuralNetwork.js loaded')
class NeuralNetwork{
constructor(){
this.weights = []
this.biases = []
}
}
NeuralNetwork.prototype.build = function(structure){
// builds a new neural network
// structure - [2,4,5,2] => 2 inputs, 4nodes, 5nodes, 2 outputs
for(let i=0;i<structure.length-1;i++){
this.weights[i] = mCreate(structure[i+1],structure[i],1)
this.biases[i] = mCreate(structure[i+1],1,1)
}
}
NeuralNetwork.prototype.load = function(weights,biases){
// loads an existing neural network
this.weights = weights
this.biases = biases
}
NeuralNetwork.prototype.feedForward = function(inputs){
// feedForward
// inputs - [input0,input1,input2,...]
let currentVal=inputs
arrToVec(currentVal)
for(let i=0;i<this.weights.length;i++){
currentVal = mMulti(this.weights[i],currentVal)
mAddSig(currentVal,this.biases[i])
}
return currentVal
}
NeuralNetwork.prototype.createMutatedCopy = function(amp){
if(isNaN(amp)){
throw new Error('ERROR: isNaN(amp)')
}
const newMutatedNeuralNetwork = new NeuralNetwork()
// COPY
// weights
const weights = new Array(this.weights.length)
for(let i=0;i<this.weights.length;i++){
weights[i] = mCopy(this.weights[i])
// MUTATE
mMutate(weights[i],amp)
}
// biases
const biases = new Array(this.biases.length)
for(let i=0;i<this.biases.length;i++){
biases[i] = mCopy(this.biases[i])
// MUTATE
mMutate(biases[i],amp)
}
// LOAD && return
newMutatedNeuralNetwork.load(weights,biases)
return newMutatedNeuralNetwork
}