Tutorial Singkat Tentang Tensorflow.js

in #machinelearning6 years ago (edited)

Tensorflow.js adalah sebuah library yang dibangun diatas deeplearn.js untuk membuat modul deep learning langsung dari web browser! Deep learning sendiri adalah sebuah cabang dari machine learning dan juga artificial intelligence. Dengan Tensorflow.js kita dapat membuat implementasi Convolutional Neural Network (CNN), Recurrent Neural Network (RNN) dan lain sebagainya.

Tensorflow sendiri merupakan library yang ditulis dengan bahasa C++ dan biasanya digunakan dengan bahasa pemrograman Python. Dengan adanya tensorflow.js, kita sekarang sudah bisa menggunakan beberapa fitur tensorflow di sisi web browser tanpa harus dibebani oleh instalasi yang cukup 'menantang'. Dengan Tensorflow.js kita tinggal melakukan instalasi dengan npm install @tensorflow/tfjs ataupun juga dengan menggunakan CDN.

import * as tf from "@tensorflow/tfjs";

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });

// Provide some housing data
const xs = tf.tensor1d([
  7.9, 8.1, 8.3, 8.5, 8.6, 8.4
]);
const ys = tf.tensor1d([
  738967, 742371, 750984, 759598, 763905, 755291
]);

// Train the model using the data provided
model.fit(xs, ys).then(() => {
  const form = document.getElementById("myform");
  const inputText = document.getElementById("inputText");
  const predictPlaceholder = document.getElementById("predict");

  form.addEventListener("submit", e => {
    e.preventDefault();
    // Use the model to predict or to inference
    const output = model.predict(
      tf.tensor2d(
        [parseFloat(inputText.value) / 10], [1, 1]
      ));
    predictPlaceholder.innerHTML = formatting(Array.from(output.dataSync())[0]);
  });
});

const formatting = num => {
  num *= 1000;
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
};

Untuk mengetahui lebih lanjut silakan simak video berikut ini.

Repo: GitHub

Referensi

Artikel ini dirangkum dari episode ke-18 randomscreencast.com.

Sort:  

Congratulations @rizafahmi! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @rizafahmi! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @rizafahmi! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!