Quantcast
Channel: CAVEDU教育團隊技術部落格
Viewing all articles
Browse latest Browse all 673

[ 翻譯 ] 2018.09.21- 認識TensorFlow.js,在瀏覽器就能用JavaScript 訓練機器學習模型喔!

$
0
0
原文作者

Josh Gordon  Sara Robinson

圖片

Josh Gordon、Sara Robinson提供

翻譯 宗諭 審閱 阿吉老師
說明

感謝兩位作者的授權翻譯,特此致謝!原文請見這裡

We’re excited to introduce TensorFlow.js, an open-source library you can use to define, train, and run machine learning models entirely in the browser, using Javascript and a high-level layers API. If you’re a Javascript developer who’s new to ML, TensorFlow.js is a great way to begin learning. Or, if you’re a ML developer who’s new to Javascript, read on to learn more about new opportunities for in-browser ML. In this post, we’ll give you a quick overview of TensorFlow.js, and getting started resources you can use to try it out.

 

我們很興奮能向大家介紹TensorFlow.js。TensorFlow.js是一個完全開放原始碼的函式庫,只要運用Javascript與其高階API,就能在瀏覽器中直接定義、訓練並運行機器學習模型。若您本身是一位Javascript開發者,但對機器學習不是那麼熟悉,那TensorFlow.js是一個很好的學習方式。又或者,您是一位對Javascript不太熟的機器學習開發者,請繼續閱讀本文以幫助您深入了解更多關於瀏覽器內機器學習的新契機。在這篇文章中,我們將快速介紹TensorFlow.js,還有您可用來嘗試TensorFlow.js的快速上手資源。

 

In-Browser ML

Running machine learning programs entirely client-side in the browser unlocks new opportunities, like interactive ML! If you’re watching the livestream for the TensorFlow Developer Summit, during the TensorFlow.js talk you’ll find a demo where @dsmilkov and @nsthorat train a model to control a PAC-MAN game using computer vision and a webcam, entirely in the browser. You can try it out yourself, too, with the link below — and find the source in the examples folder.

 

瀏覽器內的機器學習

在瀏覽器客戶端就能直接執行機器學習程式能帶出許多新契機,例如互動式的機器學習!您可以看看TensorFlow開發者大會直播,在關於TensorFlow.js的分享中有一段是@dsmilkov@nsthorat 正demo一個完全跑在瀏覽器中的範例,運用電腦視覺和網路攝影機訓練一個模型以控制小精靈遊戲。您也可以使用下面的連結親自試一下,並在examples資料夾中找到原始程式碼。

 

If you’d like to try another game, give the Emoji Scavenger Hunt a whirl — this time, from a browser on your mobile phone.

 

如果想試試看其他遊戲,可以試試看Emoji Scavenger Hunt ,但這次請使用您手機上的瀏覽器嘗試。

圖1

 

ML running in the browser means that from a user’s perspective, there’s no need to install any libraries or drivers. Just open a webpage, and your program is ready to run. In addition, it’s ready to run with GPU acceleration. TensorFlow.js automatically supports WebGL, and will accelerate your code behind the scenes when a GPU is available. Users may also open your webpage from a mobile device, in which case your model can take advantage of sensor data, say from a gyroscope or accelerometer. Finally, all data stays on the client, making TensorFlow.js useful for low-latency inference, as well as for privacy preserving applications.

 

由使用者的角度來說,在瀏覽器中就能執行機器學習代表不用再安裝任何函式庫或驅動程式了。只要打開網頁就可以執行程式了。此外,它還可支援GPU加速。TensorFlow.js會自動支援WebGL,當GPU可用的時候,它會在背景中加速您的程式。使用者也可透過行動裝置開啟網頁,這樣一來您的模型就能取用手機的感測器資料,例如陀螺儀或是加速度計。最後,所有資料皆保留在客戶端上,使TensorFlow.js既適用於低延遲推論,也適用於需要保護的應用程式。

 

What can you do with TensorFlow.js?

If you’re developing with TensorFlow.js, here are three workflows you can consider.

  • You can import an existing, pre-trained model for inference. If you have an existing TensorFlow or Kerasmodel you’ve previously trained offline, you can convert into TensorFlow.js format, and load it into the browser for inference.

 

  • You can re-train an imported model. As in the Pac-Man demo above, you can use transfer learning to augment an existing model trained offline using a small amount of data collected in the browser using a technique called Image Retraining. This is one way to train an accurate model quickly, using only a small amount of data.

 

  • Author models directly in browser. You can also use TensorFlow.js to define, train, and run models entirely in the browser using Javascript and a high-level layers API. If you’re familiar with Keras, the high-level layers API should feel familiar.

 

我們可以運用TensorFlow.js做些什麼?

