[Pytorch] Convert the matrix to onehot

1. Method One

import torch

def one_hot(arr):
    zero_arr=torch.zeros(len(arr),max(arr)+1)
    zero_arr[torch.arange(len(arr)),arr]=1

    return zero_arr

Guess you like

Origin blog.csdn.net/qq_43586192/article/details/111536772