Skip to main content

语言模型中文识字率分析

Project description

语言模型中文识字率分析

项目介绍

本项目的目的是为了调查各个语言模型的中文识字率的情况,以此可以作为后续模型评估分析的参考。

为了分析模型的中文识字率,我们使用三个常用的字符集,总共21267个汉字。

  • 中华人民共和国教育部于2013年颁布的《通用规范汉字表》,在该字表中,共收录了 8105 个汉字,其中一级字表(常用字集)3500个,二级字表3000个,三级字表1605个。字表内容从中文百科中获取。
  • 中華民國教育部頒布的《常用國字標準字體表》 中的甲表和乙表。甲表收录常用字4808个,其中有1749个汉字不在《通用规范汉字表》中;乙表收录次常用字6343个,其中有4503个汉字不在《通用规范汉字表》中。统计汉字识字率时,将只针对增加的汉字进行统计,已经在《通用规范汉字表》中的汉字不再重复统计。
  • 《Unicode中日韩统一表意文字》,为汉字在 Unicode 中的基本区段。在 Unicode 14.0 时,收录了 20992 个汉字,占据码位 U+4E00-U+9FFF。其中有6910个汉字,既不在《通用规范汉字表》中,也不在《常用國字標準字體表》中。统计汉字识字率时,将只针对增加的汉字进行统计,已经在《通用规范汉字表》和《常用國字標準字體表》中的汉字不在重复统计。汉字在 Unicode 中还有其它区段,总共将近9万汉字,但由于其它汉字不常使用,这里暂不纳入统计范围。

对于语言模型是否认知某个汉字的判断,我们通过对应语言模型所使用的 Tokenizer 是否可以对该汉字进行 encode 来判断。

  • 模型不认识某汉字的判定为:
    • 模型对该汉字的编码结果为空;
    • 模型对该汉字的编码结果为 unk_token_id
  • 模型认识某汉字的判定为:
    • 模型对该汉字的编码结果长度为1;
  • 如果编码结果长度大于1,这有可能是因为使用了 BBPE 的原因,一个不常出现的汉字被拆分成了多个 token。由于汉字被以UTF-8的形式编码,拆散该编码并不能体现汉字语义,因此,一个汉字被打散的编码越多,我们认为该模型对该汉字的认知程度可能越低。所以,对于编码结果长度大于1的情况,我们认为该模型对该汉字的认知程度为 1 / len(encode_result),用以控制半透明程度。在识字率的计数中,将计数为 0

在进行判断前,会先行去除前缀后缀的特殊token。

命令行工具 vocab-coverage 使用指南

vocab-coverage 是一个命令行工具,用于分析模型的汉字识字率。

安装

pip install vocab-coverage

由于图中有汉字,因此需要中文字体,这里我使用了 Noto Sans CJK 字体用于中文,以及 Anonymous Pro字体,建议安装该字体。

Linux

如 Ubuntu,可以通过以下命令安装:

sudo apt install fonts-noto-cjk fonts-anonymous-pro

Mac

如 MacOS,可以通过以下命令安装:

brew install font-noto-sans-cjk font-noto-serif-cjk font-anonymous-pro

Windows

如 Windows,可以通过以下命令安装:

choco install noto anonymouspro

使用

vocab-coverage 它有三个子命令:charsetcoverageembedding

charset 子命令

charset 子命令用于生成用以统计识字率的字表文件。

$ vocab-coverage charset --help
usage: vocab-coverage charset [-h] [--charset_file CHARSET_FILE]

options:
  -h, --help            show this help message and exit
  --charset_file CHARSET_FILE
                        用以统计识字率的字表文件(默认:charset.json)

coverage 子命令

coverage 子命令用于分析模型的汉字识字率。

$ vocab-coverage coverage --help
usage: vocab-coverage coverage [-h] [--model_name MODEL_NAME] [--charset_file CHARSET_FILE] [--output_dir OUTPUT_DIR] [--debug]

