Python 对于张量列表,Torchscript与torch.cat不兼容

Python 对于张量列表,Torchscript与torch.cat不兼容,python,deep-learning,pytorch,jit,torchscript,Python,Deep Learning,Pytorch,Jit,Torchscript,Torch.cat在torchscript中使用时会引发张量列表错误 下面是一个最小的可复制示例,用于复制错误 import torch import torch.nn as nn """ Smallest working bug for torch.cat torchscript """ class Model(nn.Module): """dummy model for showing error""" def __init__(self): supe

Torch.cat在torchscript中使用时会引发张量列表错误

下面是一个最小的可复制示例,用于复制错误

import torch
import torch.nn as nn

"""
Smallest working bug for torch.cat torchscript
"""


class Model(nn.Module):
    """dummy model for showing error"""

    def __init__(self):
        super(Model, self).__init__()
        pass

    def forward(self):
        a = torch.rand([6, 1, 12])
        b = torch.rand([6, 1, 12])
        out = torch.cat([a, b], axis=2)
        return out


if __name__ == '__main__':
    model = Model()
    print(model())  # works
    torch.jit.script(model)  # throws error
预期结果将是torch.cat的torchscript输出。以下是提供的错误消息:

File "/home/anil/.conda/envs/rnn/lib/python3.7/site-packages/torch/jit/__init__.py", line 1423, in _create_methods_from_stubs
    self._c._create_methods(self, defs, rcbs, defaults)
RuntimeError: 
Arguments for call are not valid.
The following operator variants are available:

  aten::cat(Tensor[] tensors, int dim=0) -> (Tensor):
  Keyword argument axis unknown.

  aten::cat.out(Tensor[] tensors, int dim=0, *, Tensor(a!) out) -> (Tensor(a!)):
  Argument out not provided.

The original call is:
at smallest_working_bug_torch_cat_torchscript.py:19:14
    def forward(self):
        a = torch.rand([6, 1, 12])
        b = torch.rand([6, 1, 12])
        out = torch.cat([a, b], axis=2)
              ~~~~~~~~~ <--- HERE
        return out

File”/home/anil/.conda/envs/rnn/lib/python3.7/site packages/torch/jit/_init__.py“,第1423行,从存根创建方法
self.\u c.\u创建方法(self、defs、rcb、默认值)
运行时错误:
调用的参数无效。
以下操作员变量可用:
aten::cat(张量[]张量,int dim=0)->(张量):
关键字参数轴未知。
aten::cat.out(张量[]张量,int dim=0,*,张量(a!)out)->(张量(a!)):
没有提供论证。
最初的电话是:
在最小的工作时间,bug,torch,cat,torchscript.py:19:14
def转发(自):
a=火炬的.rand([6,1,12])
b=火炬的兰特([6,1,12])
out=火炬。cat([a,b],轴=2)

~~~~~~~~~将
更改为
dim
可修复错误, 原始解决方案已发布