-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (90 loc) · 3.17 KB
/
index.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
import * as tf from '@tensorflow/tfjs';
import * as tfvis from '@tensorflow/tfjs-vis';
import { ForestDataset, FEATURE_NAMES } from './data';
import { normalizeData } from './utils'
import * as ui from './ui';
import regeneratorRuntime from "regenerator-runtime";
import { outerProduct, tensor } from '@tensorflow/tfjs';
const forestdata = new ForestDataset();
const tensors = {}
const LEARNING_RATE = 0.01
const EPOCHS = 50
const BATCH_SIZE = 32
/**
* Convert Array of Arrays to Tensors, and normalize the features
*/
export function arrayToTensor() {
tensors['Xtrain_tf'] = normalizeData(tf.tensor2d(forestdata.Xtrain));
tensors['Xtest_tf'] = normalizeData(tf.tensor2d(forestdata.Xtest));
tensors['ytrain_tf'] = normalizeData(tf.tensor2d(forestdata.ytrain));
tensors['ytest_tf'] = normalizeData(tf.tensor2d(forestdata.ytest));
}
export function CreateNeuralNetwork(){
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [forestdata.dataShape],
units: 32,
activation: 'relu',
}));
model.add(tf.layers.dense({
units: 16,
activation: "relu",
}));
model.add(tf.layers.dense({
units: 1,
activation: 'sigmoid',
}));
model.summary();
return model;
}
/**
* Trains the neural Network and prints the result
*/
export async function train(model){
let trainingLogs = [];
let chartbox = document.getElementById('chart')
model.compile({
optimizer: tf.train.adam(LEARNING_RATE),
loss: 'binaryCrossentropy',
metrics: ['accuracy'],
});
ui.updateStatus("Training started....")
await model.fit(tensors.Xtrain_tf, tensors.ytrain_tf,{
batchSize: BATCH_SIZE,
epochs: EPOCHS,
validationSplit: 0.2,
callbacks:{
onEpochEnd: async(curr_epoch, logs)=>{
await ui.updateTrainingStatus(curr_epoch, EPOCHS)
trainingLogs.push(logs);
//plot the training chart
tfvis.show.history(chartbox, trainingLogs, ['loss', 'val_loss', 'acc'])
}
}
});
ui.updateStatus("Evaluating model on test data")
const result = model.evaluate(tensors.Xtest_tf, tensors.ytest_tf, {
batchSize: BATCH_SIZE,
});
const m_accuracy = result[1].dataSync()[0] * 100;
const test_loss = result[0].dataSync()[0];
const train_loss = trainingLogs[trainingLogs.length - 1].loss;
const val_loss = trainingLogs[trainingLogs.length - 1].val_loss;
await ui.updateTrainingStatus(train_loss, val_loss, test_loss, m_accuracy)
};
export async function prediction(model, data) {
let predData = normalizeData(tf.tensor2d(data));
const pred = model.predict(predData, {
batchSize: 2
});
pred.print();
}
//Download and convert data to tensor as soon as the page is loaded
document.addEventListener('DOMContentLoaded', async () => {
ui.updateStatus("Loading Data set and Converting to Tensors....")
await forestdata.loadAllData()
arrayToTensor();
ui.updateStatus("Data Loaded Successfully....")
document.getElementById('trainModel').style.display = 'block'
await ui.setUp()
}, false);