options:
  -h, --help            show this help message and exit
  --model_name MODEL_NAME
                        模型在 HuggingFace Hub 上的名称(默认为 shibing624/text2vec-base-chinese)
  --charset_file CHARSET_FILE
                        用以统计识字率的字表文件(默认为内置字符集文件)
  --output_dir OUTPUT_DIR
                        生成的图像文件的输出目录(默认为 images)
  --debug               是否打印调试信息
  • --model_name:模型在 HuggingFace Hub 上的名称。默认为 shibing624/text2vec-base-chinese
  • --charset_file:用以统计识字率的字表文件。默认为 charset.json
  • --output_dir:生成的图像文件的输出目录。默认为 images
  • --debug:是否打印调试信息。

示例

$ vocab-coverage coverage --model_name=THUDM/chatglm-6b
检查模型 THUDM/chatglm-6b 的字表
字表《通用规范汉字表》一级汉字:3499/3500 (99.97%)
字表《通用规范汉字表》二级汉字:1724/3000 (57.47%)
字表《通用规范汉字表》三级汉字:48/1605 (2.99%)
字表《常用國字標準字體表》甲表():185/1749 (10.58%)
字表《常用國字標準字體表》乙表():14/4503 (0.31%)
字表《Unicode中日韩统一表意文字》():115/6910 (1.66%)

除了上述输出外,还会在 images 目录下生成一个图像文件,images/coverage/THUDM_chatglm-6b.coverage.png,为可视化的分析结果。

embedding 子命令

embedding 子命令用于分析模型词向量在空间中的分布情况。

usage: main.py embedding [-h] [--model_name MODEL_NAME] [--charset_file CHARSET_FILE] [--output_dir OUTPUT_DIR] [--is_detail] [--debug]

options:
  -h, --help            show this help message and exit
  --model_name MODEL_NAME
                        模型在 HuggingFace Hub 上的名称(默认为 shibing624/text2vec-base-chinese)
  --charset_file CHARSET_FILE
                        用以统计识字率的字表文件(默认为内置字符集文件)
  --output_dir OUTPUT_DIR
                        生成的图像文件的输出目录(默认为 images)
  --is_detail           是否对汉字进行详细分类(默认为 False)
  --debug               是否打印调试信息(默认为 False)
  • --model_name:模型在 HuggingFace Hub 上的名称。默认为 shibing624/text2vec-base-chinese
  • --charset_file:用以加载字符集的文件。默认为 charset.json
  • --output_dir:生成的图像文件的输出目录。默认为 images
  • --is_detail:是否对汉字进行详细分类。默认为 False
  • --debug:是否打印调试信息。默认为 False

示例

$ vocab-coverage embedding --model_name=THUDM/chatglm-6b --debug
对模型 THUDM/chatglm-6b  embedding 进行可视化...
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████| 8/8 [00:14<00:00,  1.79s/it]
reducing embeddings (130528, 4096) to 2D...
[t-SNE] Computing 91 nearest neighbors...
[t-SNE] Indexed 130528 samples in 0.223s...
...
[t-SNE] Computed conditional probabilities for sample 130528 / 130528
[t-SNE] Mean sigma: 0.251563
[t-SNE] Computed conditional probabilities in 1.189s
[t-SNE] Iteration 50: error = 111.8378067, gradient norm = 0.0000213 (50 iterations in 8.560s)
[t-SNE] Iteration 100: error = 111.7450027, gradient norm = 0.0002728 (50 iterations in 13.605s)
[t-SNE] Iteration 150: error = 111.7283707, gradient norm = 0.0000039 (50 iterations in 7.279s)
[t-SNE] Iteration 200: error = 111.7283707, gradient norm = 0.0000039 (50 iterations in 7.123s)
...
[t-SNE] Iteration 950: error = 3.7165585, gradient norm = 0.0030942 (50 iterations in 7.229s)
[t-SNE] Iteration 1000: error = 3.6922786, gradient norm = 0.0029739 (50 iterations in 7.451s)
[t-SNE] KL divergence after 1000 iterations: 3.692279
draw embeddings (130528, 2)...
...
draw embedding point: 130528
font size: 25, font: ('Noto Sans CJK JP', 'Regular')
...
save to images/embeddings/THUDM-chatglm-6b.embedding.jpg...

分析结果

如果图片无法显示,请访问项目 Github 页面:https://github.com/twang2218/vocab-coverage

下面是挑选出来的一些比较有特点的模型,完整的模型分析列表请查看 模型分析列表

BERT 类模型

名称 中文覆盖率 输入词向量分布 输出词向量分布
bert-base-cased Vocab Coverage for <b>bert-base-cased</b> input embedding image for <b>bert-base-cased</b> output embedding image for <b>bert-base-cased</b>
roberta-large Vocab Coverage for <b>roberta-large</b> input embedding image for <b>roberta-large</b> output embedding image for <b>roberta-large</b>
bert-base-multilingual-cased Vocab Coverage for <b>bert-base-multilingual-cased</b> input embedding image for <b>bert-base-multilingual-cased</b> output embedding image for <b>bert-base-multilingual-cased</b>

T5 模型及其衍生的模型对比

名称 中文覆盖率 输入词向量分布 输出词向量分布

google

/

flan-t5-base

Vocab Coverage for <b><p>google</p><p>/</p><p>flan-t5-base</p></b> input embedding image for <b><p>google</p><p>/</p><p>flan-t5-base</p></b> output embedding image for <b><p>google</p><p>/</p><p>flan-t5-base</p></b>

shibing624

/

prompt-t5-base-chinese

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>prompt-t5-base-chinese</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>prompt-t5-base-chinese</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>prompt-t5-base-chinese</p></b>

shibing624

/

mengzi-t5-base-chinese-correction

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>mengzi-t5-base-chinese-correction</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>mengzi-t5-base-chinese-correction</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>mengzi-t5-base-chinese-correction</p></b>

bert-base-chinese 模型及其衍生的模型对比

名称 中文覆盖率 输入词向量分布 输出词向量分布
bert-base-chinese Vocab Coverage for <b>bert-base-chinese</b> input embedding image for <b>bert-base-chinese</b> output embedding image for <b>bert-base-chinese</b>

hfl

/

chinese-macbert-base

Vocab Coverage for <b><p>hfl</p><p>/</p><p>chinese-macbert-base</p></b> input embedding image for <b><p>hfl</p><p>/</p><p>chinese-macbert-base</p></b> output embedding image for <b><p>hfl</p><p>/</p><p>chinese-macbert-base</p></b>

shibing624

/

text2vec-base-chinese

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese</p></b>

moka-ai

/

m3e-base

Vocab Coverage for <b><p>moka-ai</p><p>/</p><p>m3e-base</p></b> input embedding image for <b><p>moka-ai</p><p>/</p><p>m3e-base</p></b> output embedding image for <b><p>moka-ai</p><p>/</p><p>m3e-base</p></b>

junnyu

/

wobert_chinese_plus_base

Vocab Coverage for <b><p>junnyu</p><p>/</p><p>wobert_chinese_plus_base</p></b> input embedding image for <b><p>junnyu</p><p>/</p><p>wobert_chinese_plus_base</p></b> output embedding image for <b><p>junnyu</p><p>/</p><p>wobert_chinese_plus_base</p></b>

ERNIE 模型及其衍生的模型对比

名称 中文覆盖率 输入词向量分布 输出词向量分布

nghuyong

/

ernie-3.0-base-zh

Vocab Coverage for <b><p>nghuyong</p><p>/</p><p>ernie-3.0-base-zh</p></b> input embedding image for <b><p>nghuyong</p><p>/</p><p>ernie-3.0-base-zh</p></b> output embedding image for <b><p>nghuyong</p><p>/</p><p>ernie-3.0-base-zh</p></b>

shibing624

/

text2vec-base-chinese-sentence

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-sentence</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-sentence</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-sentence</p></b>

shibing624

/

text2vec-base-chinese-paraphrase

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-paraphrase</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-paraphrase</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-chinese-paraphrase</p></b>

paraphrase-multilingual-MiniLM-L12-v2 模型及其衍生的模型对比

名称 中文覆盖率 输入词向量分布 输出词向量分布

sentence-transformers

/

paraphrase-multilingual-MiniLM-L12-v2

Vocab Coverage for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-MiniLM-L12-v2</p></b> input embedding image for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-MiniLM-L12-v2</p></b> output embedding image for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-MiniLM-L12-v2</p></b>

shibing624

/

text2vec-base-multilingual

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>text2vec-base-multilingual</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-multilingual</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>text2vec-base-multilingual</p></b>

xlm-roberta-base 模型及其微调后的模型对比

名称 中文覆盖率 输入词向量分布 输出词向量分布
xlm-roberta-base Vocab Coverage for <b>xlm-roberta-base</b> input embedding image for <b>xlm-roberta-base</b> output embedding image for <b>xlm-roberta-base</b>

sentence-transformers

/

paraphrase-multilingual-mpnet-base-v2

Vocab Coverage for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-mpnet-base-v2</p></b> input embedding image for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-mpnet-base-v2</p></b> output embedding image for <b><p>sentence-transformers</p><p>/</p><p>paraphrase-multilingual-mpnet-base-v2</p></b>

LLaMA 及衍生模型

名称 中文覆盖率 输入词向量分布 输出词向量分布

decapoda-research

/

llama-7b-hf

Vocab Coverage for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b> input embedding image for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b> output embedding image for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b>

lmsys

/

vicuna-7b-delta-v1.1

Vocab Coverage for <b><p>lmsys</p><p>/</p><p>vicuna-7b-delta-v1.1</p></b> input embedding image for <b><p>lmsys</p><p>/</p><p>vicuna-7b-delta-v1.1</p></b> output embedding image for <b><p>lmsys</p><p>/</p><p>vicuna-7b-delta-v1.1</p></b>

togethercomputer

/

RedPajama-INCITE-7B-Chat

Vocab Coverage for <b><p>togethercomputer</p><p>/</p><p>RedPajama-INCITE-7B-Chat</p></b> input embedding image for <b><p>togethercomputer</p><p>/</p><p>RedPajama-INCITE-7B-Chat</p></b> output embedding image for <b><p>togethercomputer</p><p>/</p><p>RedPajama-INCITE-7B-Chat</p></b>

openlm-research

/

open_llama_7b

Vocab Coverage for <b><p>openlm-research</p><p>/</p><p>open_llama_7b</p></b> input embedding image for <b><p>openlm-research</p><p>/</p><p>open_llama_7b</p></b> output embedding image for <b><p>openlm-research</p><p>/</p><p>open_llama_7b</p></b>

shibing624

/

chinese-alpaca-plus-7b-hf

Vocab Coverage for <b><p>shibing624</p><p>/</p><p>chinese-alpaca-plus-7b-hf</p></b> input embedding image for <b><p>shibing624</p><p>/</p><p>chinese-alpaca-plus-7b-hf</p></b> output embedding image for <b><p>shibing624</p><p>/</p><p>chinese-alpaca-plus-7b-hf</p></b>

英文大语言模型

名称 中文覆盖率 输入词向量分布 输出词向量分布

decapoda-research

/

llama-7b-hf

Vocab Coverage for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b> input embedding image for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b> output embedding image for <b><p>decapoda-research</p><p>/</p><p>llama-7b-hf</p></b>

mosaicml

/

mpt-7b-instruct

Vocab Coverage for <b><p>mosaicml</p><p>/</p><p>mpt-7b-instruct</p></b> input embedding image for <b><p>mosaicml</p><p>/</p><p>mpt-7b-instruct</p></b> output embedding image for <b><p>mosaicml</p><p>/</p><p>mpt-7b-instruct</p></b>

tiiuae

/

falcon-7b-instruct

Vocab Coverage for <b><p>tiiuae</p><p>/</p><p>falcon-7b-instruct</p></b> input embedding image for <b><p>tiiuae</p><p>/</p><p>falcon-7b-instruct</p></b> output embedding image for <b><p>tiiuae</p><p>/</p><p>falcon-7b-instruct</p></b>

nomic-ai

/

gpt4all-j

