LoginSignup
0
0

More than 3 years have passed since last update.

plunkerでtensorflow.js その18

Last updated at Posted at 2019-11-05

概要

plunkerでtensorflow.jsやってみた。
lstm使ってみた。
xor、学習させてみた。

サンプルコード


tf.setBackend('cpu');
const model = tf.sequential();
model.add(tf.layers.lstm({
    units: 8,
    returnSequences: false,
    inputShape: [2, 1]
}));
model.add(tf.layers.dense({
    units: 1,
    activation: 'sigmoid'
}));
model.compile({
    optimizer: 'adam',
    loss: 'binaryCrossentropy'
});
const xs = tf.tensor3d([[[1], [0]], [[0], [1]], [[1], [1]], [[0], [0]]]);
const ys = tf.tensor2d([[1], [1], [0], [0]]);
model.fit(xs, ys, {
    batchSize: 4, 
    epochs: 1000
}).then((d) => {
    var str = "loss = ";
    str += d.history.loss[0]; 
    str += "<br>1, 1 = ";
    var pre0 = model.predict(tf.tensor3d([[[1], [1]]]));
    str += pre0.dataSync() + "<br>0, 0 = ";
    var pre1 = model.predict(tf.tensor3d([[[0], [0]]]));
    str += pre1.dataSync() + "<br>0, 1 = ";
    var pre2 = model.predict(tf.tensor3d([[[0], [1]]]));
    str += pre2.dataSync() + "<br>1, 0 = ";
    var pre3 = model.predict(tf.tensor3d([[[1], [0]]]));
    str += pre3.dataSync() + "<br>";
    document.write(str);
});

成果物

比較用

tanh
https://embed.plnkr.co/xTaxEW41fcrjvWcxKDxL/

以上。

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