MCPcopy Create free account
hub / github.com/apple/ml-pointersect / construct_onehot_vectors

Function construct_onehot_vectors

cdslib/core/nn/nn_utils.py:294–304  ·  view source on GitHub ↗

Given labels :param labels: (seq_len, batch_size,), (batch_size,), or (*) int, [0, total_classes-1] :param total_classes: total number of classes :return: onehot embedding in float (*, total_classes) and same device as labels

(labels: torch.Tensor, total_classes: int)

Source from the content-addressed store, hash-verified

292
293
294def construct_onehot_vectors(labels: torch.Tensor, total_classes: int) -> torch.Tensor:
295 """
296 Given labels
297 :param labels: (seq_len, batch_size,), (batch_size,), or (*) int, [0, total_classes-1]
298 :param total_classes: total number of classes
299 :return: onehot embedding in float (*, total_classes) and same device as labels
300 """
301 ori_label_shape = labels.shape
302 onehots = torch.zeros(labels.numel(), total_classes, device=labels.device)
303 onehots.scatter_(1, labels.view(-1, 1), 1)
304 return onehots.view(*ori_label_shape, total_classes)
305
306
307class HyperParams(ABC, MutableMapping):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected