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

當時時間 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 已經有一千一百萬下載,超過三萬的 commits,6900 以上的 pull requests,1400 多位 contributors。bash

今年,圍繞 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.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 庫真正能訓練並部署機器學習模型,所以機器之心也將持續關注並嘗試構建有意思的應用。

相關文章
相關標籤/搜索