jsdoでtensorflow.js その10
概要
jsdoでtensorflow.jsやってみた。
keras風、高級APIを使わないで、coreとか言われるレベルでやってみた。
xor問題、やってみた。
写真
サンプルコード
const xt = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]);
const yt = tf.tensor2d([[1, 0], [1, 0], [0, 1], [0, 1]], [4, 2]);
const w1 = tf.variable(tf.randomNormal([2, 6]));
const b1 = tf.variable(tf.zeros([6]));
const w2 = tf.variable(tf.randomNormal([6, 2]));
const b2 = tf.variable(tf.zeros([2]));
function func(x) {
const h = tf.relu(x.matMul(w1).add(b1));
return tf.softmax(h.matMul(w2).add(b2));
}
function loss(pred, label) {
return tf.losses.softmaxCrossEntropy(pred, label).mean();
}
const optimizer = tf.train.sgd(0.1);
for (let i = 0; i < 3000; i++)
{
const cost = optimizer.minimize(() => loss(func(xt), yt), true);
}
alert(func(xt));
成果物
const xt = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]);
const yt = tf.tensor2d([[1, 0], [1, 0], [0, 1], [0, 1]], [4, 2]);
const w1 = tf.variable(tf.randomNormal([2, 6]));
const b1 = tf.variable(tf.zeros([6]));
const w2 = tf.variable(tf.randomNormal([6, 2]));
const b2 = tf.variable(tf.zeros([2]));
function func(x) {
const h = tf.relu(x.matMul(w1).add(b1));
return tf.softmax(h.matMul(w2).add(b2));
}
function loss(pred, label) {
return tf.losses.softmaxCrossEntropy(pred, label).mean();
}
const optimizer = tf.train.sgd(0.1);
for (let i = 0; i < 3000; i++)
{
const cost = optimizer.minimize(() => loss(func(xt), yt), true);
}
alert(func(xt));
以上。
Author And Source
この問題について(jsdoでtensorflow.js その10), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/4e0323668643eb06ee05著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .