mindspore.ops.ComputeAccidentalHits

class mindspore.ops.ComputeAccidentalHits(num_true=1)[source]

Compute accidental hits of sampled classes which match target classes.

When a target class matches the sample class, we call it “accidental hit”. The result of calculating accidental hits contain three parts (index, id, weight), where index represents the row number in true_classes, and id represents the position in sampled_candidates, the weight is -FLOAT_MAX. FLOAT_MAX indicates the max value in the type of Float

Parameters

num_true (int) – The number of target classes per training example. Default: 1.

Inputs:
  • true_classes (Tensor) - The target classes. With data type of int32 or int64 and shape \((batch\_size, num\_true)\).

  • sampled_candidates (Tensor) - The Candidate sampling results of operators, types of training samples, with data type of int32 or int64 and shape \((num\_sampled, )\).

Outputs:

Tuple of 3 Tensors.

  • indices (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.

  • ids (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.

  • weights (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the type float32.

Raises
  • TypeError – If dtype of num_true is not int.

  • TypeError – If true_classes or sampled_candidates is not a Tensor.

  • TypeError – If dtype of true_classes or sampled_candidates is neither int32 nor int64.

Supported Platforms:

Ascend

Examples

>>> true_classes = np.array([[1, 2], [0, 4], [3, 3]])
>>> sampled_candidates = np.array([0, 1, 2, 3, 4])
>>> sampler = ops.ComputeAccidentalHits(2)
>>> indices, ids, weights = sampler(Tensor(true_classes), Tensor(sampled_candidates))
>>> print(indices, ids, weights)
[0 0 1 1 2 2]
[1 2 0 4 3 3]
[-3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38]