{"id":25947,"date":"2025-05-11T13:29:33","date_gmt":"2025-05-11T05:29:33","guid":{"rendered":"http:\/\/139.9.1.231\/?p=25947"},"modified":"2025-05-11T13:29:34","modified_gmt":"2025-05-11T05:29:34","slug":"bpe-subword-nmt","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2025\/05\/11\/bpe-subword-nmt\/","title":{"rendered":"BPE\u5206\u8bcd\u7b97\u6cd5-\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0"},"content":{"rendered":"\n<ul class=\"has-medium-pink-color has-light-gray-background-color has-text-color has-background\"><li><strong><em><a href=\"https:\/\/www.wolai.com\/suprit\/heU7bADzdRaJje1jdeoVLp\">https:\/\/www.wolai.com\/suprit\/heU7bADzdRaJje1jdeoVLp<\/a><\/em><\/strong><\/li><li><strong><em><a href=\"https:\/\/github.com\/espnet\/espnet\/blob\/master\/espnet2\/text\/sentencepiece_tokenizer.py\">https:\/\/github.com\/espnet\/espnet\/blob\/master\/espnet2\/text\/sentencepiece_tokenizer.py<\/a><\/em><\/strong><\/li><li><strong><em><a href=\"https:\/\/github.com\/rsennrich\/subword-nmt\/tree\/master\">https:\/\/github.com\/rsennrich\/subword-nmt\/tree\/master<\/a> \u3010\u6700\u7ecf\u5178\u7684BPE\u5305\uff0cParaformer\u5206\u8bcd\u5927\u6982\u7387\u8ddf\u8be5\u4ed3\u5e93\u7c7b\u4f3c\u3011<\/em><\/strong><\/li><\/ul>\n\n\n\n\n\n<p><strong>\u4e09\u79cd\u4e3b\u6d41\u7684Subword\u7b97\u6cd5\uff0c\u5b83\u4eec\u5206\u522b\u662f\uff1aByte Pair Encoding (BPE)\u3001WordPiece\u548cUnigram Language Model\u3002<\/strong><\/p>\n\n\n\n<p>\u6267\u884c\u5206\u8bcd\u7684\u7b97\u6cd5\u6a21\u578b\u79f0\u4e3a<strong>\u5206\u8bcd\u5668\uff08Tokenizer\uff09<\/strong>\uff0c\u5212\u5206\u597d\u7684\u4e00\u4e2a\u4e2a\u8bcd\u79f0\u4e3a<strong>Token<\/strong>\uff08\u4e2d\u6587\u53eb\u8bcd\u5143\uff0c\u4e3a\u5565\u4e0d\u76f4\u63a5\u53ebWord\uff1f\u63a5\u7740\u5f80\u540e\u770b\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a<strong>Tokenization<\/strong>\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u4e00\u4e2a\u4e2a\u7684token\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c0f\u7247\u6bb5\uff09\u8868\u793a\u5411\u91cf\uff0c\u6211\u4eec\u5206\u8bcd\u7684\u76ee\u7684\u5c31\u662f\u5c3d\u53ef\u80fd\u7684\u8ba9\u8fd9\u4e9b\u5411\u91cf\u8574\u542b\u66f4\u591a\u6709\u7528\u7684\u4fe1\u606f\uff0c\u7136\u540e\u628a\u8fd9\u4e9b\u5411\u91cf\u8f93\u5165\u5230\u7b97\u6cd5\u6a21\u578b\u4e2d\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u4e00\u7bc7\u6587\u672c\u7684\u8bcd\u5f80\u5f80\u592a\u591a\u4e86\uff0c\u4e3a\u4e86\u65b9\u4fbf\u7b97\u6cd5\u6a21\u578b\u8bad\u7ec3\uff0c\u6211\u4eec\u4f1a\u9009\u53d6\u51fa<strong>\u9891\u7387<\/strong>\uff08\u4e5f\u53ef\u80fd\u662f\u5176\u5b83\u7684\u6743\u91cd\uff09\u6700\u9ad8\u7684\u82e5\u5e72\u4e2a\u8bcd\u7ec4\u6210\u4e00\u4e2a<strong>\u8bcd\u8868\uff08Vocabulary\uff09<\/strong>\u3002<\/p>\n\n\n\n<h3>\u203c\ufe0f\u53e4\u5178\u5206\u8bcd\u65b9\u6cd5\u7684\u7f3a\u70b9<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"784\" height=\"301\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-13.png\" alt=\"\" class=\"wp-image-26166\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-13.png 784w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-13-300x115.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-13-768x295.png 768w\" sizes=\"(max-width: 784px) 100vw, 784px\" \/><\/figure>\n\n\n\n<p>\u4e00\u4e2a\u53e5\u5b50\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u89c4\u5219\uff0c\u5c06\u6709\u8bb8\u591a\u79cd\u4e0d\u540c\u7684\u5206\u8bcd\u7ed3\u679c\u3002\u53e4\u5178\u5206\u8bcd\u65b9\u6cd5\u7684\u7f3a\u70b9\u975e\u5e38\u660e\u663e\uff1a<\/p>\n\n\n\n<ul><li>\u5bf9\u4e8e<strong>\u672a\u5728\u8bcd\u8868\u4e2d\u51fa\u73b0\u7684\u8bcd\uff08Out Of Vocabulary, OOV<\/strong>\uff09\uff0c\u6a21\u578b\u5c06\u65e0\u6cd5\u5904\u7406\uff08\u672a\u77e5\u7b26\u53f7\u6807\u8bb0\u4e3a <code>[UNK]<\/code>\uff09\u3002<\/li><li>\u8bcd\u8868\u4e2d\u7684\u4f4e\u9891\u8bcd\/\u7a00\u758f\u8bcd\u5728\u6a21\u578b\u8bad\u65e0\u6cd5\u5f97\u5230\u8bad\u7ec3\uff08\u56e0\u4e3a\u8bcd\u8868\u5927\u5c0f\u6709\u9650\uff0c\u592a\u5927\u7684\u8bdd\u4f1a\u5f71\u54cd\u6548\u7387\uff09\u3002<\/li><li>\u2b50\ufe0f\u5f88\u591a\u8bed\u8a00\u96be\u4ee5\u7528\u7a7a\u683c\u8fdb\u884c\u5206\u8bcd\uff0c\u4f8b\u5982\u82f1\u8bed\u5355\u8bcd\u7684\u591a\u5f62\u6001\uff0c&#8221;look&#8221;\u884d\u751f\u51fa\u7684&#8221;looks&#8221;, &#8220;looking&#8221;, &#8220;looked&#8221;\uff0c\u5176\u5b9e\u90fd\u662f\u4e00\u4e2a\u610f\u601d\uff0c\u4f46\u662f\u5728\u8bcd\u8868\u4e2d\u5374\u88ab\u5f53\u4f5c\u4e0d\u540c\u7684\u8bcd\u5904\u7406\uff0c\u6a21\u578b\u4e5f\u65e0\u6cd5\u901a\u8fc7<code>old, older, oldest<\/code>\u4e4b\u95f4\u7684\u5173\u7cfb\u5b66\u5230<code>smart, smarter, smartest<\/code>\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u8fd9\u4e00\u65b9\u9762\u589e\u52a0\u4e86\u8bad\u7ec3\u5197\u4f59\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u9020\u6210\u4e86\u5927\u8bcd\u6c47\u91cf\u95ee\u9898\u3002<\/li><\/ul>\n\n\n\n<p><strong>Character embedding\uff0c<\/strong>\u662f\u4e00\u79cd\u66f4\u4e3a\u6781\u7aef\u7684\u5206\u8bcd\u65b9\u6cd5\uff0c\u76f4\u63a5\u628a\u4e00\u4e2a\u8bcd\u5206\u6210\u4e00\u4e2a\u4e00\u4e2a\u7684\u5b57\u6bcd\u548c\u7279\u6b8a\u7b26\u53f7\u3002\u867d\u7136\u80fd\u89e3\u51b3OOV\u95ee\u9898\uff0c\u4e5f\u907f\u514d\u4e86\u5927\u8bcd\u6c47\u91cf\u95ee\u9898\uff0c\u4f46\u7f3a\u70b9\u4e5f\u592a\u660e\u663e\u4e86\uff0c<strong>\u7c92\u5ea6\u592a\u7ec6\uff0c\u8bad\u7ec3\u82b1\u8d39\u7684\u6210\u672c\u592a\u9ad8\uff0c\u4f46\u8fd9\u79cd\u601d\u60f3\u6216\u8bb8\u6211\u4eec\u540e\u9762\u4f1a\u7528\u5230\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"613\" height=\"249\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-14.png\" alt=\"\" class=\"wp-image-26171\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-14.png 613w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-14-300x122.png 300w\" sizes=\"(max-width: 613px) 100vw, 613px\" \/><\/figure>\n\n\n\n<p>BERT\u7b97\u6cd5\u7684\u6a2a\u7a7a\u51fa\u4e16\uff0cNLP\u4e2d\u7684\u5f88\u591a\u9886\u57df\u90fd\u88ab\u98a0\u8986\u6027\u7684\u6539\u53d8\u4e86\uff0cBERT\u4e5f\u79f0\u4e3a\u4e86\u4e00\u4e2a\u975e\u5e38\u4e3b\u6d41\u7684NLP\u7b97\u6cd5\u3002\u7531\u4e8eBERT\u7684\u7279\u6027\uff0c\u8981\u6c42\u5206\u8bcd\u65b9\u6cd5\u4e5f\u5fc5\u987b\u4f5c\u51fa\u6539\u53d8\u3002\u8fd9\u5c31\u5bf9\u5e94\u63d0\u51fa\u4e86Subword\u7b97\u6cd5\uff08\u6216\u6210\u4e3aWordPiece\uff09\uff0c\u8be5\u7b97\u6cd5\u5df2\u7ecf\u6210\u4e3a\u4e00\u79cd\u6807\u914d\u3002<\/p>\n\n\n\n<h3>\u57fa\u4e8e\u5b50\u8bcd\u7684\u5206\u8bcd\u65b9\u6cd5\uff08Subword Tokenization\uff09<\/h3>\n\n\n\n<p>\u53ef\u89c1\u4e0d\u8bba\u662f\u4f20\u7edf\u5206\u8bcd\u7b97\u6cd5\u7684\u5c40\u9650\u6027\uff0c\u8fd8\u662fBERT\u7684\u6a2a\u7a7a\u51fa\u4e16\uff0c\u90fd\u8981\u6c42\u6211\u4eec\u63d0\u51fa\u65b0\u7684\u5206\u8bcd\u7b97\u6cd5\uff0c\u4e0b\u9762\u5c31\u8f6e\u5230\u672c\u6587\u7684\u4e3b\u89d2\u767b\u573a\uff1a<strong>\u57fa\u4e8e\u5b50\u8bcd\u7684\u5206\u8bcd\u65b9\u6cd5\uff08Subword Tokenization\uff09<\/strong>\uff0c\u7b80\u79f0\u4e3aSubword\u7b97\u6cd5\uff0c\u610f\u601d\u5c31\u662f<strong>\u628a\u4e00\u4e2a\u8bcd\u5207\u6210\u66f4\u5c0f\u7684\u4e00\u5757\u4e00\u5757\u7684\u5b50\u8bcd\u3002\u5982\u679c\u6211\u4eec\u80fd\u4f7f\u7528\u5c06\u4e00\u4e2atoken\u5206\u6210\u591a\u4e2asubtokens\uff0c\u4e0a\u9762\u7684\u95ee\u9898\u5c31\u80fd\u5f88\u597d\u7684\u89e3\u51b3<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u76ee\u7684\u662f\u901a\u8fc7<strong>\u4e00\u4e2a\u6709\u9650\u7684\u8bcd\u8868<\/strong>\u6765\u89e3\u51b3\u6240\u6709\u5355\u8bcd\u7684\u5206\u8bcd\u95ee\u9898\uff0c\u540c\u65f6\u5c3d\u53ef\u80fd\u5c06\u7ed3\u679c\u4e2dtoken\u7684\u6570\u76ee\u964d\u5230\u6700\u4f4e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u66f4\u5c0f\u7684\u8bcd\u7247\u6bb5\u6765\u7ec4\u6210\u66f4\u5927\u7684\u8bcd\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>\u201c<strong><em>unfortunately<\/em><\/strong>\u201d = \u201c<strong><em>un<\/em><\/strong>\u201d + \u201c<strong><em>for<\/em><\/strong>\u201d + \u201c<strong><em>tun<\/em><\/strong>\u201d + \u201c<strong><em>ate<\/em><\/strong>\u201d + \u201c<strong><em>ly<\/em><\/strong>\u201d\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6709\u70b9\u7c7b\u4f3c\u82f1\u8bed\u4e2d\u7684\u8bcd\u6839\u8bcd\u7f00\u62fc\u8bcd\u6cd5\uff0c\u5176\u4e2d\u7684\u8fd9\u4e9b\u5c0f\u7247\u6bb5\u53c8\u53ef\u4ee5\u7528\u6765\u6784\u9020\u5176\u4ed6\u8bcd\u3002\u53ef\u89c1\u8fd9\u6837\u505a\uff0c<strong>\u65e2\u53ef\u4ee5\u964d\u4f4e\u8bcd\u8868\u7684\u5927\u5c0f<\/strong>\uff0c<strong>\u540c\u65f6\u5bf9\u76f8\u8fd1\u8bcd\u4e5f\u80fd\u66f4\u597d\u5730\u5904\u7406<\/strong>\u3002<\/p>\n\n\n\n<h4>Subword\u4e0e\u4f20\u7edf\u5206\u8bcd\u65b9\u6cd5\u7684\u6bd4\u8f83<\/h4>\n\n\n\n<ul><li>\u4f20\u7edf\u8bcd\u8868\u793a\u65b9\u6cd5\u65e0\u6cd5\u5f88\u597d\u7684\u5904\u7406\u672a\u77e5\u6216\u7f55\u89c1\u7684\u8bcd\u6c47\uff08OOV\u95ee\u9898\uff09\u3002<\/li><li>\u4f20\u7edf\u8bcdtokenization\u65b9\u6cd5\u4e0d\u5229\u4e8e\u6a21\u578b\u5b66\u4e60\u8bcd\u7f00\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4f8b\u5982\u6a21\u578b\u5b66\u5230\u7684\u201cold\u201d, \u201colder\u201d, and \u201coldest\u201d\u4e4b\u95f4\u7684\u5173\u7cfb\u65e0\u6cd5\u6cdb\u5316\u5230\u201csmart\u201d, \u201csmarter\u201d, and \u201csmartest\u201d\u3002<\/li><li>Character embedding\u4f5c\u4e3aOOV\u7684\u89e3\u51b3\u65b9\u6cd5\u7c92\u5ea6\u592a\u7ec6\u3002<\/li><li>Subword\u7c92\u5ea6\u5728\u8bcd\u4e0e\u5b57\u7b26\u4e4b\u95f4\uff0c\u80fd\u591f\u8f83\u597d\u7684\u5e73\u8861OOV\u95ee\u9898\u3002<\/li><\/ul>\n\n\n\n<p>\u76ee\u524d\u6709\u4e09\u79cd\u4e3b\u6d41\u7684Subword\u7b97\u6cd5\uff0c\u5b83\u4eec\u5206\u522b\u662f\uff1aByte Pair Encoding (BPE)\u3001WordPiece\u548cUnigram Language Model\u3002<\/p>\n\n\n\n<h3>\u5b57\u8282\u5bf9\u7f16\u7801\uff08BPE, Byte Pair Encoding\uff09<\/h3>\n\n\n\n<p>\u5b57\u8282\u5bf9\u7f16\u7801\uff08BPE, Byte Pair Encoder\uff09\uff0c\u53c8\u79f0digram coding\u53cc\u5b57\u6bcd\u7ec4\u5408\u7f16\u7801\uff0c\u662f\u4e00\u79cd<strong>\u6570\u636e\u538b\u7f29<\/strong>\u7b97\u6cd5\uff0c\u7528\u6765\u5728\u56fa\u5b9a\u5927\u5c0f\u7684\u8bcd\u8868\u4e2d\u5b9e\u73b0\u53ef\u53d8\u2ed3\u5ea6\u7684\u5b50\u8bcd\u3002\u8be5\u7b97\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u56e0\u800c\u76ee\u524d\u5b83\u662f<strong>\u6700\u6d41\u884c<\/strong>\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>BPE\u9996\u5148\u5c06\u8bcd\u5206\u6210\u5355\u4e2a\u5b57\u7b26\uff0c<strong>\u7136\u540e\u4f9d\u6b21\u7528\u53e6\u4e00\u4e2a\u5b57\u7b26\u66ff\u6362\u9891\u7387\u6700\u9ad8<\/strong>\u7684<strong>\u4e00\u5bf9\u5b57\u7b26<\/strong>\uff0c\u76f4\u5230\u5faa\u73af\u6b21\u6570\u7ed3\u675f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/BPE\u8fc7\u7a0b.gif\" alt=\"\" class=\"wp-image-26186\" width=\"476\" height=\"266\"\/><\/figure>\n\n\n\n<p>\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecdBPE\u5728\u5206\u8bcd\u4e2d\u7684\u7b97\u6cd5\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<h4>\u7b97\u6cd5\u8fc7\u7a0b<\/h4>\n\n\n\n<ol><li>\u51c6\u5907\u8bed\u6599\u5e93\uff0c\u786e\u5b9a\u671f\u671b\u7684subword\u8bcd\u8868\u5927\u5c0f\u7b49\u53c2\u6570<\/li><li><strong>\u901a\u5e38\u5728\u6bcf\u4e2a\u5355\u8bcd\u672b\u5c3e\u6dfb\u52a0\u540e\u7f00<code>&lt;\/w><\/code><\/strong>\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u9891\u7387\uff0c\u4f8b\u5982\uff0c<code>low<\/code>\u7684\u9891\u7387\u4e3a5\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u5176\u6539\u5199\u4e3a<code>\"l o w &lt;\/ w>\u201d\uff1a5<\/code>  \u6ce8\uff1a<strong>\u505c\u6b62\u7b26<code>&lt;\/w><\/code>\u7684\u610f\u4e49\u5728\u4e8e\u6807\u660esubword\u662f\u8bcd\u540e\u7f00<\/strong>\u3002\u4e3e\u4f8b\u6765\u8bf4\uff1a<strong><code>st<\/code>\u4e0d\u52a0<code>&lt;\/w><\/code>\u53ef\u4ee5\u51fa\u73b0\u5728\u8bcd\u9996\uff0c\u5982<code>st ar<\/code>\uff1b\u52a0\u4e86<code>&lt;\/w><\/code>\u8868\u660e\u8be5\u5b50\u8bcd\u4f4d\u4e8e\u8bcd\u5c3e\uff0c\u5982<code>we st&lt;\/w><\/code>\uff0c\u4e8c\u8005\u610f\u4e49\u622a\u7136\u4e0d\u540c<\/strong><\/li><li><strong>\u5c06\u8bed\u6599\u5e93\u4e2d\u6240\u6709\u5355\u8bcd\u62c6\u5206\u4e3a\u5355\u4e2a\u5b57\u7b26\uff0c\u7528\u6240\u6709\u5355\u4e2a\u5b57\u7b26\u5efa\u7acb\u6700\u521d\u7684\u8bcd\u5178\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u9891\u7387\uff0c\u672c\u9636\u6bb5\u7684subword\u7684\u7c92\u5ea6\u662f\u5b57\u7b26<\/strong><\/li><li><strong>\u6311\u51fa\u9891\u6b21\u6700\u9ad8\u7684\u7b26\u53f7\u5bf9<\/strong>\uff0c\u6bd4\u5982\u8bf4 <code>t<\/code> \u548c <code>h<\/code> \u7ec4\u6210\u7684 <code>th<\/code>\uff0c\u5c06\u65b0\u5b57\u7b26\u52a0\u5165\u8bcd\u8868\uff0c\u7136\u540e\u5c06\u8bed\u6599\u4e2d\u6240\u6709\u8be5\u5b57\u7b26\u5bf9\u878d\u5408\uff08merge\uff09\uff0c\u5373\u6240\u6709 <code>t<\/code> \u548c<code>h<\/code> \u90fd\u53d8\u4e3a <code>th<\/code>\u3002 \u6ce8\uff1a\u65b0\u5b57\u7b26\u4f9d\u7136\u53ef\u4ee5\u53c2\u4e0e\u540e\u7eed\u7684merge\uff0c\u6709\u70b9\u7c7b\u4f3c\u54c8\u592b\u66fc\u6811\uff0cBPE\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u79cd<strong>\u8d2a\u5fc3\u7b97\u6cd5<\/strong>\u3002<\/li><li>\u91cd\u590d\u904d\u5386 2\u548c 3 \u64cd\u4f5c\uff0c\u76f4\u5230<strong>\u8bcd\u8868\u4e2d\u5355\u8bcd\u6570\u8fbe\u5230\u8bbe\u5b9a\u91cf<\/strong>\u6216<strong>\u4e0b\u4e00\u4e2a\u6700\u9ad8\u9891\u6570\u4e3a1<\/strong>\uff0c\u5982\u679c\u5df2\u7ecf\u6253\u5230\u8bbe\u5b9a\u91cf\uff0c\u5176\u4f59\u7684\u8bcd\u6c47\u76f4\u63a5\u4e22\u5f03<\/li><\/ol>\n\n\n\n<p>\u6ce8\uff1a\u770b\u4f3c\u6211\u4eec\u8981\u7ef4\u62a4\u4e24\u5f20\u8868\uff0c\u4e00\u4e2a\u8bcd\u8868\uff0c\u4e00\u4e2a\u5b57\u7b26\u8868\uff0c\u5b9e\u9645\u4e0a\u53ea\u6709\u4e00\u5f20\uff0c\u8bcd\u8868\u53ea\u662f\u4e3a\u4e86\u6211\u4eec\u65b9\u4fbf\u7406\u89e3\u3002<code>&lt;\/w><\/code> \u662f\u4e3a\u4e86\u660e\u786e subword \u662f\u5426\u662f\u8bcd\u5c3e\uff0c\u5b83\u5728\u8bad\u7ec3\u3001merge \u8fc7\u7a0b\u4e2d\u548c\u666e\u901a\u5b57\u7b26\u4e00\u6837\u5bf9\u5f85\uff0c\u5728\u6700\u7ec8\u8f93\u51fa\u65f6\u4f5c\u4e3a\u91cd\u5efa\u539f\u8bcd\u7684\u6807\u8bb0\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e3e\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u6765\u76f4\u89c2\u5730\u770b\u4e00\u4e0b\u8fd9\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<p>1\u3001\u83b7\u53d6\u8bed\u6599\u5e93\uff0c\u8fd9\u6837\u4e00\u6bb5\u8bdd\u4e3a\u4f8b\uff1a<code>\u201c<\/code><strong><em><code>FloydHub is the fastest way to build, train and deploy deep learning models. Build deep learning models in the cloud. Train deep learning models.<\/code><\/em><\/strong><code>\u201d<\/code><\/p>\n\n\n\n<p>2\u3001\u62c6\u5206\uff0c\u52a0\u540e\u7f00\uff0c\u7edf\u8ba1\u8bcd\u9891\uff1a <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"537\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/a-1024x537.png\" alt=\"\" class=\"wp-image-26195\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/a-1024x537.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/a-300x157.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/a-768x403.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/a.png 1152w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>3\u3001\u5efa\u7acb\u8bcd\u8868\uff0c\u7edf\u8ba1\u5b57\u7b26\u9891\u7387\uff08\u987a\u4fbf\u6392\u4e2a\u5e8f\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"640\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/b-1024x640.png\" alt=\"\" class=\"wp-image-26197\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/b-1024x640.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/b-300x188.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/b-768x480.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/b.png 1216w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><img src=\"https:\/\/secure2.wostatic.cn\/static\/og5NBPr9PNwthvCFhG8Pe3\/image.png?auth_key=1746941610-jBipoDNVd3jjtAqgGD81WP-0-c05d5217420f03f99266f0fcbd03ccc1\" alt=\"\"><\/p>\n\n\n\n<p>4\u3001\u4ee5\u7b2c\u4e00\u6b21\u8fed\u4ee3\u4e3a\u4f8b\uff0c\u5c06\u5b57\u7b26\u9891\u7387\u6700\u9ad8\u7684<code>d<\/code>\u548c<code>e<\/code>\u66ff\u6362\u4e3a<code>de<\/code>\uff0c\u540e\u9762\u4f9d\u6b21\u8fed\u4ee3\uff1a <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"727\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/c-1024x727.png\" alt=\"\" class=\"wp-image-26198\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/c-1024x727.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/c-300x213.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/c-768x545.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/c.png 1214w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>5\u3001\u66f4\u65b0\u8bcd\u8868 <img src=\"https:\/\/secure2.wostatic.cn\/static\/v1pV9ck7TJm2nNcCnz9q4B\/image.png?auth_key=1746941610-hfZPs4GzyUvXNDwQxC7i8D-0-2907927d4aa9c9c3a2c2177b3e72a5ee\" alt=\"\"> \u7ee7\u7eed\u8fed\u4ee3\u76f4\u5230\u8fbe\u5230\u9884\u8bbe\u7684subwords\u8bcd\u8868\u5927\u5c0f\u6216\u4e0b\u4e00\u4e2a\u6700\u9ad8\u9891\u7684\u5b57\u8282\u5bf9\u51fa\u73b0\u9891\u7387\u4e3a1\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"540\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/d-1024x540.png\" alt=\"\" class=\"wp-image-26200\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/d-1024x540.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/d-300x158.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/d-768x405.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/d.png 1152w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5982\u679c\u5c06\u8bcd\u8868\u5927\u5c0f\u8bbe\u7f6e\u4e3a10\uff0c\u6700\u7ec8\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d e\nr n\nrn i\nrni n\nrnin g&lt;\/w&gt;\no de\node l\nm odel\nl o\nl e<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u66f4\u52a0\u5408\u9002\u7684\u8bcd\u8868\uff0c\u8fd9\u4e2a\u8bcd\u8868\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u4e0d\u662f\u5355\u8bcd\u7684\u7ec4\u5408\uff0c\u4f46\u662f<strong>\u5176\u672c\u8eab\u6709\u610f\u4e49\u7684\u4e00\u79cd\u5f62\u5f0f<\/strong><\/p>\n\n\n\n<h4>BPE\u7684\u4f18\u70b9<\/h4>\n\n\n\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8bed\u6599\u5e93\u5f88\u5c0f\uff0c\u77e5\u8bc6\u4e3a\u4e86\u65b9\u4fbf\u6211\u4eec\u7406\u89e3BPE\u7684\u8fc7\u7a0b\uff0c\u4f46\u5b9e\u9645\u4e2d\u8bed\u6599\u5e93\u5f80\u5f80\u975e\u5e38\u975e\u5e38\u5927\uff0c\u65e0\u6cd5\u7ed9\u6bcf\u4e2a\u8bcd(token)\u90fd\u653e\u5728\u8bcd\u8868\u4e2d\u3002BPE\u7684\u4f18\u70b9\u5c31\u5728\u4e8e\uff0c\u53ef\u4ee5\u5f88\u6709\u6548\u5730\u5e73\u8861\u8bcd\u5178\u5927\u5c0f\u548c\u7f16\u7801\u6b65\u9aa4\u6570\uff08\u5c06\u8bed\u6599\u7f16\u7801\u6240\u9700\u8981\u7684token\u6570\u91cf\uff09\u3002<\/p>\n\n\n\n<p>\u968f\u7740\u5408\u5e76\u7684\u6b21\u6570\u589e\u52a0\uff0c\u8bcd\u8868\u5927\u5c0f\u901a\u5e38\u5148\u589e\u52a0\u540e\u51cf\u5c0f\u3002\u8fed\u4ee3\u6b21\u6570\u592a\u5c0f\uff0c\u5927\u90e8\u5206\u8fd8\u662f\u5b57\u6bcd\uff0c\u6ca1\u4ec0\u4e48\u610f\u4e49\uff1b\u8fed\u4ee3\u6b21\u6570\u591a\uff0c\u53c8\u91cd\u65b0\u53d8\u56de\u4e86\u539f\u6765\u90a3\u51e0\u4e2a\u8bcd\u3002\u6240\u4ee5\u8bcd\u8868\u5927\u5c0f\u8981\u53d6\u4e00\u4e2a\u4e2d\u95f4\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"751\" height=\"460\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-15.png\" alt=\"\" class=\"wp-image-26204\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-15.png 751w, http:\/\/139.9.1.231\/wp-content\/uploads\/2025\/05\/image-15-300x184.png 300w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><\/figure>\n\n\n\n<h4>BPE\u7684\u7f3a\u70b9<\/h4>\n\n\n\n<ul><li>\u5bf9\u4e8e\u540c\u4e00\u4e2a\u53e5\u5b50, \u4f8b\u5982Hello world\uff0c\u5982\u56fe\u6240\u793a\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684Subword\u5e8f\u5217\u3002\u4e0d\u540c\u7684Subword\u5e8f\u5217\u4f1a\u4ea7\u751f\u5b8c\u5168\u4e0d\u540c\u7684id\u5e8f\u5217\u8868\u793a\uff0c\u8fd9\u79cd\u6b67\u4e49\u53ef\u80fd\u5728\u89e3\u7801\u9636\u6bb5\u65e0\u6cd5\u89e3\u51b3\u3002\u5728\u7ffb\u8bd1\u4efb\u52a1\u4e2d\uff0c\u4e0d\u540c\u7684id\u5e8f\u5217\u53ef\u80fd\u7ffb\u8bd1\u51fa\u4e0d\u540c\u7684\u53e5\u5b50\uff0c\u8fd9\u663e\u7136\u662f\u9519\u8bef\u7684\u3002 <img src=\"https:\/\/secure2.wostatic.cn\/static\/x7PXqiQrETsULpEtMgprAP\/image.png?auth_key=1746941610-58oku1kFjmWEwbsSqMx3e7-0-dea4cd6b1cd425813758a19165e62ff1\" alt=\"\"><\/li><li>\u5728\u8bad\u7ec3\u4efb\u52a1\u4e2d\uff0c\u5982\u679c\u80fd\u5bf9\u4e0d\u540c\u7684Subword\u8fdb\u884c\u8bad\u7ec3\u7684\u8bdd\uff0c\u5c06\u589e\u52a0\u6a21\u578b\u7684\u5065\u58ee\u6027\uff0c\u80fd\u591f\u5bb9\u5fcd\u66f4\u591a\u7684\u566a\u58f0\uff0c\u800cBPE\u7684\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u5bf9\u968f\u673a\u5206\u5e03\u8fdb\u884c\u5b66\u4e60\u3002<\/li><\/ul>\n\n\n\n<p>\u4e2a\u4eba\u7406\u89e3\uff1a\u6211\u611f\u89c9\u7f3a\u70b9\u76f4\u63a5\u53ef\u4ee5\u5ffd\u7565<\/p>\n\n\n\n<h4>BPE\u7684\u9002\u7528\u8303\u56f4<\/h4>\n\n\n\n<p>BPE\u4e00\u822c\u9002\u7528\u5728\u6b27\u7f8e\u8bed\u8a00\u62c9\u4e01\u8bed\u7cfb\u4e2d\uff0c\u56e0\u4e3a\u6b27\u7f8e\u8bed\u8a00\u5927\u591a\u662f\u5b57\u7b26\u5f62\u5f0f\uff0c\u6d89\u53ca\u524d\u7f00\u3001\u540e\u7f00\u7684\u5355\u8bcd\u6bd4\u8f83\u591a\u3002\u800c\u4e2d\u6587\u7684\u6c49\u5b57\u4e00\u822c\u4e0d\u7528BPE\u8fdb\u884c\u7f16\u7801\uff0c\u56e0\u4e3a\u4e2d\u6587\u662f\u5b57\u65e0\u6cd5\u8fdb\u884c\u62c6\u5206\u3002\u5bf9\u4e2d\u6587\u7684\u5904\u7406\u901a\u5e38\u53ea\u6709\u5206\u8bcd\u548c\u5206\u5b57\u4e24\u79cd\u3002\u7406\u8bba\u4e0a\u5206\u8bcd\u6548\u679c\u66f4\u597d\uff0c\u66f4\u597d\u7684\u533a\u522b\u8bed\u4e49\u3002\u5206\u5b57\u6548\u7387\u9ad8\u3001\u7b80\u6d01\uff0c\u56e0\u4e3a\u5e38\u7528\u7684\u5b57\u4e0d\u8fc73000\u5b57\uff0c\u8bcd\u8868\u66f4\u52a0\u7b80\u77ed\u3002<\/p>\n\n\n\n<h3>BPE\u7684\u5b9e\u73b0<\/h3>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re, collections\n\ndef get_vocab(filename):\n    vocab = collections.defaultdict(int)\n    with open(filename, 'r', encoding='utf-8') as fhand:\n        for line in fhand:\n            words = line.strip().split()\n            for word in words:\n                vocab&#091;' '.join(list(word)) + ' &lt;\/w&gt;'] += 1\n    return vocab\n\ndef get_stats(vocab):\n    pairs = collections.defaultdict(int)\n    for word, freq in vocab.items():\n        symbols = word.split()\n        for i in range(len(symbols)-1):\n            pairs&#091;symbols&#091;i],symbols&#091;i+1]] += freq\n    return pairs\n\ndef merge_vocab(pair, v_in):\n    v_out = {}\n    bigram = re.escape(' '.join(pair))\n    p = re.compile(r'(?&lt;!\\S)' + bigram + r'(?!\\S)')\n    for word in v_in:\n        w_out = p.sub(''.join(pair), word)\n        v_out&#091;w_out] = v_in&#091;word]\n    return v_out\n\ndef get_tokens(vocab):\n    tokens = collections.defaultdict(int)\n    for word, freq in vocab.items():\n        word_tokens = word.split()\n        for token in word_tokens:\n            tokens&#091;token] += freq\n    return tokens<\/code><\/pre>\n\n\n\n<p>\u8dd1\u4e00\u4e2a\u4f8b\u5b50\u8bd5\u4e00\u4e0b\uff0c\u8fd9\u91cc\u5df2\u7ecf\u5bf9\u539f\u53e5\u5b50\u8fdb\u884c\u4e86\u9884\u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vocab = {'l o w &lt;\/w&gt;': 5, 'l o w e r &lt;\/w&gt;': 2, 'n e w e s t &lt;\/w&gt;': 6, 'w i d e s t &lt;\/w&gt;': 3}\nprint('==========')\nprint('Tokens Before BPE')\ntokens = get_tokens(vocab)\nprint('Tokens: {}'.format(tokens))\nprint('Number of tokens: {}'.format(len(tokens)))\nprint('==========')\n\nnum_merges = 5\nfor i in range(num_merges):\n    pairs = get_stats(vocab)\n    if not pairs:\n        break\n    best = max(pairs, key=pairs.get)\n    vocab = merge_vocab(best, vocab)\n    print('Iter: {}'.format(i))\n    print('Best pair: {}'.format(best))\n    tokens = get_tokens(vocab)\n    print('Tokens: {}'.format(tokens))\n    print('Number of tokens: {}'.format(len(token<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>==========\nTokens Before BPE\nTokens: defaultdict(&lt;class 'int'&gt;, {'l': 7, 'o': 7, 'w': 16, '&lt;\/w&gt;': 16, 'e': 17, 'r': 2, 'n': 6, 's': 9, 't': 9, 'i': 3, 'd': 3})\nNumber of tokens: 11\n==========\nIter: 0\nBest pair: ('e', 's')\nTokens: defaultdict(&lt;class 'int'&gt;, {'l': 7, 'o': 7, 'w': 16, '&lt;\/w&gt;': 16, 'e': 8, 'r': 2, 'n': 6, 'es': 9, 't': 9, 'i': 3, 'd': 3})\nNumber of tokens: 11\n==========\nIter: 1\nBest pair: ('es', 't')\nTokens: defaultdict(&lt;class 'int'&gt;, {'l': 7, 'o': 7, 'w': 16, '&lt;\/w&gt;': 16, 'e': 8, 'r': 2, 'n': 6, 'est': 9, 'i': 3, 'd': 3})\nNumber of tokens: 10\n==========\nIter: 2\nBest pair: ('est', '&lt;\/w&gt;')\nTokens: defaultdict(&lt;class 'int'&gt;, {'l': 7, 'o': 7, 'w': 16, '&lt;\/w&gt;': 7, 'e': 8, 'r': 2, 'n': 6, 'est&lt;\/w&gt;': 9, 'i': 3, 'd': 3})\nNumber of tokens: 10\n==========\nIter: 3\nBest pair: ('l', 'o')\nTokens: defaultdict(&lt;class 'int'&gt;, {'lo': 7, 'w': 16, '&lt;\/w&gt;': 7, 'e': 8, 'r': 2, 'n': 6, 'est&lt;\/w&gt;': 9, 'i': 3, 'd': 3})\nNumber of tokens: 9\n==========\nIter: 4\nBest pair: ('lo', 'w')\nTokens: defaultdict(&lt;class 'int'&gt;, {'low': 7, '&lt;\/w&gt;': 7, 'e': 8, 'r': 2, 'n': 6, 'w': 9, 'est&lt;\/w&gt;': 9, 'i': 3, 'd': 3})\nNumber of tokens: 9\n==========<\/code><\/pre>\n\n\n\n<h4>\u7f16\u7801\u4e0e\u89e3\u7801<\/h4>\n\n\n\n<p>\u4e0a\u9762\u7684\u8fc7\u7a0b\u79f0\u4e3a\u7f16\u7801\u3002\u89e3\u7801\u8fc7\u7a0b\u6bd4\u8f83\u7b80\u5355\uff0c\u5982\u679c\u76f8\u90bb\u5b50\u8bcd\u95f4\u6ca1\u6709\u4e2d\u6b62\u7b26\uff0c\u5219\u5c06\u4e24\u5b50\u8bcd\u76f4\u63a5\u62fc\u63a5\uff0c\u5426\u5219\u4e24\u5b50\u8bcd\u4e4b\u95f4\u6dfb\u52a0\u5206\u9694\u7b26\u3002 \u5982\u679c\u4ecd\u7136\u6709\u5b50\u5b57\u7b26\u4e32\u6ca1\u88ab\u66ff\u6362\u4f46\u6240\u6709token\u90fd\u5df2\u8fed\u4ee3\u5b8c\u6bd5\uff0c\u5219\u5c06\u5269\u4f59\u7684\u5b50\u8bcd\u66ff\u6362\u4e3a\u7279\u6b8atoken\uff0c\u5982<code>&lt;unk&gt;<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7f16\u7801\u5e8f\u5217\n&#091;\"the&lt;\/w&gt;\", \"high\", \"est&lt;\/w&gt;\", \"moun\", \"tain&lt;\/w&gt;\"]\n\n# \u89e3\u7801\u5e8f\u5217\n\"the&lt;\/w&gt; highest&lt;\/w&gt; mountain&lt;\/w&gt;\"<\/code><\/pre>\n\n\n\n<h4>\u5982\u4f55\u8c03\u5305\u4f7f\u7528BPE<\/h4>\n\n\n\n<p><strong>BPE \u53ef\u4ee5\u76f4\u63a5\u7528\u6700\u7ecf\u5178\u7684 <a href=\"https:\/\/link.zhihu.com\/?target=https:\/\/github.com\/rsennrich\/subword-nmt\">subword-nmt<\/a> \u5305\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u5b9e\u73b0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.wolai.com\/suprit\/heU7bADzdRaJje1jdeoVLp htt &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2025\/05\/11\/bpe-subword-nmt\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">BPE\u5206\u8bcd\u7b97\u6cd5-\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,4,38,1,34],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/25947"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=25947"}],"version-history":[{"count":55,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/25947\/revisions"}],"predecessor-version":[{"id":26212,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/25947\/revisions\/26212"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=25947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=25947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=25947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}