如何使用脚本转换pytorch transformer?
我正在尝试编译pytorch transformer以在C++中运行它:如何使用脚本转换pytorch transformer?,pytorch,transformer,torchscript,Pytorch,Transformer,Torchscript,我正在尝试编译pytorch transformer以在C++中运行它: from torch.nn import TransformerEncoder, TransformerEncoderLayer encoder_layers = TransformerEncoderLayer(1000, 8, 512, 0.1) transf = TransformerEncoder(encoder_layers, 6) sm = torch.jit.script(transf) 但我得到了一个错误:
from torch.nn import TransformerEncoder, TransformerEncoderLayer
encoder_layers = TransformerEncoderLayer(1000, 8, 512, 0.1)
transf = TransformerEncoder(encoder_layers, 6)
sm = torch.jit.script(transf)
但我得到了一个错误:
RuntimeError: Expected a default value of type Tensor on parameter
"src_mask": File "C:\Program Files (x86)\Microsoft Visual
Studio\Shared\Python36_64\lib\site-packages\torch\nn\modules\transformer.py",
line 271
def forward(self, src, src_mask=None, src_key_padding_mask=None):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~... <--- HERE
r"""Pass the input through the encoder layer.
RuntimeError:参数上应为Tensor类型的默认值
“src\u掩码”:文件“C:\Program Files(x86)\Microsoft Visual
Studio\Shared\Python36\u 64\lib\site packages\torch\nn\modules\transformer.py“,
第271行
def forward(自我、src、src_掩码=无,src_键\u填充\u掩码=无):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~... 您需要升级到PyTorch 1.5.0,旧版本不支持将转换器转换为TorchScript(JIT)模块
在1.5.0中,您将看到一些关于被声明为常量的参数的警告,例如:
UserWarning: 'q_proj_weight' was found in ScriptModule constants, but it is a non-constant parameter. Consider removing it.
可以放心地忽略这些
UserWarning: 'q_proj_weight' was found in ScriptModule constants, but it is a non-constant parameter. Consider removing it.