在 Google Cloud Platform 上部署學習可解釋性工具 (LIT) 示範

1. 總覽

本研究室將提供快速部署 LIT 示範的操作說明。目標是協助您熟悉 LIT 工具,瞭解模型行為。您將進行情緒分析,並利用「反事實門檻」(Counterfactual LIT) 功能,找出特定字詞的重要性。示範內容包括用來執行分析的資料集。本研究室將提供相關步驟,協助您在 Google Cloud Platform 中部署 LIT、分析資料,以及刪除已部署的服務。

什麼是學習可解釋性工具 (LIT)?

🔥?LIT 是一款視覺化、互動式機器學習模型理解工具,支援文字、圖片和表格資料。它可以做為獨立伺服器執行,也可以在筆記本環境中執行,例如 Colab、Jupyter 和 Google Cloud Vertex AI 筆記本。

LIT 旨在回答以下問題:

  • 模型處理哪種樣本的成效不佳?
  • 為什麼模型做出這項預測?這項預測可歸功於惡意行為還是訓練集內的不受歡迎之前?
  • 如果變更文字風格、動詞時態或代名詞性別等,模型行為是否仍一致?

LIT GitHub 存放區提供這項工具。如需相關資訊,請前往 ArXiv

做法

您將使用 Google Cloud Shell 提取、標記、推送和部署容器映像檔。

您將使用 Google Artifact Registry 集中儲存構件及建構依附元件,在經過整合的 Google Cloud 體驗中的一部分。您會將 Docker 映像檔上傳至 Artifact Registry。詳情請參閱 Google Cloud Artifact Registry 說明文件。

您將使用 Google Cloud Run 這個代管的 Knative 服務來部署 Docker 映像檔。這個代管運算平台可讓您直接在 Google 的可擴充基礎架構上執行容器。

資料集

此示範使用 Stanford Sentiment Treebank 資料集。

「史丹佛 Sentiment Treebank 是第一個擁有完整標籤剖析樹的語料庫,能夠全面分析語言情緒的構成影響。這個語料庫是以 Pang 和 Lee (2005) 引入的資料集為基礎,包含從電影評論中擷取的 11,855 個單句。透過使用史丹佛剖析器 (Klein and Manning,2003 年) 進行剖析,並從這些解析的樹中納入共 215,154 個不重複的詞組,每個詞組都由 3 名人工審查員加註。」參考文件

事前準備

如要查看本參考指南,您需要建立 Google Cloud 專案。您可以建立新專案,或選取已建立的專案。

2. 啟動 Google Cloud 控制台和 Cloud Shell

您將啟動 Google Cloud 控制台,並在這個步驟中使用 Google Cloud Shell。

2-a:啟動 Google Cloud 控制台

啟動瀏覽器,然後前往 Google Cloud 控制台

Google Cloud 控制台是功能強大又安全的網路管理員介面,可協助您快速管理 Google Cloud 資源。這項開發運作工具可隨身帶著走。

2-b:啟動 Google Cloud Shell

啟動 Google Cloud Shell。請見下方圖片。

推出 Google Cloud 控制台

畫面應如下所示:

Google Cloud Console

您將在接下來的步驟中使用命令提示字元。

Cloud Shell 是線上開發與作業環境,可透過瀏覽器隨時隨地存取。Cloud Shell 的線上終端機已預先載入 gcloud 指令列工具、kubectl 等公用程式,可讓您管理資源。您也可以使用線上 Cloud Shell 編輯器來開發、建構及部署雲端式應用程式,並對應用程式進行偵錯。Cloud Shell 提供開發人員立即可用的線上環境,其中已預先安裝喜愛的工具組,以及 5 GB 的永久儲存空間。

2-c:設定 Google Cloud 專案

設定 Google Cloud 專案和您要建立 Google Cloud 服務的位置。您會使用這些資訊建立 Google Cloud Workbench 和 Artifact Registry。您將使用前者來建構並推送容器。您將使用後者儲存容器映像檔。

您必須設定唯一的必要 PROJECT_ID 變數。您可以修改其他變數,但預設值足以執行研究室。設定正確的 Google Cloud 專案。由 gcloud 指令使用。

# Set your GCP Project ID.
export PROJECT_ID=[Your project ID]

3. 將 Docker 映像檔部署至 Google Cloud Artifact Registry

3-a:建立 Google Cloud Artifact Registry

首先,您需要建立 Artifact Registry,以便儲存 Docker 映像檔。

# Set Google Cloud Location.
export GCP_LOCATION=us-central1
# Set image container artifact repo name.
export ARTIFACT_REPO=lit-demo
# Set lit demo name.
export DEMO_NAME=demo1

# Use below cmd to list all Google Cloud Artifact locations:
# gcloud artifacts locations list
# Create a repo to upload the docker container images.
gcloud artifacts repositories create $ARTIFACT_REPO \
    --repository-format=docker \
    --location=$GCP_LOCATION \
    --description="LIT Demos"

# Validate the repo creation.
gcloud artifacts repositories describe $ARTIFACT_REPO \
    --location=$GCP_LOCATION

3-b:提取 Docker 映像檔

接著使用下列指令,列出公開存放區中的所有 LIT Docker 映像檔。

# List all the public LIT docker images.
gcloud container images  list-tags  us-east4-docker.pkg.dev/lit-demos/lit-app/lit-app

