1
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 2018

Day 1

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()

#成果物
http://jsdo.it/ohisama1/U5Sn

以上。

1
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
1
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?