在雲端平台部署 tfjs-node 專案

本文件說明如何在雲端平台上使用 @tensorflow/tfjs-node 套件執行 Node.js 處理程序。

從 tfjs-node@1.2.4 開始,在雲端平台上執行 Node.js 專案不需要額外設定。本指南將說明如何在 Heroku 和 GCloud 上執行 @tensorflow/tfjs-examples 儲存庫中的 mnist-node 範例。Heroku 的 Node.js 支援記錄在本文章中。關於在 Google Cloud Platform 上執行 Node.js 的說明文件請見此處

在 Heroku 上部署 Node.js 專案

先決條件

  1. 已安裝 Node.js 和 npm
  2. Heroku 帳戶
  3. Heroku CLI

建立 Node.js 應用程式

  1. 建立資料夾並從 mnist-node 範例複製 data.jsmain.jsmodel.jspackage.json 檔案。
  2. 確認 @tensorflow/tfjs-node 相依性為 @1.2.4 或更新版本。

建置您的應用程式並在本機執行

  1. 在本機目錄中執行 npm install 命令,以安裝在 package.json 檔案中宣告的相依性。您應該能夠看到已安裝 tfjs-node 套件,並且已下載 libtensorflow。
$ npm install
> @tensorflow/tfjs-node@1.2.5 install mnist-node/node_modules/@tensorflow/tfjs-node
> node scripts/install.js

CPU-linux-1.2.5.tar.gz
* Downloading libtensorflow
[==============================] 22675984/bps 100% 0.0s
* Building TensorFlow Node.js bindings
  1. 透過執行 npm start 在本機訓練模型。
$ npm start
> tfjs-examples-mnist-node@0.1.0 start /mnist-node
> node main.js

2019-07-30 17:33:34.109195: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-07-30 17:33:34.147880: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3492175000 Hz
2019-07-30 17:33:34.149030: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x52f7090 executing computations on platform Host. Devices:
2019-07-30 17:33:34.149057: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>

Total params: 594922
Trainable params: 594922
Non-trainable params: 0
_________________________________________________________________
Epoch 1 / 20
Epoch 1 / 20
========================>----------------------------------------------------------------------------------: 35.5
  1. 請務必在 .gitignore 檔案中忽略建置成品,例如 node_modules。

建立並部署 Heroku 應用程式

  1. 在 Heroku 網站上建立新的應用程式
  2. 提交您的變更並推送至 heroku master
$ git init
$ heroku git:remote -a your-app-name
$ git add .
$ git commit -m "First Commit"
$ git push heroku master
  1. 在建置記錄中,您應該能夠看到 tfjs-node 套件下載 TensorFlow C 程式庫並載入 TensorFlow Node.js 原生附加元件
remote: -----> Installing dependencies
remote:        Installing node modules (package.json)
remote:
remote:        > @tensorflow/tfjs-node@1.2.5 install /tmp/build_de800e169948787d84bcc2b9ccab23f0/node_modules/@tensorflow/tfjs-node
remote:        > node scripts/install.js
remote:
remote:        CPU-linux-1.2.5.tar.gz
remote:        * Downloading libtensorflow
remote:
remote:        * Building TensorFlow Node.js bindings
remote:        added 92 packages from 91 contributors and audited 171 packages in 9.983s
remote:        found 0 vulnerabilities
remote:

在 Heroku 上的處理程序記錄中,您應該能夠看到模型訓練記錄

Total params: 594922
Trainable params: 594922
Non-trainable params: 0
_________________________________________________________________
Epoch 1 / 20
Epoch 1 / 20
====>--------------------------------------------------------------------: 221.9

您也可以在 Heroku 主控台中啟動或偵錯處理程序。

使用 1.2.4 之前的 tfjs-node 版本

如果您使用的是 1.2.4 之前的 tfjs-node 套件版本,則該套件需要 g++ 從原始碼檔案編譯節點原生附加元件。您必須確認您的堆疊具有 Linux build-essential 套件 (較新版本的堆疊預設可能沒有)。

在 Google Cloud Platform 上部署 Node.js 專案

先決條件

  1. 擁有有效的 Google Cloud 專案及帳單帳戶
  2. 安裝 Google Cloud 用戶端工具
  3. 新增 app.yaml 檔案以設定 Node.js Runtime

將應用程式部署至 GCloud

執行 gcloud app deploy 以將本機程式碼和設定部署到 App Engine。在部署記錄中,您應該能夠看到已安裝 tfjs-node

$ gcloud app deploy

Step #1:
Step #1: > @tensorflow/tfjs-node@1.2.5 install /app/node_modules/@tensorflow/tfjs-node
Step #1: > node scripts/install.js
Step #1:
Step #1: CPU-linux-1.2.5.tar.gz
Step #1: * Downloading libtensorflow
Step #1:
Step #1: * Building TensorFlow Node.js bindings
Step #1: added 88 packages from 85 contributors and audited 171 packages in 13.392s
Step #1: found 0 vulnerabilities

在應用程式記錄中,您應該能夠看到模型訓練處理程序

Total params: 594922
Trainable params: 594922
Non-trainable params: 0

Epoch 1 / 20
===============================================================================>
745950ms 14626us/step - acc=0.920 loss=0.247 val_acc=0.987 val_loss=0.0445
Loss: 0.247 (train), 0.044 (val); Accuracy: 0.920 (train), 0.987 (val) (14.62 ms/step)
Epoch 2 / 20
===============================================================================>
818140ms 16042us/step - acc=0.980 loss=0.0655 val_acc=0.989 val_loss=0.0371
Loss: 0.066 (train), 0.037 (val); Accuracy: 0.980 (train), 0.989 (val) (16.04 ms/step)
Epoch 3 / 20
Epoch 3 / 20