Created
June 15, 2018 14:35
-
-
Save Neverik/6bc75d10731b6f5ea1a9718f2995863a to your computer and use it in GitHub Desktop.
A universal approximator that doesn't work properly yet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const xs = []; | |
const ys = [] | |
let poly = null; | |
const opt = tf.train.adam(0.001) | |
const iter = 1 | |
function polynomial (i, o, n) { | |
this.i = i | |
this.o = o | |
this.n = n | |
this.w = {} | |
this.b = {} | |
for (let p = -n; p < n; p++) { | |
let w = tf.variable(tf.zeros([i,o]), true) | |
let b = tf.variable(tf.zeros([o]), true) | |
this.w[p] = w | |
this.b[p] = b | |
} | |
this.predict = function (x) { | |
result = x | |
for (let p = -n; p < n; p++) { | |
power = tf.pow (x, tf.scalar (p)) | |
weight = this.w[p] | |
mix = tf.dot (power, weight).sub(this.b[p]) | |
result = tf.add (result, mix) | |
} | |
return result | |
} | |
} | |
function setup() { | |
createCanvas(400, 400); | |
stroke(255); | |
poly = new polynomial(1, 1, 4); | |
} | |
function mousePressed() { | |
xs.push([mouseX / width,]) | |
ys.push([mouseY / height,]) | |
} | |
function loss() { | |
x = tf.tensor(xs) | |
y = tf.tensor(ys) | |
const prediction = poly.predict(x) | |
return tf.sub(y, prediction).square().sum() | |
} | |
function draw() { | |
background(55); | |
strokeWeight(10); | |
for (let i = 0; i < xs.length; i++) { | |
let x = xs[i][0] | |
let y = ys[i][0] | |
point(x * width, y * height) | |
} | |
tf.tidy (() => { | |
strokeWeight(4) | |
let x = tf.linspace(0, 1, 4) | |
let y = poly.predict(x.as2D(4, 1)).dataSync() | |
for (let x in y) { | |
point(int(x) * width, y[x] * height) | |
} | |
if (xs.length > 0) { | |
for (let i = 0; i < iter; i++) { | |
opt.minimize(loss) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment