在大會上午的 Keynote 中,谷歌大腦負責人 Jeff Dean、TensorFlow 總監 Rajat Monga 等人圍繞 TensorFlow 做了表現、流行度等方面的介紹。
據介紹,在過去的兩年中,TensorFlow 不斷更新,不斷改善,逐漸成爲社區內最爲流行的深度學習框架。下圖是從開源以來,TensorFlow 的重大更新,例如 TensorBoard、tfdata、tfkeras、Eager Execution 等。

而且據統計,兩年內,TensorFlow 已經有一千一百萬下載,超過三萬的 commits,6900 以上的 pull requests,1400 多位 contributors。
今年,圍繞 TensorFlow,谷歌同樣做出了幾項重大宣佈:
1. 發佈新的 TensorFlow 官方博客(http://blog.tensorflow.org/)與 TensorFlow YouTube 頻道;
2. 面向 JavaScript 開發者的全新機器學習框架 TensorFlow.js;
3. 發佈一系列新的庫與工具:例如 TensorFlowHub、TensorFlow Probability API、Nucleus、DeepVariant 等。
在今天的幾項重大宣佈中,比較有趣的是面向 JavaScript 開發者的全新機器學習框架 TensorFlow.js。在下文中,機器之心對 TensorFlow.js 做了細緻介紹:
在大會的 Keynote 中,TensorFlow 團隊表示基於網頁的 JavaScript 庫 TensorFlow.js 現在已經能訓練並部署機器學習模型。我們可以使用神經網絡的層級 API 構建模型,並在瀏覽器中使用 WebGL 創建複雜的數據可視化應用。此外 Node.js 很快就會發布,它能爲網站模型提供 GPU、TPU 等快速訓練與推斷的方法。

在 TensorFlow.js 中,我們可以使用最底層的 JavaScript 線性代數庫或最高級的 API 在瀏覽器上開發模型,也能基於瀏覽器運行已訓練的模型。因此,它可以充分利用瀏覽器和計算機的計算資源實現非常多機器學習應用。例如在網頁端訓練一個模型來識別圖片或語音,訓練一個模型以新穎的方式玩遊戲或構建一個能創造鋼琴音樂的神經網絡等。這些新穎的模型作爲案例在 TensorFlow.js 中都提供了實現代碼,讀者也可以跟隨教程實現基於瀏覽器的模型。
TensorFlow.js 項目主頁:https://js.tensorflow.org/
TensorFlow.js 的核心概念
TensorFlow.js 是一個開源的用於開發機器學習項目的 WebGL-accelerated JavaScript 庫。TensorFlow.js 可以爲你提供高性能的、易於使用的機器學習構建模塊,允許你在瀏覽器上訓練模型,或以推斷模式運行預訓練的模型。TensorFlow.js 不僅可以提供低級的機器學習構建模塊,還可以提供高級的類似 Keras 的 API 來構建神經網絡。
TensorFlow.js 的安裝非常簡單,我們可以直接使用 NMP 或腳本完成構建。它的使用也有非常多的文檔與教程,我們只需要掌握一些基本的核心概念就能快速入手這一 JS 庫。接下來,我們介紹這個庫的一些核心概念。
Tensor
TensorFlow.js 中的中心數據單元是張量(tensor):一維或多維數組。一個 Tensor 實例的 shape 屬性定義了其數組形狀(即,數組的每個維度上有多少個值)。
Tensor 主要構造函數是 tf.tensor 函數:
// 2x3 Tensorconst shape = [2, 3]; // 2 rows, 3 columnsconst a = tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], shape);a.print(); // print Tensor values// Output: [[1 , 2 , 3 ],// [10, 20, 30]]// The shape can also be inferred:const b = tf.tensor([[1.0, 2.0, 3.0], [10.0, 20.0, 30.0]]);b.print();// Output: [[1 , 2 , 3 ],// [10, 20, 30]]
Variable
Variable 使用一個張量值來初始化。然而,和 Tensor 不一樣,它們的值是可變的。你可以用 assign 方法分配一個新的張量到一個已有的變量(variable):
const initialValues = tf.zeros([5]);const biases = tf.variable(initialValues); // initialize biasesbiases.print(); // output: [0, 0, 0, 0, 0]const updatedValues = tf.tensor1d([0, 1, 0, 1, 0]);biases.assign(updatedValues); // update values of biasesbiases.print(); // output: [0, 1, 0, 1, 0]
Variable 主要用於在模型訓練過程中保存和更新值。
Operations (Ops)
Tensor 可以用於保存數據,而 Operation(Op)則可用於操作數據。TensorFlow.js 提供了多種適用於張量的線性代數和機器學習運算的 Op。由於 Tensor 是不可改變的,這些 Op 不會改變它們的值,而會返回新的 Tensor。這些運算不僅包含 add、sub 和 mul 等二元運算,同時還包括 square 等一元運算:
const e = tf.tensor2d([[1.0, 2.0], [3.0, 4.0]]);const f = tf.tensor2d([[5.0, 6.0], [7.0, 8.0]]);const e_plus_f = e.add(f);e_plus_f.print();// Output: [[6 , 8 ],// [10, 12]]const d = tf.tensor2d([[1.0, 2.0], [3.0, 4.0]]);const d_squared = d.square();d_squared.print();// Output: [[1, 4 ],// [9, 16]]
模型和層
從概念上說,一個模型就是一個函數,給定輸入之後生成所需要的輸出。
在 Tensorflow.js 有兩種創建模型的方式:直接使用 Op 表示模型的運算。或者使用高級 API tf.model 來構建以層定義的模型,這在深度學習中是很常用的抽象形式。其實除了以上的特徵,Tensorflow.js 還有一些很重要的核心概念,例如內存管理、神經網絡基本運算和訓練過程等。但我們瞭解以上概念就能輕鬆在瀏覽器中構建出簡單的機器學習模型,如下展示了簡單線性迴歸的定義方法:
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'}); // 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).then(() => { // Use the model to do inference on a data point the model hasn't seen before: model.predict(tf.tensor2d([5], [1, 1])).print(); });
目前該項目還是非常新穎的應用,我們非常容易將機器學習模型部署在網頁端並在用戶的瀏覽器與硬件實現簡單的推斷。雖然我們還不清楚實現的效果,但這個 JS 庫真正能訓練並部署機器學習模型,因此機器之心也將持續關注並嘗試構建有意思的應用。