我正在尝试将 torchtext 词汇层与 torchscript 一起使用,但我遇到了一些错误,我想知道这里是否有人使它工作。
我现在的模型是
class VocabText(torch.nn.Module):
def __init__(self):
super(VocabText, self).__init__()
self.embedding = torch.nn.Embedding(10,128)
vocab = ['This', 'is', 'a', 'test']
counter = Counter(vocab)
self.lookup = text.vocab.vocab(counter)
self.tensor = torch.Tensor
def forward(self, x: str):
x_mapped = self.lookup(x)
x_mapped = self.tensor(x_mapped).int()
x_mapped = self.embedding(x_mapped)
return x
当我像这样通过模型时,这很有效:
example_str = ["is"]
model(example_str)
但是当我尝试用 torchscript 编译它时它失败了:
model_scripted = torch.jit.script(model)
model_scripted.save('model_scripted.pt')
出现以下错误:
RuntimeError:
Unknown builtin op: aten::Tensor.
Here are some suggestions:
当我在转发功能期间映射查找层的结果时
我认为是由于打字,因为词汇层需要字符串作为输入,但嵌入层将使用张量。我在前锋中间做一个演员表。
如果有人想要,我在 colab 中有一个工作笔记本可以重现此问题:https ://colab.research.google.com/drive/14nZF5X8rQrZET_7iA1N2MUV3XSzozpeI?usp=sharing