-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
344 lines (299 loc) · 11.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Celsius to Fahrenheit test site</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
/>
</head>
<body>
<div
class="container"
style="
flex: 1;
justify-content: center;
align-content: center;
max-width: 500px;
margin-top: 50px;
"
>
<div
class="form-control text-center"
style="
font-weight: bold;
background-color: #b452ff;
border: 6px solid red;
"
>
<!-- DATA ADD FIELD -->
<label class="form-label mt-4">Add Celsius data: </label>
<input
class="form-control"
type="text"
id="cdata"
value="0.0,-5.0,9.44,-40,-85.56,-29.28,-9.33,38.17,2.46,31.11,-66.11,-79.99,-48.09,24.21"
/>
<label class="form-label mt-4">Add Fahrenheit data: </label>
<input
class="form-control"
type="text"
id="fdata"
value="32.0,23.0,49.0,-40.0,-122.0,-20.7,15.2,100.7,36.42,87.99,-87.0,-111.98,-54.56,75.57"
/>
<!-- <label class="form-label mt-4">Select Activation: </label>
<select
class="form-select"
id="acti"
aria-label="Default select example"
>
<option selected value="relu">Relu</option>
<option value="tanh">Tanh</option>
<option value="sigmoid">Sigmoid</option>
<option value="liner">Liner</option>
</select> -->
<button class="btn btn-primary mt-4" onclick="addData()">
Add the data
</button>
<div
class="mt-4 container text-center form-control"
style="background-color: black; border: 6px solid red;"
>
<h5 class="mt-4" style="color: #ff0303;">Added Data:</h5>
<p class="mt-4" style="color: #2dff2d;" id="output1"></p>
<p class="mt-4" style="color: #2dff2d;" id="output2"></p>
</div>
<!-- Add Layer and activation -->
<div class="row">
<div class="col">
<label class="form-label mt-4">Select Activation: </label>
<select
class="form-select"
id="default-acti"
aria-label="Default select example"
>
<option selected value="relu">ReLU</option>
<option value="tanh">Tanh</option>
<option value="sigmoid">Sigmoid</option>
<option value="linear">Linear</option>
</select>
</div>
<div class="col">
<label class="form-label mt-4">Number of hidden units:</label>
<input
class="form-control"
type="number"
id="default-units"
value="1"
/>
</div>
</div>
<div id="layers"></div>
<div class="row">
<div class="col">
<button class="btn btn-primary mt-4" id="addLayer">
Add another layer
</button>
</div>
<div class="col">
<button class="btn btn-primary mt-4" id="delLayer">
Delete layer
</button>
</div>
</div>
<!-- DATA TRAINING FIELD -->
<br />
<label class="form-label mt-4">Give Celsius Value: </label>
<input class="form-control" type="text" id="data" value="-6.67" />
<label class="form-label mt-4">Give No of Epochs Value: </label>
<input class="form-control" type="text" id="epoc" value="100" />
<button class="btn btn-primary mt-4" id="press">SUBMIT</button>
</div>
</div>
<div
class="mt-4 container text-center form-control"
style="background-color: black; border: 6px solid red;"
>
<h5 class="mt-4" style="color: #ff0303;">OUTPUT:</h5>
<p class="mt-4" style="color: #2dff2d;" id="output"></p>
</div>
<div class="container">
<canvas id="myChart" ></canvas>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js">
</script>
<script type="text/javascript">
var cel_valu = [];
var far_valu = [];
// var activation;
//const celsius_values = tf.tensor([-30, 45, 32, 4, 8, 29, 90, 12, 29, -40, 9, 30, 22, -12]);
//const fahrenheit_values = tf.tensor([-22, 113, 89.6, 39.2, 46.4, 84.2, 194, 53.6, 84.2, -40, 48.2, 86, 71.6, 10.4]);
// var celsius_values = [];
// var fahrenheit_values = [];
//console.log(fahrenheit_values);
// const layer_1 = tf.layers.dense({inputShape: [1], units: 1})
let addBtn = document.getElementById("addLayer");
addBtn.addEventListener("click", function () {
layers = document.getElementById("layers");
let new_layer = document.createElement("div");
new_layer.className = "row";
let new_acti_div = document.createElement("div");
new_acti_div.className = "col";
let relu_option = document.createElement("option");
relu_option.value = "relu";
relu_option.innerHTML = "ReLU";
relu_option.setAttribute("selected", true);
let tanh_option = document.createElement("option");
tanh_option.value = "tanh";
tanh_option.innerHTML = "TanH";
let sigmoid_option = document.createElement("option");
sigmoid_option.value = "sigmoid";
sigmoid_option.innerHTML = "Sigmoid";
let linear_option = document.createElement("option");
linear_option.value = "linear";
linear_option.innerHTML = "Linear";
let select_option = document.createElement("select");
select_option.className = "form-select mt-4";
select_option.appendChild(relu_option);
select_option.appendChild(tanh_option);
select_option.appendChild(sigmoid_option);
select_option.appendChild(linear_option);
new_acti_div.appendChild(select_option);
let new_units_div = document.createElement("div");
new_units_div.className = "col";
let unit_input = document.createElement("input");
unit_input.className = "form-control mt-4";
unit_input.setAttribute("type", "number");
unit_input.setAttribute("value", "1");
new_units_div.appendChild(unit_input);
new_layer.appendChild(new_acti_div);
new_layer.appendChild(new_units_div);
layers.appendChild(new_layer);
});
let delBtn = document.getElementById("delLayer");
delBtn.addEventListener("click", function () {
let layers = document.getElementById("layers");
if (layers.lastChild) {
layers.removeChild(layers.lastChild);
}
});
function addData() {
// activation = document.getElementById("acti").value;
var cel_val = document.getElementById("cdata").value;
var far_val = document.getElementById("fdata").value;
alert("data submitted" + cel_val + far_val);
cel_valu = cel_val.split(",").map(parseFloat);
far_valu = far_val.split(",").map(parseFloat);
// cel_valu.push(parseFloat(cel_val.value));
// far_valu.push(parseFloat(far_val.value));
console.log(cel_valu);
console.log(far_valu);
document.getElementById("output1").innerHTML = "Data_1: " + cel_valu;
document.getElementById("output2").innerHTML = "Data_2: " + far_valu;
}
document.getElementById("press").addEventListener("click", function () {
let cel_valu_len = cel_valu.length;
let far_valu_len = far_valu.length;
celsius_values = tf.tensor2d(cel_valu, [cel_valu_len, 1]);
console.log(celsius_values);
fahrenheit_values = tf.tensor2d(far_valu, [far_valu_len, 1]);
console.log(fahrenheit_values);
var datas = document.getElementById("data");
var ins = parseFloat(datas.value);
var epoc = document.getElementById("epoc");
let epocs = parseInt(epoc.value);
console.log(ins);
document.getElementById("output").innerText =
"Training Machine Please Wait...";
const model = tf.sequential({
layers: [tf.layers.dense({ inputShape: [1], units: 1 })]
});
let default_acti = document.getElementById("default-acti");
let default_units = document.getElementById("default-units");
model.add(
tf.layers.dense({
units: Number(default_units.value),
activation: default_acti.value
})
);
let layers = document.getElementById("layers");
let noOfChildren = layers.childElementCount;
for (let i = 0; i < noOfChildren; i++) {
let this_layer = layers.childNodes[i];
let acti_div = this_layer.children[0];
let units_div = this_layer.children[1];
let activation = acti_div.children[0].value;
let units = Number(units_div.children[0].value);
model.add(
tf.layers.dense({
units: units,
activation: activation
})
);
}
model.add(
tf.layers.dense({
units: 1
})
);
model.summary();
model.compile({
loss: "meanAbsoluteError",
optimizer: tf.train.adam(0.1),
metrics: ["mae"]
});
model
.fit(celsius_values, fahrenheit_values, { epochs: epocs })
.then((trained_object) => {
const inputTensor = tf.tensor2d([ins], [1, 1]);
console.log(inputTensor);
const answer = model.predict(inputTensor);
// To Display Loss Value and storing it in variable and passsing the values to charts() function.
var xyValues = []
trained_object.history["loss"].map(( a, index)=>{
xyValues.push({ 'x': index, 'y': a});
console.log({ 'x': index, 'y': a})
});
console.table(xyValues)
charts(xyValues)
for (let i = 0; i < model.getWeights().length; i++) {
console.log(model.getWeights()[i].dataSync());
}
// console.log(model.getWeights().length);
// console.log(trained_object.history["loss"][99]);
//console.log(answer.dataSync());
let answers = answer.dataSync()[0];
console.log(answers);
answers = answers.toFixed(2);
document.getElementById("output").innerHTML =
"Answer: " + answers + "° F";
});
// body...
});
// Create LOSS CHART
function charts(xy){
new Chart("myChart", {
type: "scatter",
data: {
datasets: [{
pointRadius: 4,
pointBackgroundColor: "black",
data: xy
}]
},
});
}
</script>
</body>
</html>