Convert string to int/float in torch script

Currently, I cannot convert simple python strings like “0” or “1” to an integer or float in torchscript. Trying int('0') will result in the error:

Arguments for call are not valid.

And it expects only a tensor, a bool, a float or a number to be converted to int.

With float, it only allows "-inf" or "inf" to be converted.

I tried encoding with bytes, but that isn’t allowed either. Do you have any suggestions for a JIT/Torchscript compatible way to convert strings to ints/floats?

Which version of torch are you using? I just tried this with torch-1.6 (the latest release) and it works:

import torch

def fn() -> int:
    a = int('0')
    return a

s = torch.jit.script(fn)