Answered step by step
Verified Expert Solution
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:
computeevaluations
logits: torch.Tensor,
targets: torch.Tensor,
numclasses: int
tupletorchTensor, float, float:
The function parameters are described as follows:
logits: A D tensor of shape nsamples,numclasses containing the raw model outputs
for all samples. The data type must be a floating point data type.
targets: A D tensor of shape nsamples, 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
numclasses: 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 scikitlearn 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.isfloatingpoint
targets is not a torch.Tensor of integer data type one of torch.uint torch.int
torch.int torch.int torch.int
Your function should raise a ValueError if
the shape of logits is not D
the shape of targets is not D
the first dimension of logits does not have the same size as targets
targets is not an integer tensor that contains values smaller than numclasses. Example program execution:
if namemain:
torch.manualseed
logits torch.randsize
targets torch.randintlow high size
numclasses
confusionmatrix, accuracy, balancedaccuracy
computeevaluationslogits targets, numclasses
printconfusionmatrix
printfAccuracy: accuracy:f
printfBalanced accuracy: balancedaccuracy:f
Example output randomization might vary due to version differences:
tensor
Accuracy:
Balanced accuracy:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started