Skip to content

Instantly share code, notes, and snippets.

@jojonki
Last active January 1, 2018 21:49
Show Gist options
  • Select an option

  • Save jojonki/36b94d0d172add49368e5d37164d403c to your computer and use it in GitHub Desktop.

Select an option

Save jojonki/36b94d0d172add49368e5d37164d403c to your computer and use it in GitHub Desktop.
to_categorical_labels
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return torch.autograd.Variable(x)
def to_categorical(ys, n_classes):
# 1-hot encodes tensors
if len(ys) == 0:
return ys
d = [to_var(torch.from_numpy(np.eye(n_classes, dtype='uint8')[y])) for y in ys]
return torch.stack(d, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment