日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NLP - sentencepiece

發布時間:2023/12/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NLP - sentencepiece 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 一、關于 sentencepiece
    • 二、安裝
      • 1、Python 模塊
      • 2、從 C++ 源構建和安裝 SentencePiece 命令行工具
      • 3、使用 vcpkg 構建和安裝
      • 4、從簽名發布的 wheels 下載和安裝 SentencePiece
    • 三、命令行使用
      • 1、訓練模型
      • 2、將原始文本編碼為 sentence pieces/ids
      • 3、編碼 sentence pieces/ids 到原始文本
      • 4、端到端示例 End-to-End Example
      • 5、導出詞匯表 Export vocabulary list
      • 6、重新定義特殊元token
      • 7、詞表限制 Vocabulary restriction
    • 四、Python 調用


一、關于 sentencepiece

  • github : https://github.com/google/sentencepiece
  • 論文 《SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing》:https://aclanthology.org/D18-2012.pdf

Unsupervised text tokenizer for Neural Network-based text generation.

SentencePiece is an unsupervised text tokenizer and detokenizer mainly for Neural Network-based text generation systems where the vocabulary size is predetermined prior to the neural model training.

SentencePiece implements subword units (e.g., byte-pair-encoding (BPE) [Sennrich et al.]) and unigram language model [Kudo.]) with the extension of direct training from raw sentences.

SentencePiece allows us to make a purely end-to-end system that does not depend on language-specific pre/postprocessing.

重復出現次數多的詞組,就認為是一個詞。
粒度比分詞大。
模型在訓練中主要使用統計指標,比如出現的頻率,左右連接度等,還有困惑度來訓練最終的結果。


相關教程:

  • 燭之文: sentencepiece原理與實踐
    https://www.jianshu.com/p/d36c3e06fb98

二、安裝

SentencePiece分為兩部分:訓練模型和使用模型。
訓練模型部分是用C語言實現的,可編成二進程程序執行,訓練結果是生成一個model和一個詞典文件。
模型使用部分同時支持二進制程序和Python調用兩種方式,訓練完生成的詞典數據是明文,可編輯,因此也可以用任何語言讀取和使用。


1、Python 模塊

SentencePiece 提供了 Python 封裝支持 訓練和 segmentation。
你可以通過以下命令安裝 Python 二進制包:

% pip install sentencepiece

For more detail, see Python module


2、從 C++ 源構建和安裝 SentencePiece 命令行工具

需要安裝一下工具和依賴庫:

  • cmake
  • C++11 compiler
  • gperftools library (optional, 10-40% performance improvement can be obtained.)

在 Ubuntu 上,可以使用 apt-get 安裝編譯工具:

sudo apt-get install cmake build-essential pkg-config libgoogle-perftools-dev

然后,你可以以如下方式,構建和安裝命令行工具:

git clone https://github.com/google/sentencepiece.git cd sentencepiece mkdir build cd build cmake .. make -j $(nproc) sudo make install sudo ldconfig -v

在 macOS 上,最后一行用 sudo update_dyld_shared_cache 命令替代


3、使用 vcpkg 構建和安裝

  • vcpkg : https://github.com/Microsoft/vcpkg

你可以使用 vcpkg 下載和安裝 sentencepiece
You can download and install sentencepiece using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install sentencepiece

vcpkg 中的 sentencepiece 被微軟團隊和社區貢獻者保持更新;
如果版本過時了,請聯系vcpkg 倉庫這里創建 issue https://github.com/Microsoft/vcpkg


4、從簽名發布的 wheels 下載和安裝 SentencePiece

你可以從 GitHub releases page 下載 wheel:https://github.com/google/sentencepiece/releases/latest

在發布過程中,我們使用 OpenSSF 生成了 SLSA3 簽名,
OpenSSF’s : slsa-framework/slsa-github-generator
https://github.com/slsa-framework/slsa-github-generator

去驗證一個發布的二進制包:
To verify a release binary:
1、安裝驗證工具:https://github.com/slsa-framework/slsa-verifier#installation
2、從 https://github.com/google/sentencepiece/releases/latest 下載 attestation.intoto.jsonl 源文件;
3、運行驗證器:

slsa-verifier -artifact-path <the-wheel> -provenance attestation.intoto.jsonl -source github.com/google/sentencepiece -tag <the-tag>pip install wheel_file.whl

三、命令行使用

1、訓練模型

訓練模型語法:

spm_train --input= --model_prefix=<model_name> --vocab_size=8000 --character_coverage=1.0 --model_type=


例如:

spm_train --input='../corpus.txt' --model_prefix='../mypiece' --vocab_size=8000 --character_coverage=1 --model_type='bpe'

參數說明

  • --input 指定需要訓練的文本文件;不需要分詞、標準化或其他預處理;
    SentencePiece 默認采用 Unicode NFKC 進行標準化;
    如果有多個文件,可以使用逗號分隔;
  • --model_prefix 指定訓練好的模型名前綴。
    將會生成兩個文件: <model_name>.model 和 <model_name>.vocab (詞典信息)。
  • --vocab_size 訓練后詞表的大小,比如 8000, 16000, 或 32000。
    數量越大訓練越慢,太小(<4000)可能訓練不了。
  • --character_coverage 模型中覆蓋的字符數。中文、日語等字符多的語料可以設置為 0.9995;其他字符少的語料可設置為 1。
  • --model_type,訓練時模型。可選擇的類別有:unigram (默認), bpe, char, 或 word。

  • max_sentence_length 最大句子長度,默認是4192,長度貌似按字節來算,意味一個中文字代表長度為2
  • max_sentencepiece_length 最大的句子塊長度,默認是16
  • seed_sentencepiece_size 控制句子數量,默認是100w
  • num_threads 線程數,默認是開16個
  • use_all_vocab 使用所有的tokens作為詞庫,不過只對word/char 模型管用
  • input_sentence_size 訓練器最大加載數量,默認為0


