小编Chu*_*ang的帖子

如何在torchscript中使用自定义python对象

我准备将 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)

python jit pytorch torchscript

2
推荐指数
1
解决办法
1686
查看次数

标签 统计

jit ×1

python ×1

pytorch ×1

torchscript ×1