其他分享
首页 > 其他分享> > torchtext.vocab

torchtext.vocab

作者:互联网

class Vocab

功能:用于创建字典和应用字典

函数:

示例:

from torchtext.vocab import Vocab, vocab
from collections import Counter, OrderedDict

counter = Counter(["a", "b", "b", "b", "c", "c"])
sorted_by_freq_tuples = sorted(counter.items(), key=lambda x:x[1], reverse=True)

ordered_dict = OrderedDict(sorted_by_freq_tuples)

v1 = vocab(ordered_dict)
# get_itos() → List[str]
print(v1.get_itos())

# get_stoi() → Dict[str, int]
print(v1.get_stoi())

# insert_token(token: str, index: int) → None
v1.insert_token("d", 1)
print(v1.get_stoi())

# lookup_indices(tokens: List[str]) → List[int]
print(v1.lookup_indices(["a", "a", "b", "c", "d"]))

# lookup_token(index: int) → str
print(v1.lookup_token(0))

# lookup_tokens(indices: List[int]) → List[str]
print(v1.lookup_tokens([2, 2, 0, 1, 3]))

# lookup_token(index: int) → str
print(v1.lookup_token(0))

示例结果:

['b', 'c', 'a']
{'b': 0, 'c': 1, 'a': 2}
{'b': 0, 'd': 1, 'c': 2, 'a': 3}
[3, 3, 0, 2, 1]
b
['c', 'c', 'b', 'd', 'a']
b

vocab(ordered_dict: Dict, min_freq: int = 1) → torchtext.vocab.Vocab

功能:

创建Vocab对象的工厂方法。

注意:在构建vocab对象时,将会参照传入的dict在构建时的键值对顺序。因此单词是否按照词频排序对用户来说很重要,推荐用ordered_dict来构建词典。

参数:

返回值:对应的Vocab对象

示例:

from torchtext.vocab import Vocab, vocab
from collections import Counter, OrderedDict

counter = Counter(["a", "b", "b", "b", "c", "c"])
sorted_by_freq_tuples = sorted(counter.items(), key=lambda x:x[1], reverse=True)
sorted_by_freq_tuples

ordered_dict = OrderedDict(sorted_by_freq_tuples)
ordered_dict

v1 = vocab(ordered_dict,  min_freq=2)
print(v1.get_stoi())

tokens = ["e", "d","d","d", "c", "b", "a"]
v2 = vocab(OrderedDict([(token, 1) for token in tokens]))
unk_token = "<unk>"
default_index = -1
if unk_token not in v2: v2.insert_token(unk_token, 0)
print(v2.get_stoi())

示例结果:

{'b': 0, 'c': 1}
{'<unk>': 0, 'e': 1, 'd': 2, 'c': 3, 'b': 4, 'a': 5}

build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: Optional[List[str]] = None, special_first: bool = True) → torchtext.vocab.Vocab

功能:用迭代器来生成Vocab对象。

参数:

返回值:对应的Vocab对象

示例:

from torchtext.vocab import build_vocab_from_iterator

tokens = ["e", "d","d","d", "c", "b", "b", "a"]
vocab = build_vocab_from_iterator(tokens, min_freq=2, specials=["<unk>", "<eos>"],  special_first=False)
print(vocab.get_itos())

示例结果:

['d', 'b', '<unk>', '<eos>']

class Vectors

功能:存储词的嵌入向量。

函数:

示例:

from torchtext.vocab import build_vocab_from_iterator,GloVe,Vectors

vec = Vectors(".vector_cache/glove.6B.50d.txt")
print("hi:", vec["hi"])

examples = ["ChIP", "chip"]
ret = vec.get_vecs_by_tokens(examples, lower_case_backup=True)

print("\n".join(["{}:{}".format(examples[i], ret[i]) for i in range(len(examples))]))

示例结果:

hi: tensor([-0.5431,  0.3443,  0.2713,  1.0487, -1.1642, -1.2722,  0.3578, -0.5653,
        -0.2988,  0.8518,  0.5222, -0.0020, -0.4643,  0.0336,  0.0484,  0.7876,
         0.0760,  0.5158,  0.3478,  0.5380,  0.2830, -0.1313, -0.0738,  0.4261,
         0.0310, -0.5503, -0.9979, -0.2895,  0.3052, -1.1194,  1.2957,  0.9117,
         0.3222,  0.9341, -0.3415, -0.6271, -0.0922,  0.5090,  0.2920, -0.2012,
         0.1961, -0.4588,  1.1099, -0.6874,  1.5724, -0.1045,  0.2359, -0.5659,
         0.4368,  0.9809])
ChIP:tensor([-0.7710, -1.1697,  1.5195,  0.8371,  0.7419, -0.2185, -0.7212, -0.9400,
        -0.0113,  0.5485,  0.4040, -0.1846, -0.4630,  0.2620, -0.6464,  0.3599,
        -0.8610, -0.3869, -0.0271, -1.0254,  0.3280, -0.7500, -0.6859, -0.6912,
         0.3429, -0.6660, -0.2910, -0.6104,  0.3322, -0.4252,  2.4573, -0.8748,
         0.4891,  1.2888,  0.5780, -0.5509, -0.2263,  0.8127,  0.7048, -0.5498,
         0.3620, -0.2171, -0.2991,  0.2917,  1.2260,  0.2446,  1.2133, -0.0967,
         0.0474,  0.1971])
chip:tensor([-0.7710, -1.1697,  1.5195,  0.8371,  0.7419, -0.2185, -0.7212, -0.9400,
        -0.0113,  0.5485,  0.4040, -0.1846, -0.4630,  0.2620, -0.6464,  0.3599,
        -0.8610, -0.3869, -0.0271, -1.0254,  0.3280, -0.7500, -0.6859, -0.6912,
         0.3429, -0.6660, -0.2910, -0.6104,  0.3322, -0.4252,  2.4573, -0.8748,
         0.4891,  1.2888,  0.5780, -0.5509, -0.2263,  0.8127,  0.7048, -0.5498,
         0.3620, -0.2171, -0.2991,  0.2917,  1.2260,  0.2446,  1.2133, -0.0967,
         0.0474,  0.1971])

注意:

预训练词向量

GloVe

torchtext.vocab.GloVe(name='840B', dim=300, **kwargs)

FastText

torchtext.vocab.FastText(language='en', **kwargs)

CharNGram

torchtext.vocab.CharNGram(**kwargs)

标签:vocab,int,token,列表,tokens,torchtext,str
来源: https://blog.csdn.net/qq_42464569/article/details/120803135