TensorFlow釋出面向JavaScript開發者的機器學習框架TensorFlow.js

機器之心發表於2019-03-03
當時時間 3 月 30 日,谷歌 TenosrFlow 開發者峰會 2018 在美國加州石景山開幕,來自全球的機器學習使用者圍繞 TensorFlow 展開技術演講與演示。去年的 TensorFlow 開發者大會上,該框架正式升級到了 1.0 版本,逐漸成為最流行的深度學習框架。今年,TensorFlow 釋出了面向 JavaScript 開發者的全新機器學習框架 TensorFlow.js。

在大會上午的 Keynote 中,谷歌大腦負責人 Jeff Dean、TensorFlow 總監 Rajat Monga 等人圍繞 TensorFlow 做了表現、流行度等方面的介紹。

據介紹,在過去的兩年中,TensorFlow 不斷更新,不斷改善,逐漸成為社群內最為流行的深度學習框架。下圖是從開源以來,TensorFlow 的重大更新,例如 TensorBoard、tfdata、tfkeras、Eager Execution 等。

TensorFlow釋出面向JavaScript開發者的機器學習框架TensorFlow.js

而且據統計,兩年內,TensorFlow 已經有一千一百萬下載,超過三萬的 commits,6900 以上的 pull requests,1400 多位 contributors。

今年,圍繞 TensorFlow,谷歌同樣做出了幾項重大宣佈:

1. 釋出新的 TensorFlow 官方部落格(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釋出面向JavaScript開發者的機器學習框架TensorFlow.js

在 TensorFlow.js 中,我們可以使用最底層的 JavaScript 線性代數庫或最高階的 API 在瀏覽器上開發模型,也能基於瀏覽器執行已訓練的模型。因此,它可以充分利用瀏覽器和計算機的計算資源實現非常多機器學習應用。例如在網頁端訓練一個模型來識別圖片或語音,訓練一個模型以新穎的方式玩遊戲或構建一個能創造鋼琴音樂的神經網路等。這些新穎的模型作為案例在 TensorFlow.js 中都提供了實現程式碼,讀者也可以跟隨教程實現基於瀏覽器的模型。

TensorFlow.js 專案主頁: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 Tensor
const shape = [2, 3]; // 2 rows, 3 columns
const 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 biases
biases.print(); // output: [0, 0, 0, 0, 0]

const updatedValues = tf.tensor1d([0, 1, 0, 1, 0]);
biases.assign(updatedValues); // update values of biases
biases.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 庫真正能訓練並部署機器學習模型,因此機器之心也將持續關注並嘗試構建有意思的應用。


相關文章