I’m struggling to figure out how to do this, if it’s possible at all. I have a torchscript function that computes a tensor on each iteration. Because the tensors have different shapes, it’s not convenient to simply concatenate the tensors, so I’m collecting them in a list. Because only tuples can be returned from torchscript functions, I’m trying to convert the final list to a tuple to return it. However, the usual python tuple(my_list) constructor doesn’t seem to work, yielding the error “expected a value of type Tensor for argument ‘0’ but found Tensor”. Apparently it’s trying to place the entire list into the first element of the tuple, rather than expanding it into a tuple of the same length as the list, as would be expected with the usual python semantics.
Here is some example code to show what I’m trying to do:
import torch
@torch.jit.script
def f(x):
list_of_tensors = []
for n in range(3):
list_of_tensors.append(torch.ones([10]))
return tuple(list_of_tensors)
traced_script_module = torch.jit.trace(f, [])
The error I get in this case is
RuntimeError Traceback (most recent call last)
in
1 import torch
2
----> 3 @torch.jit.script
4 def f(x):
5~/anaconda3/lib/python3.6/site-packages/torch/jit/init.py in script(obj, optimize, _frames_up, _rcb)
822 else:
823 ast = get_jit_def(obj)
→ 824 fn = torch._C._jit_script_compile(ast, _rcb, get_default_args(obj))
825 # Forward docstrings
826 fn.doc = obj.docRuntimeError:
for operator (Tensor 0) → Tensor:
expected a value of type Tensor for argument ‘0’ but found Tensor
Empty lists default to List[Tensor]. Use torch.jit.annotate(List[my_type], ) to create an empty list of another type
@torch.jit.script
def f(x):list_of_tensors = [] for n in range(3): list_of_tensors.append(torch.ones([10])) return tuple(list_of_tensors) ~~~~~~~~~~~~~~~ <--- HERE
:
@torch.jit.script
def f(x):list_of_tensors = [] for n in range(3): list_of_tensors.append(torch.ones([10])) return tuple(list_of_tensors) ~~~~~ <--- HERE