How to use 'keras confusion matrix' in Python

Every line of 'keras confusion matrix' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16@property
17def numpy(self):
18 return self._matrix.numpy()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4def th_confusion_matrix(y_true: torch.Tensor, y_pred: torch.Tensor, num_classes=None):
5 """
6
7 Args:
8 y_true: 1-D tensor of shape [n_samples]
9 y_pred: 1-D tensor of shape [n_samples]
10 num_classes: scalar
11 Returns:
12
13 """
14 size = [num_classes + 1, num_classes + 1] if num_classes is not None else None
15 y_true = y_true.float()
16 y_pred = y_pred.float()
17 if size is None:
18 cm = torch.sparse_coo_tensor(indices=torch.stack([y_true, y_pred], dim=0), values=torch.ones_like(y_pred))
19 else:
20 cm = torch.sparse_coo_tensor(indices=torch.stack([y_true, y_pred], dim=0), values=torch.ones_like(y_pred),
21 size=size)
22 return cm.to_dense()[1:, 1:]

Related snippets