-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfjs.html
69 lines (58 loc) · 2.6 KB
/
tfjs.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
<html>
<head>
<!-- <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
</head>
<body>
x: <input type="number" id="x"/>
y: <input type="number" id="y"/>
<input type="button" id="append" value="submit"/>
<div style="padding:50px;">
<canvas id="myChart" width="600" height="300"></canvas>
</div>
<script>
var xs = []
var ys = []
document.getElementById('x').value=1;
document.getElementById('append').onclick = function(){
const model = tf.sequential();
model.add(tf.layers.dense({units:128, inputShape:[1]}));
model.add(tf.layers.dense({units:128, inputShape:[128], activation:"sigmoid"}));
model.add(tf.layers.dense({units:1, inputShape:[128]}));
const optimizer = tf.train.adam(0.01);
model.compile({loss:"meanSquaredError", optimizer:optimizer})
var x = document.getElementById('x').value;
var y = document.getElementById('y').value;
xs.push(x);
ys.push(y);
document.getElementById('x').value=parseInt(x)+1;
model.fit(tf.tensor(xs), tf.tensor(ys),{epochs:200}).then(() =>{
bestfit = model.predict(tf.tensor2d(xs, [xs.length,1])).dataSync();
var ctx = document.getElementById("myChart").getContext('2d'); //begin chart
//Chart data and settings
var myChart = new Chart(ctx, {
type: 'line',
options: {scales:{yAxes:[{ticks: {beginAtZero: true}}]}},
data:{
labels:xs,
datasets:[
{
label:'Original Data',
data: ys,
borderWidth: 1,
},
{
label:'Bestfit',
data: bestfit,
borderWidth: 1,
borderColor: "#FF0000",
backgroundColor: "rgba(1,1,1,0)",
},]
},
});
});
}
</script>
</body>
</html>