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. Is there any workaround to do something similar with pytorch?
An example of the functionality I'm looking for:
vocab = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]
data = tf.constant([[&quot;a&quot;, &quot;c&quot;, &quot;d&quot;], [&quot;d&quot;, &quot;a&quot;, &quot;b&quot;]])
layer = tf.keras.layers.StringLookup(vocabulary=vocab)
layer(data)

Outputs:
&lt;tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 3, 4],
       [4, 1, 2]])&gt;


Package torchnlp,
pip install pytorch-nlp

from torchnlp.encoders import LabelEncoder

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

enl = encoder.batch_encode(data)

print(enl)

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

