Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

Explain why a supply curve slopes upward.

Answered: 1 week ago

Question

Describe key employee expectations.

Answered: 1 week ago

Question

Describe current business topics and their impact on HRM.

Answered: 1 week ago

Question

Define human resources management (HRM).

Answered: 1 week ago