{"id":1848,"date":"2025-02-18T20:59:07","date_gmt":"2025-02-18T12:59:07","guid":{"rendered":"https:\/\/www.forillusion.com\/?p=1848"},"modified":"2025-02-18T20:59:08","modified_gmt":"2025-02-18T12:59:08","slug":"6-3-lang-model-dataset","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/6-3-lang-model-dataset\/","title":{"rendered":"6.3 \u8bed\u8a00\u6a21\u578b\u6570\u636e\u96c6"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfb\u53d6\u6570\u636e\u96c6<\/h2>\n\n\n\n<p>\u9996\u5148\u8bfb\u53d6\u8fd9\u4e2a\u6570\u636e\u96c6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nimport random\nimport zipfile\n\nwith zipfile.ZipFile('\/data\/jaychou_lyrics.txt.zip') as zin:\n    with zin.open('jaychou_lyrics.txt') as f:\n        corpus_chars = f.read().decode('utf-8')<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u6570\u636e\u96c6\u67096\u4e07\u591a\u4e2a\u5b57\u7b26\u3002\u628a\u6362\u884c\u7b26\u66ff\u6362\u6210\u7a7a\u683c\uff0c\u7136\u540e\u4ec5\u4f7f\u7528\u524d1\u4e07\u4e2a\u5b57\u7b26\u6765\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>corpus_chars = corpus_chars.replace('\\n', ' ').replace('\\r', ' ')\ncorpus_chars = corpus_chars&#91;0:10000]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5efa\u7acb\u5b57\u7b26\u7d22\u5f15<\/h2>\n\n\n\n<p>\u5c06\u6bcf\u4e2a\u5b57\u7b26\u6620\u5c04\u6210\u4e00\u4e2a\u4ece0\u5f00\u59cb\u7684\u8fde\u7eed\u6574\u6570\uff0c\u53c8\u79f0\u7d22\u5f15\uff0c\u6765\u65b9\u4fbf\u4e4b\u540e\u7684\u6570\u636e\u5904\u7406\u3002\u4e3a\u4e86\u5f97\u5230\u7d22\u5f15\uff0c\u5c06\u6570\u636e\u96c6\u91cc\u6240\u6709\u4e0d\u540c\u5b57\u7b26\u53d6\u51fa\u6765\uff0c\u7136\u540e\u5c06\u5176\u9010\u4e00\u6620\u5c04\u5230\u7d22\u5f15\u6765\u6784\u9020\u8bcd\u5178\u3002\u63a5\u7740\uff0c\u6253\u5370<code>vocab_size<\/code>\uff0c\u5373\u8bcd\u5178\u4e2d\u4e0d\u540c\u5b57\u7b26\u7684\u4e2a\u6570\uff0c\u53c8\u79f0\u8bcd\u5178\u5927\u5c0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>idx_to_char = list(set(corpus_chars))\nchar_to_idx = dict(&#91;(char, i) for i, char in enumerate(idx_to_char)])\nvocab_size = len(char_to_idx)\nvocab_size # 1027<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\uff0c\u5c06\u8bad\u7ec3\u6570\u636e\u96c6\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u5316\u4e3a\u7d22\u5f15\uff0c\u5e76\u6253\u5370\u524d20\u4e2a\u5b57\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u7d22\u5f15\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>corpus_indices = &#91;char_to_idx&#91;char] for char in corpus_chars] # \u5c06\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u5316\u4e3a\u7d22\u5f15\uff0c\u5f97\u5230\u4e00\u4e2a\u7d22\u5f15\u7684\u5e8f\u5217\nsample = corpus_indices&#91;:20]\nprint('chars:', ''.join(&#91;idx_to_char&#91;idx] for idx in sample]))\nprint('indices:', sample)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chars: \u60f3\u8981\u6709\u76f4\u5347\u673a \u60f3\u8981\u548c\u4f60\u98de\u5230\u5b87\u5b99\u53bb \u60f3\u8981\u548c\nindices: &#91;250, 164, 576, 421, 674, 653, 357, 250, 164, 850, 217, 910, 1012, 261, 275, 366, 357, 250, 164, 850]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u65f6\u5e8f\u6570\u636e\u7684\u91c7\u6837<\/h2>\n\n\n\n<p>\u5728\u8bad\u7ec3\u4e2d\u9700\u8981\u6bcf\u6b21\u968f\u673a\u8bfb\u53d6\u5c0f\u6279\u91cf\u6837\u672c\u548c\u6807\u7b7e\u3002\u65f6\u5e8f\u6570\u636e\u7684\u4e00\u4e2a\u6837\u672c\u901a\u5e38\u5305\u542b\u8fde\u7eed\u7684\u5b57\u7b26\u3002\u6837\u672c\u7684\u6807\u7b7e\u5e8f\u5217\u4e3a\u8fd9\u4e9b\u5b57\u7b26\u5206\u522b\u5728\u8bad\u7ec3\u96c6\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u5bf9\u65f6\u5e8f\u6570\u636e\u8fdb\u884c\u91c7\u6837\uff0c\u5206\u522b\u662f\u968f\u673a\u91c7\u6837\u548c\u76f8\u90bb\u91c7\u6837\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u968f\u673a\u91c7\u6837<\/h3>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u6bcf\u6b21\u4ece\u6570\u636e\u91cc\u968f\u673a\u91c7\u6837\u4e00\u4e2a\u5c0f\u6279\u91cf\u3002\u5176\u4e2d\u6279\u91cf\u5927\u5c0f<code>batch_size<\/code>\u6307\u6bcf\u4e2a\u5c0f\u6279\u91cf\u7684\u6837\u672c\u6570\uff0c<code>num_steps<\/code>\u4e3a\u6bcf\u4e2a\u6837\u672c\u6240\u5305\u542b\u7684\u65f6\u95f4\u6b65\u6570\u3002<br>\u5728\u968f\u673a\u91c7\u6837\u4e2d\uff0c\u6bcf\u4e2a\u6837\u672c\u662f\u539f\u59cb\u5e8f\u5217\u4e0a\u4efb\u610f\u622a\u53d6\u7684\u4e00\u6bb5\u5e8f\u5217\u3002\u76f8\u90bb\u7684\u4e24\u4e2a\u968f\u673a\u5c0f\u6279\u91cf\u5728\u539f\u59cb\u5e8f\u5217\u4e0a\u7684\u4f4d\u7f6e\u4e0d\u4e00\u5b9a\u76f8\u6bd7\u90bb\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u7528\u4e00\u4e2a\u5c0f\u6279\u91cf\u6700\u7ec8\u65f6\u95f4\u6b65\u7684\u9690\u85cf\u72b6\u6001\u6765\u521d\u59cb\u5316\u4e0b\u4e00\u4e2a\u5c0f\u6279\u91cf\u7684\u9690\u85cf\u72b6\u6001\u3002\u5728\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u6bcf\u6b21\u968f\u673a\u91c7\u6837\u524d\u90fd\u9700\u8981\u91cd\u65b0\u521d\u59cb\u5316\u9690\u85cf\u72b6\u6001\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def data_iter_random(corpus_indices, batch_size, num_steps, device=None): # corpus_indices: \u5b57\u7b26\u7d22\u5f15\u5e8f\u5217\uff0cbatch_size: \u6279\u91cf\u5927\u5c0f\uff0cnum_steps: \u65f6\u95f4\u6b65\u6570\n    # \u51cf1\u662f\u56e0\u4e3a\u8f93\u51fa\u7684\u7d22\u5f15x\u662f\u76f8\u5e94\u8f93\u5165\u7684\u7d22\u5f15y\u52a01\n    num_examples = (len(corpus_indices) - 1) \/\/ num_steps # \u5148\u83b7\u53d6\u8bed\u6599\u5e93\u7684\u603b\u957f\u5ea6\uff0c\u518d\u5c06\u8bed\u6599\u5e93\u5206\u5272\u6210\u591a\u4e2a\u957f\u5ea6\u4e3a num_steps \u7684\u5b50\u5e8f\u5217\uff0c\u8ba1\u7b97\u53ef\u4ee5\u5f97\u5230\u591a\u5c11\u4e2a\u8fd9\u6837\u7684\u5b8c\u6574\u5b50\u5e8f\u5217\u3002\n    epoch_size = num_examples \/\/ batch_size # \u5c06\u603b\u7684\u5b50\u5e8f\u5217\u6570\u91cf\u9664\u4ee5\u6279\u91cf\u5927\u5c0f\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u8868\u793a\u6bcf\u4e2a\u6279\u6b21\u4e2d\u5305\u542b\u7684\u5b50\u5e8f\u5217\u6570\u91cf\u3002\n    example_indices = list(range(num_examples)) # \u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b50\u5e8f\u5217\u7684\u4e0b\u6807\u7684\u5217\u8868\n    random.shuffle(example_indices) # \u5c06\u8fd9\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u968f\u673a\u6253\u4e71\uff0c\u4ee5\u4fbf\u4e4b\u540e\u968f\u673a\u8bfb\u53d6\u5c0f\u6279\u91cf\u6570\u636e\u6837\u672c\n\n    # \u8fd4\u56de\u4ecepos\u5f00\u59cb\u7684\u957f\u4e3anum_steps\u7684\u5e8f\u5217\n    def _data(pos):\n        return corpus_indices&#91;pos: pos + num_steps] # \u8fd4\u56de\u4ece pos \u5f00\u59cb\u7684\u957f\u4e3a num_steps \u7684\u5e8f\u5217\n    if device is None:\n        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u8bbe\u5907\uff0c\u5c31\u4f7f\u7528 GPU\n\n    for i in range(epoch_size):\n        # \u6bcf\u6b21\u8bfb\u53d6batch_size\u4e2a\u968f\u673a\u6837\u672c\n        i = i * batch_size # \u4ece i * batch_size \u5f00\u59cb\n        batch_indices = example_indices&#91;i: i + batch_size] # \u4ece\u968f\u673a\u6253\u4e71\u7684\u7d22\u5f15\u5217\u8868 example_indices \u4e2d\u53d6\u51fa\u5f53\u524d\u6279\u6b21\u7684\u7d22\u5f15\n        X = &#91;_data(j * num_steps) for j in batch_indices] # \u83b7\u53d6\u5f53\u524d\u6279\u6b21\u6bcf\u4e00\u4e2a\u6837\u672c\u7684\u8f93\u5165\u5e8f\u5217\n        Y = &#91;_data(j * num_steps + 1) for j in batch_indices] # \u83b7\u53d6\u5f53\u524d\u6279\u6b21\u6bcf\u4e00\u4e2a\u6837\u672c\u7684\u6807\u7b7e\uff0c\u5373\u6bcf\u4e00\u4e2a\u6837\u672c\u7684\u8f93\u5165\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\n\n        # \u5c06\u8f93\u5165\u5e8f\u5217 X \u548c\u76ee\u6807\u5e8f\u5217 Y \u8f6c\u6362\u4e3a PyTorch \u5f20\u91cf\u3002\n        # \u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u4e3a float32\uff0c\u5e76\u79fb\u52a8\u5230\u6307\u5b9a\u7684\u8bbe\u5907\uff08CPU \u6216 GPU\uff09\u3002\n        # \u4f7f\u7528 yield \u8fd4\u56de\u5f53\u524d\u6279\u6b21\u7684\u6570\u636e\uff0c\u5f62\u6210\u4e00\u4e2a\u751f\u6210\u5668\u3002\n        yield torch.tensor(X, dtype=torch.float32, device=device), torch.tensor(Y, dtype=torch.float32, device=device) <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f8\u90bb\u91c7\u6837<\/h3>\n\n\n\n<p>\u9664\u5bf9\u539f\u59cb\u5e8f\u5217\u505a\u968f\u673a\u91c7\u6837\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4ee4\u76f8\u90bb\u7684\u4e24\u4e2a\u968f\u673a\u5c0f\u6279\u91cf\u5728\u539f\u59cb\u5e8f\u5217\u4e0a\u7684\u4f4d\u7f6e\u76f8\u6bd7\u90bb\u3002\u8fd9\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u7528\u4e00\u4e2a\u5c0f\u6279\u91cf\u6700\u7ec8\u65f6\u95f4\u6b65\u7684\u9690\u85cf\u72b6\u6001\u6765\u521d\u59cb\u5316\u4e0b\u4e00\u4e2a\u5c0f\u6279\u91cf\u7684\u9690\u85cf\u72b6\u6001\uff0c\u4ece\u800c\u4f7f\u4e0b\u4e00\u4e2a\u5c0f\u6279\u91cf\u7684\u8f93\u51fa\u4e5f\u53d6\u51b3\u4e8e\u5f53\u524d\u5c0f\u6279\u91cf\u7684\u8f93\u5165\uff0c\u5e76\u5982\u6b64\u5faa\u73af\u4e0b\u53bb\u3002\u8fd9\u5bf9\u5b9e\u73b0\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u9020\u6210\u4e86\u4e24\u65b9\u9762\u5f71\u54cd\uff1a\u4e00\u65b9\u9762\uff0c<br>\u5728\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u5728\u6bcf\u4e00\u4e2a\u8fed\u4ee3\u5468\u671f\u5f00\u59cb\u65f6\u521d\u59cb\u5316\u9690\u85cf\u72b6\u6001\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5f53\u591a\u4e2a\u76f8\u90bb\u5c0f\u6279\u91cf\u901a\u8fc7\u4f20\u9012\u9690\u85cf\u72b6\u6001\u4e32\u8054\u8d77\u6765\u65f6\uff0c\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\u8ba1\u7b97\u5c06\u4f9d\u8d56\u6240\u6709\u4e32\u8054\u8d77\u6765\u7684\u5c0f\u6279\u91cf\u5e8f\u5217\u3002\u540c\u4e00\u8fed\u4ee3\u5468\u671f\u4e2d\uff0c\u968f\u7740\u8fed\u4ee3\u6b21\u6570\u7684\u589e\u52a0\uff0c\u68af\u5ea6\u7684\u8ba1\u7b97\u5f00\u9500\u4f1a\u8d8a\u6765\u8d8a\u5927\u3002<br>\u4e3a\u4e86\u4f7f\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\u8ba1\u7b97\u53ea\u4f9d\u8d56\u4e00\u6b21\u8fed\u4ee3\u8bfb\u53d6\u7684\u5c0f\u6279\u91cf\u5e8f\u5217\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u8bfb\u53d6\u5c0f\u6279\u91cf\u524d\u5c06\u9690\u85cf\u72b6\u6001\u4ece\u8ba1\u7b97\u56fe\u4e2d\u5206\u79bb\u51fa\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def data_iter_consecutive(corpus_indices, batch_size, num_steps, device=None):\n    if device is None:\n        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u8bbe\u5907\uff0c\u5c31\u4f7f\u7528 GPU\n    corpus_indices = torch.tensor(corpus_indices, dtype=torch.float32, device=device) # \u5c06\u8bed\u6599\u5e93\u8f6c\u6362\u4e3a\u5f20\u91cf\n    data_len = len(corpus_indices) # \u83b7\u53d6\u8bed\u6599\u5e93\u7684\u603b\u957f\u5ea6\n    batch_len = data_len \/\/ batch_size # \u8ba1\u7b97\u6bcf\u4e2a\u6279\u6b21\u4e2d\u7684\u5b50\u5e8f\u5217\u957f\u5ea6\n\n    # corpus_indices&#91;0: batch_size * batch_len] \u622a\u53d6\u8bed\u6599\u5e93\u7684\u524d batch_size * batch_len \u4e2a\u5143\u7d20\uff0c\u786e\u4fdd\u6570\u636e\u53ef\u4ee5\u88ab\u5747\u5300\u5206\u5272\u3002\n    # view(batch_size, batch_len) \u5c06\u5f20\u91cf\u7684\u5f62\u72b6\u6539\u4e3a batch_size \u884c\uff0cbatch_len \u5217\u3002\n    indices = corpus_indices&#91;0: batch_size*batch_len].view(batch_size, batch_len)\n\n    epoch_size = (batch_len - 1) \/\/ num_steps # \u8ba1\u7b97\u6bcf\u4e2a\u8fed\u4ee3\u5468\u671f\u4e2d\u5305\u542b\u7684\u6279\u6b21\u6570\u91cf\n    for i in range(epoch_size): \n        i = i * num_steps\n        X = indices&#91;:, i: i + num_steps] # \u9009\u4e2d\u6bcf\u4e00\u884c\u7684\u7b2c i \u5230 i + num_steps \u4e2a\u5b57\u7b26\u4f5c\u4e3a\u8f93\u5165\n        Y = indices&#91;:, i + 1: i + num_steps + 1] # \u9009\u4e2d\u6bcf\u4e00\u884c\u7684\u7b2c i + 1 \u5230 i + num_steps + 1 \u4e2a\u5b57\u7b26\u4f5c\u4e3a\u6807\u7b7e\n        yield X, Y<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfb\u53d6\u6570\u636e\u96c6 \u9996\u5148\u8bfb\u53d6\u8fd9\u4e2a\u6570\u636e\u96c6 \u8fd9\u4e2a\u6570\u636e\u96c6\u67096\u4e07\u591a\u4e2a\u5b57\u7b26\u3002\u628a\u6362\u884c\u7b26\u66ff\u6362\u6210\u7a7a\u683c\uff0c\u7136\u540e\u4ec5\u4f7f\u7528\u524d1\u4e07\u4e2a\u5b57\u7b26\u6765\u8bad\u7ec3\u6a21\u578b\u3002 \u5efa\u7acb\u5b57\u7b26\u7d22\u5f15 \u5c06 &#8230;<\/p>","protected":false},"author":1,"featured_media":1849,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[45,44,12,22],"class_list":["post-1848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-46","category-3","tag-45","tag-44","tag-12","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1848"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1848\/revisions"}],"predecessor-version":[{"id":1850,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1848\/revisions\/1850"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1849"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}