cara menggunakan objek python khusus di skrip obor

Saya siap mengonversi modul pytorch ke ScriptModule dan kemudian memuatnya di c++, tetapi saya diblokir oleh kesalahan ini This attribute exists on the Python module, but we failed to convert Python type: 'Vocab' to a TorchScript type, Vocab adalah objek python yang saya definisikan. kode demonya ada di sini:

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)

pesan kesalahan:

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)

jadi bagaimana saya bisa menggunakan objek python saya sendiri di torchscript?


person Chuanhua Yang    schedule 09.06.2020    source sumber


Jawaban (1)


Anda harus membubuhi keterangan Vocab Anda dengan torchscript.jit seperti ini:

@torch.jit.script
class Vocab(object):
    def __init__(self, name: str):
        self.name = name

    def show(self):
        print("dict:" + self.name)

Perhatikan juga spesifikasi name: str karena ini juga diperlukan agar skrip obor dapat menyimpulkan tipenya (PyTorch mendukung anotasi tipe >=Python3.6, Anda juga dapat menggunakan komentar, tetapi kurang jelas).

Silakan lihat kelas Torchscript dan Jenis Default dan info torchscript terkait lainnya di sana.

person Szymon Maszke    schedule 09.06.2020
comment
Ya, saran Anda benar, berhasil, terima kasih banyak. saya melewatkan dokumen bagian ini. - person Chuanhua Yang; 10.06.2020