-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
183 lines (153 loc) · 5.51 KB
/
index.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<html>
<head>
<title>
NOR GATE Classfier using TensorFlow.js
</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<!--Including TensorFlow.js script in our html document-->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.9.0"></script>
<script>
<!--We will use TensorFlow.js here, construct our machine learning model and train it.-->
<!--We will store the losses for each epoch in this array-->
var losses = [];
var epochs = [];
<!--Creating tensor for input data.-->
const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
<!--Creating tensor for output data-->
const ys = tf.tensor2d([[1], [0], [0], [0]]);
<!--Defining the weights and bias-->
const w = tf.variable(tf.randomNormal([2, 1], 0, 0.1));
console.log("--------Initial-------");
w.print();
const b = tf.variable(tf.randomNormal([1]))
b.print();
console.log("--------Initial-------");
<!--This is a output function, which takes in the input. Multiples the weights with the input and performs RELU on that-->
const outputFunction = input => {
var weightInputMul = tf.matMul(input, w);
var biasAdded = tf.add(weightInputMul, b);
return tf.relu(biasAdded);
}
<!--This is a loss function which caluclates mean square error-->
const lossFunction = (output, y) => output.sub(y).square().mean();
function startTraining(){
document.getElementById("epochInput").disabled=true;
document.getElementById("learningRateInput").disabled=true;
const epoch = document.getElementById("epochInput").value;
const learningRate = document.getElementById("learningRateInput").value;
const optimizer = tf.train.sgd(learningRate);
for (let i = 0; i < epoch; i++) {
w.dataSync();
b.dataSync();
optimizer.minimize(() => {
var loss = lossFunction(outputFunction(xs), ys);
loss.data().then(lossValue => {
w.data().then(weightsValue => {
b.data().then(biasValue => {
// console.log("Epoch: "+i+" Loss: "+lossValue+ " Weight: "+weightsValue+" Bias: "+biasValue);
epochs.push(i);
losses.push(lossValue);
if(i==(epoch-1))
{
console.log("--------Final-------");
w.dataSync();
w.print();
b.dataSync();
b.print();
console.log("--------Final-------");
generateGraph();
}
})
})
})
return loss;
});
}
}
function generateGraph(){
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: epochs,
datasets: [{
data: losses,
label: "Loss",
borderColor: "#3e95cd",
fill: false
}
]
},
options: {
title: {
display: true,
text: 'Loss change over epoch'
}
}
});
}
function generateOutput(){
b.dataSync();
w.dataSync();
const firstInput = document.getElementById("firstInput").value;
const secondInput = document.getElementById("secondInput").value;
const sampleInput = tf.tensor2d([[firstInput, secondInput]]);
const pred = outputFunction(sampleInput).dataSync();
document.getElementById("sampleOutput").innerHTML = "The predicted output for input "+firstInput+" and "+secondInput+" is "+Number(pred);
}
</script>
<body>
<div class = "container">
<h1>OR GATE Neural Network Implementation using TensorFlow.js</h1>
<h2>Truth Table for OR Gate</h2>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Input 1</th>
<th scope="col">Input 2</th>
<th scope="col">output</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<label for="learningRateInput">Learning Rate</label>
<input id="learningRateInput" value="0.15" type="text" class="form-control" placeholder="Enter Learning Rate">
<label for="epochInput">Epoch</label>
<input id="epochInput" value="1000" type="text" class="form-control" placeholder="Enter Number of Epochs">
<center>
<button id="train" onclick="startTraining();" type="button" class="btn btn-light btn-lg">TRAIN</button>
<center>
<canvas id="myChart" width="400" height="400"></canvas>
</center>
<label for="firstInput">Enter Input Value 1</label>
<input id="firstInput" type="text" class="form-control" placeholder="Enter Input Value 1">
<label for="secondInput">Enter Input Value 2</label>
<input id="secondInput" type="text" class="form-control" placeholder="Enter Input Value 2">
<button id="outputbutton" onclick="generateOutput();" type="button" class="btn btn-dark btn-lg">Generate Output</button>
<h3 id="sampleOutput"></h3>
</div>
</body>
</head>
</html>