-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivate.js
More file actions
66 lines (62 loc) · 1.84 KB
/
activate.js
File metadata and controls
66 lines (62 loc) · 1.84 KB
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
// Loads training data
function trainingDataPreparation () {
return [].concat(cats.training)
.concat(rainbows.training)
.concat(trains.training)
.concat(dogs.training)
.concat(horses.training)
.concat(houses.training)
.concat(plants.training)
}
// Runs training
function trainingNN (nn) {
// Getting array with all data to be trained
// training now is a 2D array
const training = trainingDataPreparation()
// Shuffling training array
shuffle(training, true)
// Training nn for one epoch
for (let i = 0; i < training.length; i++) {
// Temp array for be used in the nn.train
let data = training[i]
// Filling inputs one-dimension array
let inputs = Array.from(data).map(x => x / 255)
let label = training[i].label
// One-hot encoding
let targets = [0, 0, 0, 0, 0, 0, 0]
targets[label] = 1
// Train nn
nn.train(inputs, targets)
}
}
// Load testing data
function testingDataPreparation () {
return [].concat(cats.testing)
.concat(rainbows.testing)
.concat(trains.testing)
.concat(dogs.testing)
.concat(horses.testing)
.concat(houses.testing)
.concat(plants.testing)
}
// Runs testing and returns percentage
function testingNN (nn) {
const testing = testingDataPreparation()
let correct = 0
// Training nn for one epoch
for (let i = 0; i < testing.length; i++) {
// Temp array for be used in the nn.train
let data = testing[i]
// Filling inputs one-dimension array
let inputs = Array.from(data).map(x => x / 255);
let label = data.label
// Testing nn - predict
let guess = nn.predict(inputs)
// Getting the index of the biggest value
let m = max(guess)
let classification = guess.indexOf(m)
correct += (classification === label) ? 1 : 0
}
// Returning percentage of corrects
return 100 * correct / testing.length
}