TensorFlow.js

Với TensorFlow.js, không những có thể chạy models machine learning, mà chúng ta còn có thể training trực tiếp ngay trên browser. Trong bài viết ngắn gọn này, mình sẽ giới thiệu cách sử dụng cơ bản và nhanh nhất để bắt đầu với Tensorflow.js.

Bây giờ bắt đầu với trang HTML cơ bản nhất mọi thời đại:

<html>
  <head></head>
  <body></body>
</html>

Sau đó, đầu tiên là cần load tensorflow để bắt đầu sử dụng các APIs. Mình sử dụng CDN:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <!-- Get latest version at https://github.com/tensorflow/tfjs -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
  </head>
</html>

Phiên bản mới nhất tại thời điểm này là 0.12.3, bạn nên checkout version mới nhất tại Github.

Bài toán hồi quy đơn giản

Bây giờ đã load được Tensorflow.js, mình thử một bài toán đơn giản.
Giả sử chúng ta có phương trình

f(x): y = 2x - 1

Với phương trình trên, đi qua được các điểm sau:

(-1, -3), (0, -1), (1, 1), (2, 3), (3, 5), (4, 7)

Như hàm f(x) trên ta đã biết, cho một giá trị x ta có được giá trị y tương ứng. Bài tập là training một model, không biết trước phương trình f(x), từ các tập giá trị x suy ra y, ta có thể tìm ra được phương trình đó hay không.

Bắt đầu, mình sẽ tạo 1 neural network đơn giản, chỉ có 1 input và 1 output, vậy ở đây mình có 1 single node. Trên Javascript, mình có thể tạo một tf.sequential và add các định nghĩa của layer vào, cực kỳ đơn giản:

const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));

Sau đó, mình compile model, định nghĩa loss type và hàm optimizer. Loss type ở đây mình sử dụng meanSquaredError cho đơn giản, optimizer là stochastic gradient descent:

model.compile({
  loss: 'meanSquaredError',
  optimizer: 'sgd',
});

Để train model, mình định nghĩa 1 tensor X cho giá trị đầu vào và 1 tensor khác cho giá trị Y:

const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);

Để train model mình sử dụng fit(), đưa tập XsYs, set số epochs (số lần lăp qua toàn bộ data)

await model.fit(xs, ys, { epochs: 500 });

Done, sau khi train có được model, model này được dùng để đoán 1 giá trị Y cho giá trị X mới, ví dụ X = 10. Sử dụng predict() và in giá trị này vào 1 thẻ <div>

document.getElementById('output_field').innerText = model.predict(
  tf.tensor2d([10], [1, 1]),
);

Kết quả hiển thị trên màn hình:

Ảnh: https://medium.com/tensorflow/getting-started-with-tensorflow-js-50f6783489b2

Sau đây là source code đầy đủ của toàn bộ chương trình trên:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>

    <!-- Place your code in the script tag below. You can also use an external .js file -->
    <script>
      // Notice there is no 'import' statement. 'tf' is available on the index-page
      // because of the script tag above.

      // 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' });

      // Generate some synthetic data for training.
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // Train the model using the data.
      model.fit(xs, ys, { epochs: 10 }).then(() => {
        // Use the model to do inference on a data point the model hasn't seen before:
        // Open the browser devtools to see the output
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>

  <body></body>
</html>

Cuối cùng mình đã giới thiệu qua cách train một model rất đơn giản với Tensorflow.js, từ đây bạn đã có thể xây dựng ứng dụng deep learning ngay trên trình duyệt rồi.

References

Have fun, hãy để lại bình luận nếu có bất cứ thắc mắc gì nhé :D