0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tensorflow.jsAdvent Calendar 2019

Day 19

plunkerでtensorflow.js その3

Posted at

#概要

plunkerでtensorflow.jsやってみた。
xorやってみた。
tf.variableで書いてみた。

#サンプルコード

const xt = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]);
const yt = tf.tensor2d([[1], [1], [0], [0]], [4, 1]);
function func(x) {
	const h = tf.tanh(x.matMul(w1).add(b1));
  return tf.sigmoid(h.matMul(w2).add(b2));
}
function loss(pred, label) {
	return tf.losses.meanSquaredError(pred, label).mean();
}
const w1 = tf.variable(tf.randomNormal([2, 8]));
const b1 = tf.variable(tf.zeros([8]));
const w2 = tf.variable(tf.randomNormal([8, 1]));
const b2 = tf.variable(tf.zeros([1]));
const opti = tf.train.adam(0.01);
let cost = 0;
for (let i = 0; i < 1000; i++)
{
	cost = opti.minimize(() => loss(func(xt), yt), true);
}
let str = "loss = ";
str += cost; 
str += "<br><br>1, 0 = ";
let pre = func(xt);
pre = pre.dataSync();
str += pre[0] + "<br>0, 1 = ";
str += pre[1]+ "<br>1, 1 = ";
str += pre[2] + "<br>0, 0 = ";
str += pre[3] + "<br>";
document.write(str);

#成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?