Question: In this bonus exercise, implement the following function that evaluates the predictions of a multiclass classification task using PyTorch: compute _ evaluations ( logits: torch.Tensor,

In this bonus exercise, implement the following function that evaluates the predictions of a multiclass classification task using PyTorch:
compute_evaluations(
logits: torch.Tensor,
targets: torch.Tensor,
num_classes: int
)-> tuple[torch.Tensor, float, float]:
The function parameters are described as follows:
logits: A 2D tensor of shape (n_samples,num_classes) containing the raw model outputs
for all samples. The data type must be a floating point data type.
targets: A 1D tensor of shape (n_samples,) containing the true target class labels for each
sample. The data type must be int. The order of samples is the same as in logits.
num_classes: The number of different classes. This must be a single integer.
In the function, you should generate the actual class predictions for each sample from logits and
then compute the confusion matrix, the accuracy and the balanced accuracy. In your implementation, you are only allowed to use PyTorch. Libraries such as scikit-learn (sklearn) are not allowed
in this exercise.
Your function should raise a TypeError if
logits is not a torch.Tensor of floating point data type (see torch.is_floating_point).
targets is not a torch.Tensor of integer data type (one of torch.uint8, torch.int8,
torch.int16, torch.int32, torch.int64).
Your function should raise a ValueError if
the shape of logits is not 2D
the shape of targets is not 1D.
the first dimension of logits does not have the same size as targets
targets is not an integer tensor that contains values smaller than num_classes. Example program execution:
if __name__=="__main__":
torch.manual_seed(125)
logits = torch.rand(size=(100,10))*10-5
targets = torch.randint(low=0, high=10, size=(100,))
num_classes =10
confusion_matrix, accuracy, balanced_accuracy =
compute_evaluations(logits, targets, num_classes)
print(confusion_matrix)
print(f'Accuracy: {accuracy:.2f}')
print(f'Balanced accuracy: {balanced_accuracy:.2f}')
Example output (randomization might vary due to version differences):
tensor([[2,1,1,1,0,0,0,0,2,0],
[2,1,0,1,1,2,1,1,1,1],
[2,2,0,1,2,1,1,2,1,1],
[1,2,1,3,1,0,0,1,1,3],
[0,3,0,2,0,1,0,0,2,1],
[1,0,0,0,2,1,0,1,0,0],
[0,0,2,2,2,2,1,0,3,1],
[0,2,1,2,0,0,1,2,0,1],
[0,0,0,0,1,1,0,0,1,2],
[1,3,2,0,3,2,2,0,1,1]])
Accuracy: 0.12
Balanced accuracy: 0.14

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!