{"id":263,"date":"2021-12-16T17:38:24","date_gmt":"2021-12-16T09:38:24","guid":{"rendered":"http:\/\/139.9.1.231\/?p=263"},"modified":"2021-12-20T15:51:08","modified_gmt":"2021-12-20T07:51:08","slug":"torchtext-vocab","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/16\/torchtext-vocab\/","title":{"rendered":"torchtext.vocab\u7684\u5b66\u4e60"},"content":{"rendered":"\n<p><strong>NLP\u5e38\u89c1\u7684\u6570\u636e\u9884\u5904\u7406\u5de5\u4f5c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ol><li>Load File\uff1a\u6570\u636e\u6587\u4ef6\u52a0\u8f7d\uff1b<\/li><li>Tokenization\uff1a\u5206\u8bcd\uff1b<\/li><li>Create Vocabulary\uff1a\u521b\u5efa\u5b57\u5178;<\/li><li>Indexify\uff1a\u5c06\u8bcd\u4e0e\u7d22\u5f15\u8fdb\u884c\u6620\u5c04;<\/li><li>Word Vectors\uff1a\u521b\u5efa\u6216\u52a0\u8f7d\u8bcd\u5411\u91cf\uff1b<\/li><li>Padding or Fix Length\uff1a\u6309\u957f\u5ea6\u5bf9\u6587\u672c\u8fdb\u884c\u8865\u9f50\u6216\u622a\u53d6\uff1b<\/li><li>Dataset Splits\uff1a\u5212\u5206\u6570\u636e\u96c6\uff08\u5982\u5c06\u6570\u636e\u96c6\u5212\u5206\u95ee\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u3001\u6d4b\u8bd5\u96c6\uff09\uff1b<\/li><li>Batching and Iterators\uff1a\u5c06\u6570\u636e\u96c6\u6309\u56fa\u5b9a\u5927\u5c0f\u5212\u5206\u6210Batch\uff1b<\/li><\/ol>\n\n\n\n<p><strong>\u4f7f\u7528<\/strong><strong>torchtext<\/strong><strong>\u5b8c\u6210\u4ee5\u4e0a\u5de5\u4f5c\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u4f7f\u7528torchtext.data.Field\u5b9a\u4e49\u6837\u672c\u5404\u4e2a\u5b57\u6bb5\u7684\u5904\u7406\u6d41\u7a0b\uff08\u5206\u8bcd\u3001\u6570\u636e\u9884\u5904\u7406\u7b49\uff09\uff1b<\/li><li>\u4f7f\u7528torchtext.data.Example\u5c06torchtext.data.Field\u5904\u7406\u6210\u4e00\u6761\u6837\u672c\uff1b<\/li><li>\u4f7f\u7528torchtext.data.Dataset\u5c06torchtext.data.Example\u5904\u7406\u6210\u6570\u636e\u96c6\uff0c\u4e5f\u53ef\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u5212\u5206\u7b49\u5de5\u4f5c\uff1b<\/li><li>\u4f7f\u7528torchtext.data.Iterators\u5c06torchtext.data.Dataset\u6309\u7167batch_size\u7ec4\u88c5\u6210Batch\u4f9b\u6a21\u578b\u8bad\u7ec3\u4f7f\u7528\uff1b<\/li><li>\u4f7f\u7528torchtext.data.vocab\u548ctorchtext.data.Vectors\u521b\u5efa\u8bcd\u5178\u3001\u8bcd\u548c\u7d22\u5f15\u7684\u4e00\u4e00\u5bf9\u5e94\u3001\u4e0b\u8f7d\u6216\u4f7f\u7528\u9884\u8bad\u7ec3\u7684\u8bcd\u5411\u91cf\u7b49\uff1b<\/li><\/ul>\n\n\n\n<h2> <li>vocab<\/li> <\/h2>\n\n\n\n<p><strong><span class=\"has-inline-color has-medium-pink-color\">\u4e00\u53e5\u8bdd\u6982\u62ec<\/span><\/strong>\uff1a<strong><span class=\"has-inline-color has-medium-pink-color\">\u4e3b\u8981\u662f\u7528\u6765\u5efa\u7acb\u8bcd\u6c47\u8868<span style=\"font-size: revert;\">\u521b\u5efa\u8bcd\u5178\u3001\u8bcd\u548c\u7d22\u5f15\u7684\u4e00\u4e00\u5bf9\u5e94\u3001\u4e0b\u8f7d\u6216\u4f7f\u7528\u9884\u8bad\u7ec3\u7684\u8bcd\u5411\u91cf\u7b49<\/span><\/span><\/strong><span style=\"font-size: revert;\">\uff1b<\/span> <\/p>\n\n\n\n<p>\u5e38\u89c1\u7684\u8bcd\u5d4c\u5165\u6a21\u578b\uff1aword2vec  Glove<\/p>\n\n\n\n<h2>Pretrained Word Embeddings<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CLASS torchtext.vocab.GloVe(name='840B', dim=300, **kwargs)\n\nCLASS torchtext.vocab.FastText(language='en', **kwargs)\n\nCLASS torchtext.vocab.ChaarNGram(**kwargs)<\/code><\/pre>\n\n\n\n<p><strong>\u8fd4\u56de\u7684\u5b9e\u4f8b\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u4e2a\u5c5e\u6027\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>stoi<\/code>: \u8bcd\u5230\u7d22\u5f15\u7684\u5b57\u5178\uff1a<\/li><li><code>itos<\/code>: \u4e00\u4e2a\u5217\u8868\uff0c\u7d22\u5f15\u5230\u8bcd\u7684\u6620\u5c04\uff1b<\/li><li><code>vectors<\/code>: \u8bcd\u5411\u91cf\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u901a\u8fc7\u4e0a\u9762\u7684\u6a21\u5757\uff0c\u5b9e\u73b0\u7531\u8bcd\u5230\u5411\u91cf\u4e4b\u95f4\u7684\u8f6c\u6362<\/strong>\uff01\uff01\uff01<\/p>\n\n\n\n<h2>vocab.Vocab \u662f\u4e00\u4e2a\u8bcd\u6c47\u8868\u5bf9\u8c61\uff08\u7531 \u4e0b\u9762\u7684vocab \u751f\u6210 Vocab \u5bf9\u8c61\uff09\uff0c\u4f7f\u7528counter\u521b\u5efa\u8bcd\u6c47\u8868<\/h2>\n\n\n\n<p>  \u4ece <strong>collections.Counter<\/strong> \u6784\u5efa\u8bcd\u6c47\u8868 <\/p>\n\n\n\n<p><em>class&nbsp;<\/em><code>collections.<\/code><code>Counter<\/code>([<em>iterable-or-mapping<\/em>])<a href=\"https:\/\/docs.python.org\/zh-cn\/3.10\/library\/collections.html#collections.Counter\"><\/a><\/p>\n\n\n\n<p>\u4e00\u4e2a\u00a0<a href=\"https:\/\/docs.python.org\/zh-cn\/3.10\/library\/collections.html#collections.Counter\"><code>Counter<\/code><\/a>\u00a0\u662f\u4e00\u4e2a\u00a0<a href=\"https:\/\/docs.python.org\/zh-cn\/3.10\/library\/stdtypes.html#dict\"><code>dict<\/code><\/a>\u00a0\u7684\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u5b83\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u5143\u7d20\u50cf\u5b57\u5178\u952e(key)\u4e00\u6837\u5b58\u50a8\uff0c\u5b83\u4eec\u7684\u8ba1\u6570\u5b58\u50a8\u4e3a\u503c\u3002\u8ba1\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6574\u6570\u503c\uff0c\u5305\u62ec0\u548c\u8d1f\u6570\u3002\u00a0<a href=\"https:\/\/docs.python.org\/zh-cn\/3.10\/library\/collections.html#collections.Counter\"><code>Counter<\/code><\/a>\u00a0\u7c7b\u6709\u70b9\u50cf\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684 bags\u6216multisets\u3002<code>Counter<\/code>\u5b9e\u9645\u4e0a\u4e5f\u662f<code>dict<\/code>\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u4e4b\u4e0d\u591a\u53ef\u4ee5\u7edf\u8ba1\u4e0d\u540c\u7684\u503c\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CLASS torchtext.vocab.Vocab(counter, max_size=None, min_freq=1, \n  specials=('&lt;unk&gt;', '&lt;pad&gt;'), vectors=None, unk_init=None, vectors_cache=None,\n  specials_first=True)\n  \u5b9a\u4e49\u8bcd\u6c47\u8868\u3002\u5c5e\u6027\uff1aVocab.freqs, Vocab.stoi, Vocab.itos\n  __init__(counter,...)\n      \u4ece collections.Counter \u6784\u5efa\u8bcd\u6c47\u8868\n  load_vectors(vectors, **kwargs)\n      vectors - GloVe, CharNGram, Vectors \u5b9e\u4f8b\uff0c\u6216\u8005\u53ef\u7528\u7684\u9884\u8bad\u7ec3\u5411\u91cf\u3002\n  set_vectors(stoi, vectors, dim, unk_init=...)\n      \u4ece\u4e00\u4e2a\u5f20\u91cf\u96c6\u5408\u4e2d\u8bbe\u7f6e\u8bcd\u6c47\u8868\u5b9e\u4f8b\u7684\u5411\u91cf\u3002\n      stoi - \u5b57\u7b26\u4e32 \u5230 `vectors` \u76f8\u5e94\u5411\u91cf\u7684\u7d22\u5f15 \u7684\u5b57\u5178\n      vectors - \u8be5\u53c2\u6570\u652f\u6301\u7d22\u5f15 __getitem__\u3002\u8f93\u5165\u4e00\u4e2a\u7d22\u5f15\u503c\uff0c\u8fd4\u56de\u7d22\u5f15\u5bf9\u5e94\u8bcd\u6761\u7684\u5411\u91cf\uff08FloatTensor\uff09\u3002\n            \u4f8b\u5982\uff1avector&#91;stoi&#91;\"string\"]] \u5e94\u8be5\u8fd4\u56de \u201cstring\" \u7684\u8bcd\u5411\u91cf\u3002\n      dim - \u8bcd\u5411\u91cf\u7684\u7ef4\u5ea6<\/code><\/pre>\n\n\n\n<h2> <code>torchtext.vocab.vocab<\/code> \u4f7f\u7528dict\u521b\u5efa\u8bcd\u6c47\u8868\u5bf9\u8c61<\/h2>\n\n\n\n<p><code>torchtext.vocab.vocab<\/code>(&nbsp;<em>ordered_dict: Dict<\/em>&nbsp;,&nbsp;<em>min_freq: int = 1&nbsp;<\/em>)&nbsp;\u2192 torchtext.&nbsp; <code>vocab<\/code>.Vocab<a href=\"https:\/\/pytorch.org\/text\/stable\/_modules\/torchtext\/vocab\/vocab_factory.html#vocab\">[\u6765\u6e90]<\/a><a href=\"https:\/\/pytorch.org\/text\/stable\/vocab.html?highlight=vocab#torchtext.vocab.vocab\"><\/a><\/p>\n\n\n\n<p>\u7528\u4e8e\u521b\u5efa\u5c06\u6807\u8bb0\u6620\u5c04\u5230\u7d22\u5f15\u7684vocab\u5bf9\u8c61\u7684\u5de5\u5382\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u6784\u5efavocab\u65f6\uff0c\u5c06\u9075\u5b88\u5728ordered_dict\u4e2d\u63d2\u5165\u952e\u503c\u5bf9\u7684\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6309\u6807\u8bb0\u9891\u7387\u6392\u5e8f\u5bf9\u7528\u6237\u5f88\u91cd\u8981\uff0c\u5219\u5e94\u4ee5\u53cd\u6620\u8fd9\u4e00\u70b9\u7684\u65b9\u5f0f\u521b\u5efaordered_dict\u3002<\/p>\n\n\n\n<p>\u53c2\u6570<\/p>\n\n\n\n<ul><li><strong>ordered_dict<\/strong>&nbsp;\u2013 \u6709\u5e8f\u5b57\u5178\u5c06\u6807\u8bb0\u6620\u5c04\u5230\u5b83\u4eec\u76f8\u5e94\u7684\u51fa\u73b0\u9891\u7387\u3002<\/li><li><strong>min_freq<\/strong>&nbsp;\u2013 \u5728\u8bcd\u6c47\u8868\u4e2d\u5305\u542b\u4e00\u4e2a\u6807\u8bb0\u6240\u9700\u7684\u6700\u5c0f\u9891\u7387\u3002<\/li><\/ul>\n\n\n\n<p>Returns<\/p>\n\n\n\n<p>A&nbsp;Vocab&nbsp;objectReturn type<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.org\/text\/stable\/vocab.html?highlight=vocab#torchtext.vocab.Vocab\">torchtext.vocab.Vocab<\/a><\/p>\n\n\n\n<h3>\u6839\u636e\u5206\u597d\u8bcd\u7684\u8bad\u7ec3\u6570\u636e\u96c6\u6765\u521b\u5efa\u8bcd\u5178\uff0c\u8fc7\u6ee4\u6389\u4e86\u51fa\u73b0\u6b21\u6570\u5c11\u4e8e5\u7684\u8bcd\u3002<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<strong>#\u5b9e\u4f8b<\/strong>\ndef get_vocab_imdb(data):\n    tokenized_data = get_tokenized_imdb(data)\n    counter = collections.Counter(&#91;tk for st in tokenized_data for tk in st])\n    return Vocab.vocab(counter, min_freq=5)\n\nvocab = get_vocab_imdb(train_data)\n'# words in vocab:', len(vocab)\n\n\u8f93\u51fa\uff1a('# words in vocab:', 46151)\n\n<\/code><\/pre>\n\n\n\n<h2>SubwordVocab\uff1a \u6784\u5efa\u5b50\u8bcd\u6c47\u8868<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CLASS torchtext.vocab.SubwordVocab(counter, max_size=None, specials='&lt;pad&gt;'\n    vectors=None, unk_init=...)\n  __init__(counter, ...)\n      \u4ece collections.Counter \u6784\u5efa\u5b50\u8bcd\u8bcd\u6c47\u8868\uff0c\n      specials - padding or eos \u7b49<\/code><\/pre>\n\n\n\n<h2>Vectors\uff1a\u8fd4\u56de\u8bcd\u5411\u91cf<\/h2>\n\n\n\n<h2>Look up embedding vectors of tokens<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CLASS torchtext.vocab.Vectors(name, cache=None, url=None, unk_init=None,\n    max_vectors=None)\n  __init__(name, cache=None, url=None, unk_init=None, max_vectors=None)\n      name - \u5305\u542b\u5411\u91cf\u7684\u6587\u4ef6\u540d\n      cache - \u7f13\u5b58\u5411\u91cf\u6587\u4ef6\u7684\u76ee\u5f55\n      url - \u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u627e\u5230\u5411\u91cf\u6587\u4ef6\uff0c\u5219\u4ece\u8be5\u94fe\u63a5\u4e0b\u8f7d\n      max_vectors - \u7528\u4e8e\u9650\u5236\u52a0\u8f7d\u7684\u9884\u8bad\u7ec3\u6b64\u5411\u91cf\u7684\u6570\u91cf\u3002\u5927\u90e8\u5206\u9884\u8bad\u7ec3\u7684\u5411\u91cf\u96c6\u90fd\u662f\u6309\u7167\u8bcd\u9891\u964d\u5e8f\u6392\u5217\n         \u5728\u6574\u4e2a\u96c6\u5408\u4e0d\u9002\u5408\u5185\u5b58\u6216\u8005\u4e0d\u9700\u8981\u5168\u90e8\u52a0\u8f7d\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u52a0\u6b64\u9650\u5236\u3002\n  get_vecs_by_tokens(tokens, lower_case_backup=False)\n      \u7528\u4e8e\u67e5\u627e\u8bcd\u6761\u7684\u5d4c\u5165\u5411\u91cf\u3002\n      tokens - \u4e00\u4e2a\u8bcd\u6761\u6216\u8bcd\u6761\u5217\u8868\u3002\u5982\u679c\u4e00\u4e2a\u8bcd\u6761\uff0c\u8fd4\u56de self.dim \u5f62\u72b6\u7684\u4e00\u7ef4\u5f20\u91cf\uff1b\u5982\u679c\u5217\u8868\uff0c\u8fd4\u56de\n             \uff08len(tokens), self.dim\uff09\u5f62\u72b6\u7684\u4e8c\u7ef4\u5f20\u91cf\u3002\n      lower_case_backup -  \u662f\u5426\u67e5\u627e\u5c0f\u5199\u7684\u8bcd\u6761\u3002\u5982\u679c\u4e3aTrue\uff0c\u5148\u6309\u539f\u683c\u5f0f\u5728 `stoi` \u67e5\u627e\uff0c\n            \u6ca1\u627e\u5230\u7684\u8bdd\uff0c\u518d\u67e5\u5c0f\u5199\u683c\u5f0f\n\nexamples = &#91;'chip', 'baby', 'Beautiful']\nvec = text.vocab.GloVe(name='6B', dim=50)\nret = vec.get_vecs_by_tokens(tokens, lower_case_backup=True)<\/code><\/pre>\n\n\n\n<h2>build_vocab_from_iterator \uff1a\u4ece\u8fed\u4ee3\u5668\u521b\u5efavocab<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>torchtext.vocab.build_vocab_from_iterator(iterator, num_lines=None)\n    \u4ece \u8fed\u4ee3\u5668 \u5efa\u7acb\u8bcd\u6c47\u8868\n    iterator - \u5fc5\u987b\u4ea7\u751f\u8bcd\u6761\u7684\u5217\u8868\u6216\u8fed\u4ee3\u5668\n    num_lines - \u8fed\u4ee3\u5668\u8fd4\u56de\u5143\u7d20\u7684\u9884\u671f\u6570\u91cf\u3002\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><em>torchtext.vocab.build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: Optional&#91;List&#91;str]] = None, special_first: bool = True) \u2192 torchtext.vocab.vocab.Vocab&#91;SOURCE]\nBuild a Vocab from an iterator.\n\nParameters\niterator \u2013 Iterator used to build Vocab. Must yield list or iterator of tokens.\n\nmin_freq \u2013 The minimum frequency needed to include a token in the vocabulary.\n\nspecials \u2013 Special symbols to add. The order of supplied tokens will be preserved.\n\nspecial_first \u2013 Indicates whether to insert symbols at the beginning or at the end.<\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img src=\"https:\/\/cdn.pixabay.com\/photo\/2021\/05\/11\/06\/22\/night-sky-6245049_960_720.jpg\" alt=\"\"\/><figcaption>https:\/\/pixabay.com\/<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>NLP\u5e38\u89c1\u7684\u6570\u636e\u9884\u5904\u7406\u5de5\u4f5c\u5982\u4e0b\uff1a Load File\uff1a\u6570\u636e\u6587\u4ef6\u52a0\u8f7d\uff1b Tokenization\uff1a\u5206\u8bcd\uff1b Cre &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/16\/torchtext-vocab\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">torchtext.vocab\u7684\u5b66\u4e60<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/263"}],"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=263"}],"version-history":[{"count":37,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":420,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions\/420"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}