简体   繁体   English

Pytorch 相当于 tensorflow keras StringLookup?

[英]Pytorch equivalent of tensorflow keras StringLookup?

I'm working with pytorch now, and I'm missing a layer: tf.keras.layers.StringLookup that helped with the processing of ids.我现在正在使用 pytorch,但我缺少一个层: tf.keras.layers.StringLookup ,它有助于处理 ids。 Is there any workaround to do something similar with pytorch?有什么解决方法可以用 pytorch 做类似的事情吗?

An example of the functionality I'm looking for:我正在寻找的功能示例:

vocab = ["a", "b", "c", "d"]
data = tf.constant([["a", "c", "d"], ["d", "a", "b"]])
layer = tf.keras.layers.StringLookup(vocabulary=vocab)
layer(data)

Outputs:
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 3, 4],
       [4, 1, 2]])>

You can use Collections.Counter along with torchtext 's vocab object to construct a lookup function from your vocabulary.您可以使用Collections.Countertorchtextvocab对象从您的词汇表中构建查找功能。 You can then easily pass sequences to this and get their encodings as a tensor:然后,您可以轻松地将序列传递给它,并将它们的编码作为张量:

from torchtext.vocab import vocab
from collections import Counter

tokens = ["a", "b", "c", "d"]
samples = [["a", "c", "d"], ["d", "a", "b"]]

# Build string lookup
lookup = vocab(Counter(tokens))
>>> torch.tensor([lookup(s) for s in samples])
tensor([[0, 2, 3],
        [3, 0, 1]])

you can use the library torchtext, just install it with python3 -m pip install torchtext您可以使用库 torchtext,只需使用 python3 -m pip install torchtext 安装它

and you can is like this:你可以是这样的:

from torchtext.vocab import vocab
from collections import OrderedDict

tokens = ['a', 'b', 'c', 'd']
v1 = vocab(OrderedDict([(token, 1) for token in tokens]))
v1.lookup_indices(["a","b","c"])

and this is the result:这是结果:

([0, 1, 2],)

Package torchnlp,包 torchnlp,

pip install pytorch-nlp
from torchnlp.encoders import LabelEncoder

data = ["a", "c", "d", "e", "d"]
encoder = LabelEncoder(data, reserved_labels=['unknown'], unknown_index=0)

enl = [encoder.encode(x) for x in data]

print(enl)
[tensor(1), tensor(2), tensor(3), tensor(4), tensor(3)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM