地獄貓貓旅行團
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Tokenizer ## Week 29 ---- ## 說文解字 Byte 位元組 Character 字元 Word 單字、詞 Vocabulary 字典 ---- ## Byte 例如 `"Apple"` 可以表示成 `0x41/0x70/0x70/0x6C/0x65` 等 5 個位元組,每個英文字母佔 1 Byte ---- ## Byte 若是中文的「蘋果」則為 `0xE8/0x98/0x8B/0xE6/0x9E/0x9C` 等 6 個位元組,每個中文字佔 3 Bytes ---- ## Character 例如 `"Apple"` 可以分解成 `A/p/p/l/e` 等 5 個字元 而「蘋果」則是 2 個字元 ---- ## Word 例如 `"I have an apple"` 可以分解成 `I/have/an/apple` 等 4 個單字 ---- ## Chinese Word? 「我有一顆蘋果」可以分解成 `我有/一顆/蘋果` `我/有/一/顆/蘋果` 不同定義下有不同的切法 這在中文也稱為 「斷詞 (Word Segmentation)」 ---- ## Token Token 資訊歷史上最難翻譯的一個單字 比較理想的翻譯是「詞元」 若是當成 API Token 則會翻成「令牌」 但兩者的用法情境是不同的 ---- ## Token Tokenize 動詞,指分詞這個動作 Tokenization 名詞,指分詞的過程 Tokenizer 名詞,分詞器、詞元分析器 ---- ## 什麼是 Token 介在 Byte 與 Word 之間的單位 例如 `Apple` 可能是 1 個獨立的 Token 而 `Pineapple` 則可能被分解成 `Pine` 跟 `##apple` 兩個 Tokens ---- ## Too Many Tokens UTF-8 編碼可以表示九萬多種中日韓字元 整個 Unicode 包含了約 14 萬個字元 更遑論這些字元可以合併成更多單字詞 對模型訓練而言,這樣會產生非常高的維度 使模型尺寸相當大,難以訓練 ---- ## Byte-Level Token 把 Token 的單位縮減到 Byte-Level 就能在 256 個 Tokens 內表達所有文字 但是「文本壓縮率」就會很低 「我有一顆蘋果」需要 18 個 Tokens 來表示 ---- ## Tokenizer Training 透過一些統計的技巧 在字典大小與文本壓縮率之間取得平衡 除此之外,還要能夠應對未知詞 Out-of-Vocabulary, OOV --- # 訓練演算法 ---- ## 演算法種類 Character/Word-Level Unigram Word-Piece Byte-Pair Encoding (BPE) ---- ## Char/Word-Level 直接以字元/單字為單位 最單純的做法 不適合中日韓文字 ---- ## Unigram 統計單字詞之間的機率 介在 Char-Level 與 Word-Level 之間 比較能夠表達 OOV 文字 ---- ## Word-Piece 先將所有文本分析成 Words 再把每個 Word 拆解成 Pieces 例如已經有了 `photo, graphy` 兩字 則遇到 `photography` 可拆解成 `photo, ##graphy` 兩個 Tokens ---- ## Byte-Pair Encoding 先把所有文本拆解成 Bytes 再把常常出現的 Byte-Pair 組合在一起 例如已經有了 `a, g, h, o, p, r, t, y` 等 `photo` 很常配在一起,合併成一個 Token `graphy` 很常配在一起,合併成另一個 Token --- # 訓練工具 ---- ## 開源套件 [Hugging Face - Tokenizers](https://github.com/huggingface/tokenizers) (HF Tokenizer) [Google - SentencePiece](https://github.com/google/sentencepiece) (SPM) ---- ## 準備文本 可以參考國科會 TAIDE 計畫的[訓練資料](https://taide.tw/public/trainData) 或者 Hugging Face Hub 上的[繁體中文維基](https://huggingface.co/datasets/bigscience-data/roots_zh-tw_wikipedia) ---- ## Parquet 格式 [Apache Parquet](https://parquet.apache.org/) 是一種 **Column-Oriented** 的資料格式 具有不錯的壓縮率與讀寫速度 對機器學習相關任務相當友善 壓縮率 Gzip > Parquet >>> Raw 讀寫速度 Parquet > Raw >>> Gzip ---- ## Parquet 使用方法 寫入檔案 ```python= import pandas as pd dataset = [ dict(id=0, text="Hello"), dict(id=1, text="Hi"), ] dataset = pd.DataFrame(dataset) dataset.to_parquet("dataset.parquet") ``` 讀取檔案 ```python= import pandas as pd dataset = pd.read_parquet("dataset.parquet") print(dataset) ``` ---- ## 將資料存為純文字格式 ```python= import pandas as pd data = pd.read_parquet("wiki.parquet") with open("Raw.txt", "wt", encoding="UTF-8") as fp: for text in data["text"][:1000]: fp.write(text.strip()) fp.write("\n\n") ``` ---- ## 透過 HF Tokenizers 訓練 ```python= from tokenizers import Tokenizer, models, trainers model = models.BPE(byte_fallback=True, unk_token="[UNK]") trainer = trainers.BpeTrainer(special_tokens=["[UNK]"]) tokenizer = Tokenizer(model) files = ["Raw.txt"] tokenizer.train(files, trainer) tokenizer.save("Tokenizer.json") ``` ---- ## 透過 SentencePiece 訓練 ```bash= spm_train --input=Raw.txt --model_prefix=spm-model/tk --model_type=bpe ``` ---- ## Tokenizer 生態系 SPM 可以被轉成 HF Tokenizer HF Tokenizer 可以跟 Transformers 一起訓練 但 HF Tokenizer 比較難被轉回 SPM

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully