Pytorch基础系列(3)
导包
1 | import collections |
读取数据集
1 | d2l.DATA_HUB['time_machine'] = (d2l.DATA_URL + 'timemachine.txt', |
输出:
1 | ['the time machine by h g wells', '', '', '', '', 'i', '', '', 'the time traveller for so it will be convenient to speak of him', 'was expounding a recondite matter to us his grey eyes shone and'] |
词元化
将文本行列表(lines)作为输入,列表中的每个元素是一个文本序列(如一条文本行)。
每个文本序列又被拆分成一个词元列表,词元(token) 是文本的基本单位。
最后,返回一个由词元列表组成的列表,其中的每个词元都是一个字符串(string)。
1 | def tokenize(lines, token='word'): |
实际上,本例子种只将每次词作为一个词元,所以 token == 'char' 是不会有的。因此,tokenize 在本例中可以简化为:
1 | def tokenize(lines): |
输出:
1 | [['the', 'time', 'machine', 'by', 'h', 'g', 'wells'], |
记录词频
isinstance(A, B):判断 A 是不是 B 类型的
1 | def count_corpus(tokens): |
1 | # counter = count_corpus(tokens) |
输出:
1 | {'the': 2261, 'time': 200, 'machine': 85, 'by': 103, 'h': 1, 'g': 1, 'wells': 9, 'i': 1267, 'traveller': 61, 'for': 221, 'so': 112, 'it': 437, 'will': 37, 'be': 93, 'convenient': 5, 'to': 695, 'speak': 6, 'of': 1155, 'him': 40, 'was': 552, 'expounding': 2, 'a': 816, 'recondite': 1, 'matter': 6, 'us': 35, 'his': 129, 'grey': 11, 'eyes': 35, 'shone': 8, 'and': 1245, 'twinkled': 1, 'usually': 3, 'pale': 10, 'face': 38, 'flushed': 2, 'animated': 3, 'fire': 30, 'burned': 6, 'brightly': 4, 'soft': 16, 'radiance': 1, 'incandescent': 1, 'lights': 1, 'in': 541, 'lilies': 1, 'silver': 6, 'caught': 10, 'bubbles': 1, 'that': 443, 'flashed': 4, 'passed': 13, 'our': 57, 'glasses': 1, 'chairs': 2, 'being': 14, 'patents': 1, 'embraced': 1, 'caressed': 2, 'rather': 18, 'than': 34, 'submitted': 1, 'sat': 22, 'upon': 113, 'there': 127, 'luxurious': 1, 'after': 37, 'dinner': 13, 'atmosphere': 2, 'when': 55, 'thought': 57, 'roams': 1, 'gracefully': 1,……} |
构建词表
tokens:是一个嵌套 list,里面每个 list 表示一行,内部的 list 种是各种单词
min_freq:表示最小出现次数,低于这个次数的单词就会被删除
counter:记录着基于原文的词频统计信息_token_freqs:根据词频排序,返回字典,key 是单词 (token),value 是次数
idx_to_token:是一个 list,用来记录已经加进去的词频满足要求的词enumerate():同时列出数据下标和数据,这里保存的是原文种所有单词再 idx_to_token 的下标
1 | class Vocab: |
下面是我照着自己的想法进行的一点修改:
1 | class Vocab: |
调用
注意: 要想查看 vocab 的内容,需要调用方法并转为 list,即
list(vocab.token_to_idx.items())。
1 | vocab = Vocab(tokens) |
输出:
1 | [('<unk>', 0), ('the', 1), ('i', 2), ('and', 3), ('of', 4), ('a', 5), ('to', 6), ('was', 7), ('in', 8), ('that', 9), ('my', 10), ('it', 11), ('had', 12), ('me', 13), ('as', 14), ('at', 15), ('for', 16), ('with', 17), ('but', 18), ('time', 19), ('were', 20), ('this', 21), ('you', 22), ('on', 23), ('then', 24), ('his', 25), ('there', 26), ('he', 27), ('have', 28), ('they', 29), ('from', 30), ('one', 31), ('all', 32), ('not', 33), ('into', 34), ('upon', 35), ('little', 36), ('so', 37), ('is', 38), ('came', 39), ('by', 40), ('some', 41), ('be', 42), ('no', 43), ('could', 44), ('their', 45), ('said', 46), ('saw', 47), ('down', 48), ('them', 49), ('machine', 50), ('which', 51), ('very', 52), ('or', 53), ('an', 54), ('we', 55), ('now', 56), ('what', 57), ('been', 58), ('these', 59), ('like', 60), ('her', 61), ('out', 62), ('seemed', 63), ('up', 64), ('man', 65), ('about', 66), ('s', 67), ('its', 68), ('thing', 69), ('again', 70), ('traveller', 71), ('would', 72), ('more', 73), ('white', 74), ('our', 75), ('thought', 76), ('felt', 77), ('when', 78), ('over', 79), ('weena', 80), ('still', 81), ('world', 82), ('myself', 83), ('even', 84), ('must', 85), ('through', 86), ('if', 87),……] |
转索引
将每条文本行转为一个数字索引列表。
1 | for i in range(5): |
输出:
1 | ', 'time', 'machine', 'by', 'h', 'g', 'wells'] |
整合
将所有功能打包到load_corpus_time_machine函数中, 该函数返回corpus(词元索引列表)和vocab(时光机器语料库的词表)。
1 | def load_corpus_time_machine(max_tokens=-1): |

