Wiki40B 語言模型

在 TensorFlow.org 上檢視 在 Google Colab 中執行 在 GitHub 上檢視 下載筆記本 查看 TF Hub 模型

使用 TensorFlow HubWiki40B 語言模型產生類似 Wikipedia 的文字!

本筆記本說明如何

  • 載入 TF-Hub 上 Wiki40b-LM 系列的 41 個單語和 2 個多語語言模型
  • 使用這些模型來取得給定文字片段的困惑度、每層啟動和詞嵌入
  • 從種子文字逐個 Token 產生文字

這些語言模型是在 TensorFlow Datasets 上提供的最新發布、清理過的 Wiki40B 資料集上訓練的。訓練設定是基於論文 「Wiki-40B:多語言語言模型資料集」

設定

安裝依賴項

匯入

2023-12-08 13:06:23.563120: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2023-12-08 13:06:24.329384: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2023-12-08 13:06:24.329482: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2023-12-08 13:06:24.329492: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.

選擇語言

讓我們選擇要從 TF-Hub 載入的語言模型以及要產生的文字長度

Using the https://tfhub.dev/google/wiki40b-lm-en/1 model to generate sequences of max length 20.

建立模型

好的,既然我們已經設定好要使用的預先訓練模型,讓我們將其設定為產生最多 max_gen_len 的文字。我們需要從 TF-Hub 載入語言模型,輸入一段起始文字,然後迭代輸入 Token,因為它們是產生的。

載入語言模型組件

2023-12-08 13:06:32.463390: W tensorflow/core/common_runtime/graph_constructor.cc:1526] Importing a graph with a lower producer version 359 into an existing graph with producer version 1286. Shape inference will have run different parts of the graph with different producer versions.
2023-12-08 13:06:34.423955: E tensorflow/compiler/xla/stream_executor/cuda/cuda_driver.cc:267] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected

建構每個 Token 的產生圖

max_gen_len 個 Token 建構靜態展開圖

產生一些文字

讓我們產生一些文字!我們將設定文字 seed 來提示語言模型。

您可以使用預先定義的種子之一,或選擇性地輸入您自己的種子。此文字將用作語言模型的種子,以協助提示語言模型接下來要產生什麼。

您可以使用以下特殊 Token 來標示產生文章的特殊部分。使用 _START_ARTICLE_ 表示文章的開頭,使用 _START_SECTION_ 表示章節的開頭,以及 _START_PARAGRAPH_ 在文章中產生文字

預先定義的種子

輸入您自己的種子(選填)。

Generating text from seed:

_START_ARTICLE_
1882 Prince Edward Island general election
_START_PARAGRAPH_
The 1882 Prince Edward Island election was held on May 8, 1882 to elect members of the House of Assembly of the province of Prince Edward Island, Canada.

初始化工作階段。

產生文字

This election were also the first time that two members first met. A majority of twelve elected members (

我們也可以查看模型的其他輸出 - 困惑度、Token ID、中間啟動和嵌入

ppl_result
array([23.507736], dtype=float32)
token_ids_result
array([[   8,    3, 6794, 1579, 1582,  721,  489,  448,    8,    5,   26,
        6794, 1579, 1582,  721,  448,   17,  245,   22,  166, 2928, 6794,
          16, 7690,  384,   11,    7,  402,   11, 1172,   11,    7, 2115,
          11, 1579, 1582,  721,    9,  646,   10]], dtype=int32)
activations_result.shape
(12, 1, 39, 768)
embeddings_result
array([[[ 0.12262525,  5.548009  ,  1.4743135 , ...,  2.4388404 ,
         -2.2788858 ,  2.172028  ],
        [-2.3905468 , -0.97108954, -1.5513545 , ...,  8.458472  ,
         -2.8723319 ,  0.6534524 ],
        [-0.83790785,  0.41630274, -0.8740793 , ...,  1.6446769 ,
         -0.9074106 ,  0.3339265 ],
        ...,
        [-0.8054745 , -1.2495526 ,  2.6232922 , ...,  2.893288  ,
         -0.91287214, -1.1259722 ],
        [ 0.64944506,  3.3696785 ,  0.09543293, ..., -0.7839227 ,
         -1.3573489 ,  1.862214  ],
        [-1.2970612 ,  0.5961366 ,  3.3531897 , ...,  3.2853985 ,
         -1.6212384 ,  0.30257902]]], dtype=float32)