2、將原始文本編碼為 sentence pieces/ids

Encode raw text into sentence pieces/ids

spm_encode --model=<model_file> --output_format=piece < input > output spm_encode --model=<model_file> --output_format=id < input > output

使用 --extra_options 標識來插入 BOS/EOS 標記,或反轉輸入順序。
Use --extra_options flag to insert the BOS/EOS markers or reverse the input sequence.

spm_encode --extra_options=eos (add </s> only) spm_encode --extra_options=bos:eos (add <s> and </s>) spm_encode --extra_options=reverse:bos:eos (reverse input and add <s> and </s>)

SentencePiece 支持 nbest segmentation 和使用 --output_format=(nbest|sample)_(piece|id) 標識進行 segmentation 抽樣。

spm_encode --model=<model_file> --output_format=sample_piece --nbest_size=-1 --alpha=0.5 < input > output spm_encode --model=<model_file> --output_format=nbest_id --nbest_size=10 < input > output

3、編碼 sentence pieces/ids 到原始文本

spm_decode --model=<model_file> --input_format=piece < input > output spm_decode --model=<model_file> --input_format=id < input > output

使用 --extra_options 選項來解碼倒序的文本。

spm_decode --extra_options=reverse < input > output

4、端到端示例 End-to-End Example

% spm_train --input=data/botchan.txt --model_prefix=m --vocab_size=1000 unigram_model_trainer.cc(494) LOG(INFO) Starts training with : input: "../data/botchan.txt" ... <snip> unigram_model_trainer.cc(529) LOG(INFO) EM sub_iter=1 size=1100 obj=10.4973 num_tokens=37630 num_tokens/piece=34.2091 trainer_interface.cc(272) LOG(INFO) Saving model: m.model trainer_interface.cc(281) LOG(INFO) Saving vocabs: m.vocab% echo "I saw a girl with a telescope." | spm_encode --model=m.model ▁I ▁saw ▁a ▁girl ▁with ▁a ▁ te le s c o pe .% echo "I saw a girl with a telescope." | spm_encode --model=m.model --output_format=id 9 459 11 939 44 11 4 142 82 8 28 21 132 6% echo "9 459 11 939 44 11 4 142 82 8 28 21 132 6" | spm_decode --model=m.model --input_format=id I saw a girl with a telescope.

You can find that the original input sentence is restored from the vocabulary id sequence.


5、導出詞匯表 Export vocabulary list

spm_export_vocab --model=<model_file> --output=<output file>
  • <output file> 存儲詞匯表和排放日志概率的列表。詞匯表id對應于此文件中的行號。
    stores a list of vocabulary and emission log probabilities. The vocabulary id corresponds to the line number in this file.

6、重新定義特殊元token

一般情況下,SentencePiece 使用 Unknown ( <unk>), BOS ( <s>) and EOS (</s>) 對應的 id 為 0, 1 和 2。
我們也可以重新定義訓練中對應的id:

spm_train --bos_id=0 --eos_id=1 --unk_id=5 --input=... --model_prefix=... --character_coverage=...

When setting -1 id e.g., bos_id=-1, this special token is disabled. Note that the unknown id cannot be disabled. We can define an id for padding () as --pad_id=3.

當設置id 為-1,比如 bos_id=-1, 代表這個 token 無效;unknown id 無法取消。

我們可以定義為 padding () 定義id:--pad_id=3。

如果你想為其他特殊token定義id,可以參考:Use custom symbols

https://github.com/google/sentencepiece/blob/master/doc/special_symbols.md


7、詞表限制 Vocabulary restriction

spm_encode接收 --vocabularyand a --vocabulary_threshold選項,這樣 spm_encode 只會產生同樣出現在詞匯表中的符號(至少有一定頻率)。這個技術在 subword-nmt page 中有描述,用法與 subword-nmt 基本相同。

假設 L1和 L2是兩種語言(源語言/目標語言),訓練共享的spm模型,并獲得每種語言的最終詞匯表:

% cat {train_file}.L1 {train_file}.L2 | shuffle > train % spm_train --input=train --model_prefix=spm --vocab_size=8000 --character_coverage=0.9995 % spm_encode --model=spm.model --generate_vocabulary < {train_file}.L1 > {vocab_file}.L1 % spm_encode --model=spm.model --generate_vocabulary < {train_file}.L2 > {vocab_file}.L2

shufflecommand is used just in case because spm_trainloads the first 10M lines of corpus by default.


segment train/test 語料使用 --vocabulary 選項:

% spm_encode --model=spm.model --vocabulary={vocab_file}.L1 --vocabulary_threshold=50 < {test_file}.L1 > {test_file}.seg.L1 % spm_encode --model=spm.model --vocabulary={vocab_file}.L2 --vocabulary_threshold=50 < {test_file}.L2 > {test_file}.seg.L2

四、Python 調用


import sentencepiece as spmsp = spm.SentencePieceProcessor() text = "食材上不會有這樣的糾結" sp.Load("/tmp/test.model") print(sp.EncodeAsPieces(text))

伊織 2022-11-01(天氣第二次變冷)

總結

以上是生活随笔為你收集整理的NLP - sentencepiece的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。