若您正使用TensorFlow.js進行開發,以下三個工作流程是您可考慮的:

  • 您可匯入現有、預先訓練好的模型進行推論。若您有一個現有的TensorFlow或Keras模型,您可將其轉換為js的檔案格式,並將其載入至瀏覽器中進行推論。

 

  • 您可以重新訓練一個已匯入的模型。在前面提及的小精靈遊戲範例中,您可使用遷移式學習,透過使用一種叫做「影像再訓練(Image Retraining)」的技術,運用在瀏覽器中收集到的少量數據,做到在離線狀態下也能訓練現有模型。這是一種只需少量資料也能快速訓練精確模型的方法。

 

  • 直接在瀏覽器中建立模型。您也可以用TensorFlow.js,運用Javascript和高階API,直接在瀏覽器內定義、訓練並運作模型。若您熟悉Keras,那麼這些高層API對您來說應該不陌生。

 

Let’s see some code

If you like, you can head directly to the samples or tutorials to get started. These show how-to export a model defined in Python for inference in the browser, as well as how to define and train models entirely in Javascript. As a quick preview, here’s a snippet of code that defines a neural network to classify flowers, much like on the getting started guide on TensorFlow.org. Here, we’ll define a model using a stack of layers.

 

一起來看看程式碼

若您願意,您可直接使用範例教程開始學習。它們展示了如何匯出用Python定義的模型並在瀏覽器中進行推論,還有如何單單使用Javascript定義並訓練模型。很快提一下,以下的程式碼,它定義了一個可以分類花朵的神經網絡,很類似TensorFlow.org上的入門教學。在此用到了多個層來定義模型。

import * as tf from ‘@tensorflow/tfjs’;
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [4], units: 100}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: ‘categoricalCrossentropy’, optimizer: ‘sgd’});

 

The layers API we’re using here supports all of the Keras layers found in the examples directory (including Dense, CNN, LSTM, and so on). We can then train our model using the same Keras-compatible API with a method call:

 

這個layers API支援範例目錄中所有的Keras層(包括Dense、CNN、LSTM等等)。接著,只要呼叫對應的方法就可以使用相容於Keras的API訓練模型。以下是這個方法:

await model.fit(
  xData, yData, {
    batchSize: batchSize,
    epochs: epochs
});

 

The model is now ready to use to make predictions:

這個模型已經可以進行預測了:

// Get measurements for a new flower to generate a prediction
// The first argument is the data, and the second is the shape.
const inputData = tf.tensor2d([[4.8, 3.0, 1.4, 0.1]], [1, 4]);

// Get the highest confidence prediction from our model
const result = model.predict(inputData);
const winner = irisClasses[result.argMax().dataSync()[0]];

// Display the winner
console.log(winner);

 

TensorFlow.js also includes a low-level API (previously deeplearn.js) and support for Eager execution. You can learn more about these by watching the talk at the TensorFlow Developer Summit.

 

TensorFlow.js也包括一個低階的API(前身是deeplearn.js),並且支援Eager execution。請參考TensorFlow開發者大會的內容,您就能更深入了解。

圖2 TensorFlow.js API的概述

 

How does TensorFlow.js relate to deeplearn.js?

Good question! TensorFlow.js, an ecosystem of JavaScript tools for machine learning, is the successor to deeplearn.js which is now called TensorFlow.js Core. TensorFlow.js also includes a Layers API, which is a higher level library for building machine learning models that uses Core, as well as tools for automatically porting TensorFlow SavedModels and Keras hdf5 models. For answers to more questions like this, check out the FAQ.

 

TensorFlow.js與deeplearn.js的關聯是什麼?

這是一個好問題!TensorFlow.js是一個為了機器學習而誕生的JavaScript工具生態系,是deeplearn.js的繼承者,而deeplearn.js現在則被稱為TensorFlow.js Core。TensorFlow.js還包括一個Layers API,這是一個更高階的函式庫,能運用Core來建置機器學習模型,它也是一款自動移植TensorFlow SavedModels與Keras hdf5模型的工具。更多類似問題的答案,請參閱FAQ

 

Where’s the best place to learn more?

To learn more about TensorFlow.js, visit the project homepage, check out the tutorials, and try the examples. You can also watch the talk from the 2018 TensorFlow Developer Summit, and follow TensorFlow on Twitter.

 

哪裡最適合學習TensorFlow.js呢?

想更深入了解TensorFlow.js,請到本專案的首頁查看教學,玩玩看各個範例。您還可觀看2018年TensorFlow開發者大會的演講,並在Twitter上關注TensorFlow

 

Thanks for reading, and we’re excited to see what you’ll create with TensorFlow.js! If you like, you can follow @dsmilkov@nsthorat, and @sqcaifrom the TensorFlow.js team on Twitter for updates.

 

謝謝您耐心讀完本文,我們很期待看到您運用TensorFlow.js做出的成果!喜歡的話,可以在Twitter上關注TensorFlow.js團隊的@dsmilkov@nsthorat@ sqcai 等人的帳號來獲得最新消息。

 

備註:若您想購買AI人工智慧相關產品,請洽機器人王國商城,謝謝。

 

相關文章


Viewing all articles
Browse latest Browse all 673

Trending Articles