Vocab Coverage for <b><p>nomic-ai</p><p>/</p><p>gpt4all-j</p></b> input embedding image for <b><p>nomic-ai</p><p>/</p><p>gpt4all-j</p></b> output embedding image for <b><p>nomic-ai</p><p>/</p><p>gpt4all-j</p></b>

OpenAssistant

/

oasst-sft-4-pythia-12b-epoch-3.5

Vocab Coverage for <b><p>OpenAssistant</p><p>/</p><p>oasst-sft-4-pythia-12b-epoch-3.5</p></b> input embedding image for <b><p>OpenAssistant</p><p>/</p><p>oasst-sft-4-pythia-12b-epoch-3.5</p></b> output embedding image for <b><p>OpenAssistant</p><p>/</p><p>oasst-sft-4-pythia-12b-epoch-3.5</p></b>

中文大语言模型

名称 中文覆盖率 输入词向量分布 输出词向量分布

THUDM

/

chatglm-6b

Vocab Coverage for <b><p>THUDM</p><p>/</p><p>chatglm-6b</p></b> input embedding image for <b><p>THUDM</p><p>/</p><p>chatglm-6b</p></b> output embedding image for <b><p>THUDM</p><p>/</p><p>chatglm-6b</p></b>

THUDM

/

chatglm2-6b

Vocab Coverage for <b><p>THUDM</p><p>/</p><p>chatglm2-6b</p></b> input embedding image for <b><p>THUDM</p><p>/</p><p>chatglm2-6b</p></b> output embedding image for <b><p>THUDM</p><p>/</p><p>chatglm2-6b</p></b>

fnlp

/

moss-moon-003-sft

Vocab Coverage for <b><p>fnlp</p><p>/</p><p>moss-moon-003-sft</p></b> input embedding image for <b><p>fnlp</p><p>/</p><p>moss-moon-003-sft</p></b> output embedding image for <b><p>fnlp</p><p>/</p><p>moss-moon-003-sft</p></b>

baichuan-inc

/

baichuan-7B

Vocab Coverage for <b><p>baichuan-inc</p><p>/</p><p>baichuan-7B</p></b> input embedding image for <b><p>baichuan-inc</p><p>/</p><p>baichuan-7B</p></b> output embedding image for <b><p>baichuan-inc</p><p>/</p><p>baichuan-7B</p></b>

OpenAI 提供的模型

名称 中文覆盖率 输入词向量分布 输出词向量分布

OpenAI

/

text-embedding-ada-002

Vocab Coverage for <b><p>OpenAI</p><p>/</p><p>text-embedding-ada-002</p></b> output embedding image for <b><p>OpenAI</p><p>/</p><p>text-embedding-ada-002</p></b>

其他模型

请参见 模型分析列表

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vocab-coverage-0.6.tar.gz (100.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vocab_coverage-0.6-py3-none-any.whl (90.8 kB view details)

Uploaded Python 3

File details

Details for the file vocab-coverage-0.6.tar.gz.

File metadata

  • Download URL: vocab-coverage-0.6.tar.gz
  • Upload date:
  • Size: 100.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for vocab-coverage-0.6.tar.gz
Algorithm Hash digest
SHA256 66f57c3ea4194659173bf42892f3db1f51031b1171e20fc9f373e1b9764df008
MD5 88f7af35dcdfd48657702d0a3bd483ba
BLAKE2b-256 5202fd7c0cc9f02a442f75bb5d7d4812a0dd04e08c307b1169f2c2709f627b4d

See more details on using hashes here.

File details

Details for the file vocab_coverage-0.6-py3-none-any.whl.

File metadata

  • Download URL: vocab_coverage-0.6-py3-none-any.whl
  • Upload date:
  • Size: 90.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for vocab_coverage-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 03d8603d045a1e29a33ab58f2c18833be3adbdd0eee03ee01fe60feac34464f1
MD5 b9614f708ce524db7f39dfea0703f2c2
BLAKE2b-256 62cb2d82e0790c77ad313c4dad806093693468a87bd8ea796a0bfcd3063f6de6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page