我准备将 pytorch 模块转换为 ScriptModule,然后在 c++ 中加载它?但是我被这个错误阻止了This attribute exists on the Python module, but we failed to convert Python type: 'Vocab' to a TorchScript type,这Vocab是我定义的一个 python 对象。演示代码在这里:
import torch
class Vocab(object):
def __init__(self, name):
self.name = name
def show(self):
print("dict:" + self.name)
class Model(torch.nn.Module):
def __init__(self, ):
super(Model, self).__init__()
self.layers = torch.nn.Linear(2, 3)
self.encoder = 4
self.vocab = Vocab("vocab")
def forward(self, x):
name = self.vocab.name
print("forward show encoder:" + str(self.encoder))
print("vocab:" + name)
enc_hidden = []
step …Run Code Online (Sandbox Code Playgroud)