I implemented the loss function. Here is the gist:

  
      gist.github.com
  
  
    https://gist.github.com/benwu232/1fbf1cd6b637810f5d57902fa6d4ef1b 102
gistfile1.txt
def one_hot(size, index):
    """ Creates a matrix of one hot vectors.
        ```
        import torch
        import torch_extras
        setattr(torch, 'one_hot', torch_extras.one_hot)
        size = (3, 3)
        index = torch.LongTensor([2, 0, 1]).view(-1, 1)
        torch.one_hot(size, index)
        # [[0, 0, 1], [1, 0, 0], [0, 1, 0]]
This file has been truncated. show original




  
  
    
    
  
  


Know issue: Part of the program must run on CPU, it may be slow. In my own test, the speed is ok. If you have better solution, please let me know!