-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (103 loc) · 2.8 KB
/
Copy pathindex.js
File metadata and controls
110 lines (103 loc) · 2.8 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
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
const NeuralNet = require("../../lib/nn");
const csv = require("csvtojson");
const fs = require("fs");
require("colors");
const LoggerType = Object.freeze({
Error: 1,
Warn: 2,
Info: 3,
Data: 4
});
function debug(text, type) {
if (type) {
if (type === LoggerType.Error) {
process.stdout.write("ERROR: ".red);
console.error(JSON.stringify(text) || text);
} else if (type === LoggerType.Warn) {
process.stdout.write("WARN: ".yellow);
console.warn(JSON.stringify(text) || text);
} else if (type === LoggerType.Info) {
process.stdout.write("INFO: ".cyan);
console.log(JSON.stringify(text) || text);
} else if (type === LoggerType.Data) {
process.stdout.write("DATA: ".green);
console.log(JSON.stringify(text) || text);
}
return;
}
console.log(text);
}
const trainIP = [];
const trainOP = [];
const testIP = [];
const testOP = [];
const noOfInputs = 4;
const noOfOutputs = 3;
const nn = new NeuralNet.NeuralNetwork(noOfInputs, 5, noOfOutputs);
function outputArrGenerator(op, opSize) {
let opArr = new Array(opSize);
opArr.fill(0);
op = parseInt(op, 10);
opArr[op - 1] = 1;
return opArr;
}
csv()
.fromFile("./train.csv")
.on("json", jsonObj => {
const temp = Object.values(jsonObj);
trainOP.push(temp.slice(temp.length - 1, temp.length));
trainIP.push(temp.slice(0, temp.length - 1));
})
.on("done", error => {
if (error) {
throw error;
}
const epoch = 20000;
for (let i = 0; i < epoch; i++) {
const randomIndex = Math.floor(Math.random() * trainIP.length);
nn.train(trainIP[randomIndex], outputArrGenerator(trainOP[randomIndex][0], noOfOutputs));
}
debug("Training Complete...", LoggerType.Info);
});
const predVal = [];
csv()
.fromFile("./test.csv")
.on("json", jsonObj => {
const temp = Object.values(jsonObj);
testOP.push(temp.slice(temp.length - 1, temp.length));
testIP.push(temp.slice(0, temp.length - 1));
})
.on("done", error => {
if (error) {
throw error;
}
debug(
`|************************************|`,
LoggerType.Info
);
debug(
`| Predicted Class | Actual Class |`,
LoggerType.Info
);
debug(
`|************************************|`,
LoggerType.Info
);
for (let i = 0; i < testIP.length; i++) {
predVal.push(nn.predict(testIP[i]));
debug(
`| ${predVal[i].indexOf(Math.max(...predVal[i])) + 1} | ${testOP[i]} |`,
LoggerType.Info
); debug(
`|------------------------------------|`,
LoggerType.Info
);
}
const modal = nn.serialize();
fs.writeFile("./model.json", modal, err => {
if (err) {
debug(err, LoggerType.Error);
}
});
debug("end", LoggerType.Info);
});