question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`to_onehot` can't be torchscripted

See original GitHub issue

🐛 Bug description

I need to do one hot conversion and I use ignite.utils.to_onehot, but when I try to torchscript the model, it occurs a RuntimeError. Stacktrace:

RuntimeError: 
cannot statically infer the expected size of a list in this context:
  File "/usr/local/lib/python3.6/dist-packages/ignite/utils.py", line 59
    input's device`.
    """
    onehot = torch.zeros(indices.shape[0], num_classes, *indices.shape[1:], dtype=torch.uint8, device=indices.device)
                                                        ~~~~~~~~~~~~~~~~~ <--- HERE
    return onehot.scatter_(1, indices.unsqueeze(1), 1)
'to_onehot' is being compiled since it was called from 'SLP.forward'
  File "<ipython-input-3-0f76d1651906>", line 12
    def forward(self, x):
        x = to_onehot(x, 10)
        ~~~~~~~~~~~~~~~~~~~ <--- HERE
        return self.l1(x)

Environment

Colab

Reproducible Link

https://colab.research.google.com/drive/13mWdz9sGXvAHgKd3vtp5I6Y-gZDRlNAd?usp=sharing

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vfdev-5commented, Jan 27, 2021

Here is how it can be implemented.

def to_onehot(indices: torch.Tensor, num_classes: int) -> torch.Tensor:
    new_shape = (indices.shape[0], num_classes) + indices.shape[1:]
    onehot = torch.zeros(new_shape, dtype=torch.uint8, device=indices.device)
    return onehot.scatter_(1, indices.unsqueeze(1), 1)

@ydcjeff if you’d like to fix it, please send a PR (and if with a test it would be awesome) 😃

1reaction
Devanshu24commented, Jan 27, 2021

@Devanshu24 would you like to fix this issue according to the discussion ?

I would love to, I will go through your conversion in a little more detail (have gone through once). I think I will be able to work on it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to call super method with TorchScript #42885 - GitHub
When I try to compile the code to TorchScript, I get: Tried to access nonexistent attribute or method 'forward' of type 'Tensor'.
Read more >
Unable to convert the pytorch model to the TorchScript format
torch.jit.script create a ScriptFunction(a Function with Graph) by parsing the python source code from module.forward().
Read more >
How to Create PyTorch One Hot Encoding? - eduCBA
A one-hot encoding permits the portrayal of downright information to be more expressive. Many AI calculations can't work with downright information ...
Read more >
TorchScript — PyTorch 1.13 documentation
TorchScript is a way to create serializable and optimizable models from PyTorch code. Any TorchScript program can be saved from a Python process...
Read more >
Internals: TMVA PyTorch Interface | GSoC - Anirudh Dagar
Can't See? ... then, convert class number to one-hot vectors. ... Load PyTorch (torchscript) model from checkpoint .tar file or .pt/.pth ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found