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 20

plunkerでtensorflow.js その2

Posted at

#概要

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

#サンプルコード

const xs = tf.tensor2d([[1, 0], [0, 1], [1, 1], [0, 0]], [4, 2]);
const ys = tf.tensor2d([[1], [1], [0], [0]], [4, 1]);
const input = tf.input({
  shape: [2]
});
const layer1 = tf.layers.dense({
  units: 8, 
  activation: 'tanh'
});
const layer2 = tf.layers.dense({
  units: 1, 
  activation: 'sigmoid'
});
const output = layer2.apply(layer1.apply(input));
const model = tf.model({
  inputs: input, 
  outputs: output
});
const opti = tf.train.adam(0.01);
model.compile({
  optimizer: opti,
  loss: 'meanSquaredError'
});  
model.fit(xs, ys, {
  batchSize: 4,
  epochs: 1000,
}).then((d) => {
  var str = "loss = ";
  str += d.history.loss[0]; 
  str += "<br><br>1, 1 = ";
  var pre0 = model.predict(tf.tensor2d([1, 1], [1, 2]));
  str += pre0.dataSync() + "<br>0, 0 = ";
  var pre1 = model.predict(tf.tensor2d([0, 0], [1, 2]));
  str += pre1.dataSync() + "<br>0, 1 = ";
  var pre2 = model.predict(tf.tensor2d([0, 1], [1, 2]));
  str += pre2.dataSync() + "<br>1, 0 = ";
  var pre3 = model.predict(tf.tensor2d([1, 0], [1, 2]));
  str += pre3.dataSync() + "<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?