您可以找到所有可用的映像檔及其標記 (參照映像檔版本),然後選擇要部署的標記。

Google Cloud Docker 映像檔

使用下列指令提取您選擇的 Docker 映像檔。

# Set your chosen tag.
export TAG=[Your Chosen Tag]

# Pull the chosen docker image.
docker pull us-east4-docker.pkg.dev/lit-demos/lit-app/lit-app:$TAG

3-c:標記 Docker 映像檔

第三,將您剛才提取的映像檔標記到目標存放區。

# Push the pulled docker image to target repository.
docker tag  us-east4-docker.pkg.dev/lit-demos/lit-app/lit-app:$TAG  $GCP_LOCATION-docker.pkg.dev/$PROJECT_ID/$ARTIFACT_REPO/$DEMO_NAME:$TAG

3-d:推送 Docker 映像檔

接著,將 Docker 映像檔推送至目標存放區。

# Push the pulled docker image to the target repository.
docker push  $GCP_LOCATION-docker.pkg.dev/$PROJECT_ID/$ARTIFACT_REPO/$DEMO_NAME:$TAG

3-e:將 Docker 映像檔部署至 CloudRun

將 Docker 映像檔部署至雲端,並透過目標存放區執行。

Google Cloud Deploy Cloud Run

在部署選項中,選取「Deploy one from an existing container image」(從現有的容器映像檔部署修訂版本)。

在 Config 中,將 Service 命名為 $DEMO_NAME,然後選取與 $GCP_LOCATION 相同的地區。

在「驗證」部分,您可以選擇「允許未經驗證的叫用」或「需要驗證」。如果您選取「需要驗證」您可能需要完成額外步驟才能存取示範內容因此,建議您選取「允許未經驗證的叫用」,只滿足這個需求。

Google Cloud Run 設定

在「容器設定」中,選取「容器通訊埠」為 5432、記憶體設為 32Gib,並將 CPU 設為 8

Google Cloud 容器設定

完成設定後,請建立執行個體。

4. 在 GCP 中查看 LIT 服務

建立服務後,您可以在 Google Cloud 控制台中觀看記錄檔。

導覽:Google Cloud 控制台的頂端列 → Cloud Run (在搜尋列中) → 選取示範 1 應用程式 → 選取記錄。您也可以檢查「指標」等。

在 Google Cloud Console 中使用「Search」(搜尋) 並輸入「Cloud Run」。請見下方圖片。

Google Cloud Console 搜尋

選取「demo1」儲存空間服務請參考下圖。

Google Cloud 控制台 Cloud Run 清單

您可以查看「LOGS」(記錄) 部分。在此期間,你可以找出參考檔案的網址。請參考下圖。Google Cloud 控制台 Cloud Run 記錄檔

您可以查看「指標」部分。請參考下圖。Google Cloud 控制台 Cloud Run 指標

5. 瀏覽 LIT 示範網址

如果您因「禁止存取」錯誤而無法存取網址,請變更服務,並允許在未經驗證的情況下叫用服務。

LIT 變更 Cloud Run 權限

您也可以使用以下指令,透過 Proxy 將服務傳送至本機主機。

# Proxy the service to local host.
gcloud run services proxy $DEMO_NAME --project $PROJECT_ID

確認該區域與 GCP_LOCATION 相同。接著,您就可以瀏覽本機主機網址。

LIT Proxy 服務

LIT 示範的螢幕截圖如以下螢幕截圖所示:

LIT 示範首頁

您將在 Stanford Sentiment Treebank 資料集查看情緒分析。操作步驟如下

  • 使用 LIT 資料表中的搜尋功能,找出包含「not」一詞的 56 個資料點。

LIT 示範搜尋

  • 請檢查指標表格中的 BERT 模型準確率。BERT 模型的準確率很高。

LIT 客層準確度

  • 選取個別資料點,即可查看說明。搜尋「depression」一詞。

LIT 客層準確度

  • 選取「這不是終極憂鬱症患者的黑幫電影」。請查看 Salience Map。顯著性地圖指出「不是」和「終極」對預測結果而言都很重要

LIT 客層準確度

許多 LIT 功能都建議採用。你可以觀看 YouTube 短片LIT ArXiv 的短片功能說明。

6. 恭喜

恭喜您完成本程式碼研究室!放鬆一下!

清除所用資源

如要清除研究室資源,請刪除針對研究室建立的所有 Google Cloud 服務。請使用 Google Cloud Shell 執行下列指令。

如果 Google Cloud Connection 因閒置而中斷,請重設變數。按照 2-c 和 4-1 的指示設定殼層變數並設定 Google Cloud 專案。

# Delete the Cloud Run Service.
gcloud run services delete $DEMO_NAME \
--region=$GCP_LOCATION

# Delete the Artifact Registry.
gcloud artifacts repositories delete $ARTIFACT_REPO\
	--location=$GCP_LOCATION

### **Further reading**

Continue learning the LIT tool features with the below materials:

* LIT open source code base: [Git repo](https://github.com/PAIR-code/lit)
* LIT paper: [ArXiv](https://arxiv.org/pdf/2008.05122.pdf)
* LIT feature video demo: [Youtube](https://www.youtube.com/watch?v=CuRI_VK83dU)


### **License**

This work is licensed under a Creative Commons Attribution 2.0 Generic License.