-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlin_reg_simulation.js
54 lines (47 loc) · 1.06 KB
/
lin_reg_simulation.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
let x = []
let y = []
let m,c
const learningRate = 0.1;
const optimizer = tf.train.adam(learningRate);
function setup(){
createCanvas(400,400)
m=tf.variable(tf.scalar(random(1)))
c=tf.variable(tf.scalar(random(1)))
}
function loss(y_pred,y){
return y_pred.sub(y).square().mean()
}
function predict(x){
const x_new = tf.tensor1d(x)
const y_pred = x_new.mul(m).add(c)
return y_pred
}
function mousePressed(){
x.push(map(mouseX,0,width,0,1))
y.push(map(mouseY,0,height,1,0))
}
function draw(){
tf.tidy(()=>{
if (x.length > 0) {
const ys = tf.tensor1d(y);
optimizer.minimize(() => loss(predict(x), ys));
}
})
background(0)
stroke(255)
strokeWeight(5)
for(let i=0;i<x.length;i++){
point(map(x[i],0,1,0,width) , map(y[i],0,1,height,0))
}
xl = [0,1]
yp = tf.tidy(()=>predict(xl))
let yl = yp.dataSync()
yp.dispose()
let x1 = map(xl[0], 0, 1, 0, width);
let x2 = map(xl[1], 0, 1, 0, width);
let y1 = map(yl[0], 0, 1, height, 0);
let y2 = map(yl[1], 0, 1, height, 0);
strokeWeight(2)
line(x1,x2,y1,y2)
console.log(tf.memory().numTensors)
}