code: def test_to_onehot():
      indices = torch.tensor([0, 1, 2, 3], dtype=torch.long)
      actual = to_onehot(indices, 4)
      expected = torch.eye(4, dtype=torch.uint8)
      assert actual.equal(expected)
  
      y = torch.randint(0, 21, size=(1000,))
      y_ohe = to_onehot(y, num_classes=21)
      y2 = torch.argmax(y_ohe, dim=1)
      assert y.equal(y2)
  
      y = torch.randint(0, 21, size=(4, 250, 255))
      y_ohe = to_onehot(y, num_classes=21)
      y2 = torch.argmax(y_ohe, dim=1)
      assert y.equal(y2)
  
      y = torch.randint(0, 21, size=(4, 150, 155, 4, 6))
      y_ohe = to_onehot(y, num_classes=21)
      y2 = torch.argmax(y_ohe, dim=1)
      assert y.equal(y2)
  
      # Test with `TorchScript`
  
      x = torch.tensor([0, 1, 2, 3])
  
      # Test the raw `to_onehot` function
      scripted_to_onehot = torch.jit.script(to_onehot)
      assert scripted_to_onehot(x, 4).allclose(to_onehot(x, 4))
  
      # Test inside `torch.nn.Module`
      class SLP(torch.nn.Module):
          def __init__(self):
              super(SLP, self).__init__()
              self.linear = torch.nn.Linear(4, 1)
  
          def forward(self, x):
              x = to_onehot(x, 4)
              return self.linear(x.to(torch.float))
  
      eager_model = SLP()
      scripted_model = torch.jit.script(eager_model)
  
      assert eager_model(x).allclose(scripted_model(x))
  
file_path: ignite/tests/ignite/test_utils.py
imports: import logging
  import sys
  from collections import namedtuple
  import pytest
  import torch
  from packaging.version import Version
  from ignite.engine import Engine, Events
  from ignite.utils import convert_tensor, deprecated, hash_checkpoint, setup_logger, to_onehot
label_desc: Use image node_id 2 for calling a global function with example usage: test_to_onehot() and returns: self