Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement a simple image classification inference step that turns a model’s raw scores (logits) into a predicted class and confidence for a single image. You’ll compute probabilities with softmax and return the top prediction in a beginner-friendly way.
The softmax formula is:
Implement the function
Rules:
max(logits) before exponentiating).confidence.scipy.special.softmax).Output:
| Argument | Type |
|---|---|
| logits | np.ndarray |
| class_names | list |
| Return Name | Type |
|---|---|
| value | tuple |
Use NumPy only.
Probabilities must be a NumPy array.
Use stable softmax.
Use np.exp(shifted_logits) where shifted_logits = logits - np.max(logits) for stability.
Normalize by dividing by the sum: probs = exp_vals / np.sum(exp_vals).
Use np.argmax(probs) to find the index of the highest probability.
Use logits = np.array(logits) if input is not already an array.