作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我准备将pytorch模块转换为ScriptModule,然后在c++中加载它,但我被这个错误阻止了 This attribute exists on the Python module, but we failed to convert Python type: 'Vocab' to一个 TorchScript 类型
,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 = len(x) // 2
for i in range(step):
enc_hidden.append((x[2*i] + x[2*i + 1])/2)
enc_hidden = torch.stack(enc_hidden, 0)
enc_hidden = self.__show(enc_hidden)
return self.layers(enc_hidden)
@torch.jit.export
def __show(self, x):
return x + 1
model = Model()
data = torch.randn(10, 2)
script_model = torch.jit.script(model)
print(script_model)
r1 = model(data)
print(r1)
错误信息:
Traceback (most recent call last):
File "/mnt/d/python_projects/pytorch_deploy/model4.py", line 47, in <module>
script_model = torch.jit.script(model)
File "/mnt/d/anaconda3/lib/python3.6/site-packages/torch/jit/__init__.py", line 1261, in script
return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)
File "/mnt/d/anaconda3/lib/python3.6/site-packages/torch/jit/_recursive.py", line 305, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File "/mnt/d/anaconda3/lib/python3.6/site-packages/torch/jit/_recursive.py", line 361, in create_script_module_impl
create_methods_from_stubs(concrete_type, stubs)
File "/mnt/d/anaconda3/lib/python3.6/site-packages/torch/jit/_recursive.py", line 279, in create_methods_from_stubs
concrete_type._create_methods(defs, rcbs, defaults)
RuntimeError:
Module 'Model' has no attribute 'vocab' (This attribute exists on the Python module, but we failed to convert Python type: 'Vocab' to a TorchScript type.):
File "/mnt/d/python_projects/pytorch_deploy/model4.py", line 26
def forward(self, x):
name = self.vocab.name
~~~~~~~~~~ <--- HERE
print("forward show encoder:" + str(self.encoder))
print("vocab:" + name)
那么如何在 torchscript 中使用我自己的 python 对象呢?
最佳答案
你必须像这样用 torchscript.jit
注释你的 Vocab
:
@torch.jit.script
class Vocab(object):
def __init__(self, name: str):
self.name = name
def show(self):
print("dict:" + self.name)
还要注意规范 name: str
因为 torchscript 也需要它来推断它的类型(PyTorch 支持 >=Python3.6
类型注释,你可以使用注释作为好吧,但不太清楚)。
请参阅Torchscript classes和 Default Types以及其他相关的 torchscript
信息。
关于python - 如何在 torchscript 中使用自定义 python 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62279080/
我是一名优秀的程序员,十分优秀!