-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralNetwork.js
81 lines (76 loc) · 2.6 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
export default class NeuralNetwork {
constructor(shape, weights = false, biases = false) {
this.shape = shape
this.weights = []
this.biases = []
for (let i = 0; i < this.shape.length - 1; i++) {
this.weights.push([])
for (let j = 0; j < this.shape[i + 1]; j++) {
this.weights[i].push([])
for (let k = 0; k < this.shape[i]; k++) {
if (weights) {
this.weights[i][j].push(weights[i][j][k])
} else {
this.weights[i][j].push(Math.random() - 0.5)
}
}
}
}
for (let i = 0; i < this.shape.length - 1; i++) {
this.biases.push([])
for (let j = 0; j < this.shape[i + 1]; j++) {
if (biases) {
this.biases[i].push(biases[i][j])
} else {
this.biases[i].push(Math.random() - 0.5)
}
}
}
}
sigmoid(x) {
return 1 / (1 + Math.exp(-x))
}
softmax(array) {
let sum = 0
for (let i = 0; i < array.length; i++) {
sum += array[i]
}
let newArray = []
for (let i = 0; i < array.length; i++) {
newArray.push(array[i] / sum)
}
return newArray
}
getOutput(input) {
let activations = [input]
for (let i = 0; i < this.shape.length - 1; i++) {
activations.push([])
for (let j = 0; j < this.shape[i + 1]; j++) {
let activation = this.biases[i][j]
for (let k = 0; k < this.shape[i]; k++) {
activation += activations[i][k] * this.weights[i][j][k]
}
activations[i + 1].push(this.sigmoid(activation))
}
}
return this.softmax(activations[activations.length - 1])
}
mutate(rate, amount) {
for (let i = 0; i < this.weights.length; i++) {
for (let j = 0; j < this.weights[i].length; j++) {
for (let k = 0; k < this.weights[i][j].length; k++) {
if (Math.random() < rate) {
this.weights[i][j][k] += (Math.random() - 0.5) * amount
}
}
}
}
for (let i = 0; i < this.biases.length; i++) {
for (let j = 0; j < this.biases[i].length; j++) {
if (Math.random() < rate) {
this.biases[i][j] += (Math.random() - 0.5) * amount
}
}
}
}
}