LoginSignup
0

More than 5 years have passed since last update.

jsdoでtensorflow.js

Last updated at Posted at 2018-04-03

概要

jsdoでtensorflow.jsやってみた。

サンプルコード

const model1 = tf.sequential();
model1.add(tf.layers.dense({
    units: 1, 
    inputShape: [1]
}));
model1.compile({
    loss: 'meanSquaredError', 
    optimizer: 'sgd'
});
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
model1.fit(xs, ys).then(() => {
    model1.predict(tf.tensor2d([5], [1, 1])).print();
});

function predict(input) {
  return tf.tidy(() => {
    const x = tf.scalar(input);
    const ax2 = a.mul(x.square());
    const bx = b.mul(x);
    const y = ax2.add(bx).add(c);
    return y;
  });
}
const a = tf.scalar(2);
const b = tf.scalar(4);
const c = tf.scalar(8);
const result = predict(2);
result.print()

成果物

以上。

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